♻️ Refactoring code.use "@SneakyThrows" annotation

This commit is contained in:
lishangbu 2019-03-21 18:49:58 +08:00
parent c8ff9b708c
commit fed4d460a3
15 changed files with 49 additions and 31 deletions

View File

@ -20,6 +20,7 @@ import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.security.component.PigWebResponseExceptionTranslator;
import com.pig4cloud.pig.common.security.service.PigClientDetailsService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@ -55,7 +56,8 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
private final RedisConnectionFactory redisConnectionFactory;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
@SneakyThrows
public void configure(ClientDetailsServiceConfigurer clients) {
PigClientDetailsService clientDetailsService = new PigClientDetailsService(dataSource);
clientDetailsService.setSelectClientDetailsSql(SecurityConstants.DEFAULT_SELECT_STATEMENT);
clientDetailsService.setFindClientDetailsSql(SecurityConstants.DEFAULT_FIND_STATEMENT);

View File

@ -18,6 +18,7 @@ package com.pig4cloud.pig.auth.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pig4cloud.pig.common.security.handler.MobileLoginSuccessHandler;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -51,7 +52,8 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
private AuthorizationServerTokenServices defaultAuthorizationServerTokenServices;
@Override
protected void configure(HttpSecurity http) throws Exception {
@SneakyThrows
protected void configure(HttpSecurity http) {
http
.authorizeRequests()
.antMatchers(
@ -63,7 +65,8 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
@SneakyThrows
public AuthenticationManager authenticationManagerBean() {
return super.authenticationManagerBean();
}

View File

@ -20,6 +20,7 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
@ -46,7 +47,8 @@ import java.util.Properties;
public class DataScopeInterceptor extends AbstractSqlParserHandler implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
@SneakyThrows
public Object intercept(Invocation invocation) {
StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
this.sqlParser(metaObject);

View File

@ -16,6 +16,7 @@
package com.pig4cloud.pig.common.core.util;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
@ -92,7 +93,8 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
*/
@Override
public void destroy() throws Exception {
@SneakyThrows
public void destroy() {
SpringContextHolder.clearHolder();
}

View File

@ -20,6 +20,7 @@ import com.pig4cloud.pig.common.core.util.SpringContextHolder;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.log.event.SysLogEvent;
import com.pig4cloud.pig.common.log.util.SysLogUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
@ -35,7 +36,8 @@ import org.aspectj.lang.annotation.Aspect;
public class SysLogAspect {
@Around("@annotation(sysLog)")
public Object around(ProceedingJoinPoint point, SysLog sysLog) throws Throwable {
@SneakyThrows
public Object around(ProceedingJoinPoint point, SysLog sysLog) {
String strClassName = point.getTarget().getClass().getName();
String strMethodName = point.getSignature().getName();
log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);

View File

@ -27,15 +27,14 @@ import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.exception.PigDeniedException;
import com.pig4cloud.pig.common.core.util.R;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
@ -55,11 +54,10 @@ public class PigAccessDeniedHandler extends OAuth2AccessDeniedHandler {
* @param request request
* @param response response
* @param authException authException
* @throws IOException IOException
* @throws ServletException ServletException
*/
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException authException) throws IOException, ServletException {
@SneakyThrows
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException authException) {
log.info("授权失败,禁止访问 {}", request.getRequestURI());
response.setCharacterEncoding(CommonConstants.UTF8);
response.setContentType(CommonConstants.CONTENT_TYPE);

View File

@ -16,6 +16,7 @@
package com.pig4cloud.pig.common.security.component;
import lombok.SneakyThrows;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@ -25,8 +26,6 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
/**
* @author lengleng
* @date 2019/03/08
@ -40,7 +39,8 @@ public class PigResourceServerAutoConfiguration {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
@SneakyThrows
public void handleError(ClientHttpResponse response) {
if (response.getRawStatusCode() != HttpStatus.BAD_REQUEST.value()) {
super.handleError(response);
}

View File

@ -17,6 +17,7 @@
package com.pig4cloud.pig.common.security.component;
import com.pig4cloud.pig.common.security.exception.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@ -44,7 +45,8 @@ public class PigWebResponseExceptionTranslator implements WebResponseExceptionTr
private ThrowableAnalyzer throwableAnalyzer = new DefaultThrowableAnalyzer();
@Override
public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
@SneakyThrows
public ResponseEntity<OAuth2Exception> translate(Exception e) {
// Try to extract a SpringSecurityException from the stacktrace
Throwable[] causeChain = throwableAnalyzer.determineCauseChain(e);

View File

@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.util.R;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
@ -28,7 +29,6 @@ import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
@ -44,8 +44,9 @@ public class ResourceAuthExceptionEntryPoint implements AuthenticationEntryPoint
private final ObjectMapper objectMapper;
@Override
@SneakyThrows
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException {
AuthenticationException authException) {
response.setCharacterEncoding(CommonConstants.UTF8);
response.setContentType(CommonConstants.CONTENT_TYPE);
R<String> result = new R<>();

View File

@ -17,6 +17,7 @@
package com.pig4cloud.pig.common.security.service;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import lombok.SneakyThrows;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.provider.ClientDetails;
@ -41,11 +42,11 @@ public class PigClientDetailsService extends JdbcClientDetailsService {
*
* @param clientId
* @return
* @throws InvalidClientException
*/
@Override
@SneakyThrows
@Cacheable(value = SecurityConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null")
public ClientDetails loadClientByClientId(String clientId) throws InvalidClientException {
public ClientDetails loadClientByClientId(String clientId) {
return super.loadClientByClientId(clientId);
}
}

View File

@ -25,6 +25,7 @@ import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.core.util.R;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
@ -57,10 +58,10 @@ public class PigUserDetailsServiceImpl implements UserDetailsService {
*
* @param username 用户名
* @return
* @throws UsernameNotFoundException
*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
@SneakyThrows
public UserDetails loadUserByUsername(String username) {
Cache cache = cacheManager.getCache("user_details");
if (cache != null && cache.get(username) != null) {
return (PigUser) cache.get(username).get();

View File

@ -18,6 +18,7 @@ package com.pig4cloud.pig.common.security.util;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
@ -39,11 +40,9 @@ public class AuthUtils {
* 从header 请求中的clientId/clientsecect
*
* @param header header中的参数
* @throws RuntimeException if the Basic header is not present or is not valid
* Base64
*/
public String[] extractAndDecodeHeader(String header)
throws IOException {
@SneakyThrows
public String[] extractAndDecodeHeader(String header) {
byte[] base64Token = header.substring(6).getBytes("UTF-8");
byte[] decoded;
@ -69,10 +68,9 @@ public class AuthUtils {
*
* @param request
* @return
* @throws IOException
*/
public String[] extractAndDecodeHeader(HttpServletRequest request)
throws IOException {
@SneakyThrows
public String[] extractAndDecodeHeader(HttpServletRequest request) {
String header = request.getHeader(HttpHeaders.AUTHORIZATION);
if (header == null || !header.startsWith(BASIC_)) {

View File

@ -21,6 +21,7 @@ package com.pig4cloud.pig.eureka.security;
* @date 2019/2/1
*/
import lombok.SneakyThrows;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@ -28,7 +29,8 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
@SneakyThrows
protected void configure(HttpSecurity http) {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/actuator/**").permitAll()

View File

@ -23,6 +23,7 @@ import com.pig4cloud.pig.codegen.entity.GenConfig;
import com.pig4cloud.pig.codegen.service.SysGeneratorService;
import com.pig4cloud.pig.common.core.util.R;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
@ -55,7 +56,8 @@ public class SysGeneratorController {
* 生成代码
*/
@PostMapping("/code")
public void code(@RequestBody GenConfig genConfig, HttpServletResponse response) throws IOException {
@SneakyThrows
public void code(@RequestBody GenConfig genConfig, HttpServletResponse response) {
byte[] data = sysGeneratorService.generatorCode(genConfig);
response.reset();

View File

@ -17,6 +17,7 @@
package com.pig4cloud.pig.monitor.config;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@ -37,7 +38,8 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
}
@Override
protected void configure(HttpSecurity http) throws Exception {
@SneakyThrows
protected void configure(HttpSecurity http) {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");