From dae962b979384a10770892770753b825e6165b52 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=92=9F=E4=BF=8A?=
Date: Tue, 26 Jan 2021 17:32:32 +0800
Subject: [PATCH] perf($Exception): refine global exception handling
---
exercise-mis/pom.xml | 6 -
muscle-mis/pom.xml | 6 -
.../MafReactiveAutoConfiguration.java | 14 +-
service-registry/pom.xml | 6 -
spring-boot-admin/pom.xml | 6 -
.../MafAutoConfiguration.java | 65 ++++--
.../aspect/ExceptionControllerAdvice.java | 207 ++++++++++--------
.../WebSecurityConfiguration.java | 10 +-
.../redis/RedisCachingConfiguration.java | 2 +-
.../redis/RedisConfiguration.java | 6 +-
10 files changed, 169 insertions(+), 159 deletions(-)
rename {auth-center/src/main/java/com/jmsoftware/maf/authcenter/security => spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter}/configuration/WebSecurityConfiguration.java (78%)
diff --git a/exercise-mis/pom.xml b/exercise-mis/pom.xml
index da62128b..36ed3aae 100644
--- a/exercise-mis/pom.xml
+++ b/exercise-mis/pom.xml
@@ -76,12 +76,6 @@
com.jmsoftware.maf
spring-boot-starter
-
-
- spring-boot-starter-security
- org.springframework.boot
-
-
diff --git a/muscle-mis/pom.xml b/muscle-mis/pom.xml
index 135eea3d..b702498a 100644
--- a/muscle-mis/pom.xml
+++ b/muscle-mis/pom.xml
@@ -76,12 +76,6 @@
com.jmsoftware.maf
spring-boot-starter
-
-
- spring-boot-starter-security
- org.springframework.boot
-
-
diff --git a/reactive-spring-boot-starter/src/main/java/com/jmsoftware/maf/reactivespringbootstarter/configuration/MafReactiveAutoConfiguration.java b/reactive-spring-boot-starter/src/main/java/com/jmsoftware/maf/reactivespringbootstarter/configuration/MafReactiveAutoConfiguration.java
index ae863529..086ff960 100644
--- a/reactive-spring-boot-starter/src/main/java/com/jmsoftware/maf/reactivespringbootstarter/configuration/MafReactiveAutoConfiguration.java
+++ b/reactive-spring-boot-starter/src/main/java/com/jmsoftware/maf/reactivespringbootstarter/configuration/MafReactiveAutoConfiguration.java
@@ -34,45 +34,45 @@ public void postConstruct() {
@Bean
public WebFluxConfiguration webFluxConfiguration() {
- log.warn("Initial bean: {}", WebFluxConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", WebFluxConfiguration.class.getSimpleName());
return new WebFluxConfiguration();
}
@Bean
@ConditionalOnMissingBean
public MafConfiguration mafConfiguration() {
- log.warn("Initial bean: {}", MafConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", MafConfiguration.class.getSimpleName());
return new MafConfiguration();
}
@Bean
@ConditionalOnMissingBean
public AccessLogFilter requestFilter(MafConfiguration mafConfiguration) {
- log.warn("Initial bean: {}", AccessLogFilter.class.getSimpleName());
+ log.warn("Initial bean: '{}'", AccessLogFilter.class.getSimpleName());
return new AccessLogFilter(mafConfiguration);
}
@Bean
public MafProjectProperty mafProjectProperty() {
- log.warn("Initial bean: {}", MafProjectProperty.class.getSimpleName());
+ log.warn("Initial bean: '{}'", MafProjectProperty.class.getSimpleName());
return new MafProjectProperty();
}
@Bean
public IpHelper ipHelper(MafProjectProperty mafProjectProperty) {
- log.warn("Initial bean: {}", IpHelper.class.getSimpleName());
+ log.warn("Initial bean: '{}'", IpHelper.class.getSimpleName());
return new IpHelper(mafProjectProperty);
}
@Bean
public CommonService commonService(MafProjectProperty mafProjectProperty) {
- log.warn("Initial bean: {}", CommonServiceImpl.class.getSimpleName());
+ log.warn("Initial bean: '{}'", CommonServiceImpl.class.getSimpleName());
return new CommonServiceImpl(mafProjectProperty);
}
@Bean
public CommonController commonController(CommonService commonService) {
- log.warn("Initial bean: {}", CommonController.class.getSimpleName());
+ log.warn("Initial bean: '{}'", CommonController.class.getSimpleName());
return new CommonController(commonService);
}
}
diff --git a/service-registry/pom.xml b/service-registry/pom.xml
index e82d8d98..388d1c14 100644
--- a/service-registry/pom.xml
+++ b/service-registry/pom.xml
@@ -76,12 +76,6 @@
com.jmsoftware.maf
spring-boot-starter
-
-
- org.springframework.boot
- spring-boot-starter-security
-
-
diff --git a/spring-boot-admin/pom.xml b/spring-boot-admin/pom.xml
index 8047dda7..4978c87d 100644
--- a/spring-boot-admin/pom.xml
+++ b/spring-boot-admin/pom.xml
@@ -76,12 +76,6 @@
com.jmsoftware.maf
spring-boot-starter
-
-
- org.springframework.boot
- spring-boot-starter-security
-
-
diff --git a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/MafAutoConfiguration.java b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/MafAutoConfiguration.java
index a4802518..ca51fd3f 100644
--- a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/MafAutoConfiguration.java
+++ b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/MafAutoConfiguration.java
@@ -48,6 +48,8 @@
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
import org.springframework.messaging.MessageHandler;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import springfox.documentation.builders.PathSelectors;
@@ -80,7 +82,7 @@ public void postConstruct() {
@Bean
@ConditionalOnMissingBean
public MafConfiguration mafConfiguration() {
- log.warn("Initial bean: {}", MafConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", MafConfiguration.class.getSimpleName());
val mafConfiguration = new MafConfiguration();
log.warn("{}", mafConfiguration);
return mafConfiguration;
@@ -88,14 +90,14 @@ public MafConfiguration mafConfiguration() {
@Bean
public MafProjectProperty mafProjectProperty() {
- log.warn("Initial bean: {}", MafProjectProperty.class.getSimpleName());
+ log.warn("Initial bean: '{}'", MafProjectProperty.class.getSimpleName());
return new MafProjectProperty();
}
@Bean
@ConditionalOnMissingBean
public ExceptionControllerAdvice exceptionControllerAdvice() {
- log.warn("Initial bean: {}", ExceptionControllerAdvice.class.getSimpleName());
+ log.warn("Initial bean: '{}'", ExceptionControllerAdvice.class.getSimpleName());
return new ExceptionControllerAdvice();
}
@@ -103,35 +105,35 @@ public ExceptionControllerAdvice exceptionControllerAdvice() {
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "maf.configuration.web-request-log-disabled", havingValue = "false")
public WebRequestLogAspect webRequestLogAspect() {
- log.warn("Initial bean: {}", WebRequestLogAspect.class.getSimpleName());
+ log.warn("Initial bean: '{}'", WebRequestLogAspect.class.getSimpleName());
return new WebRequestLogAspect();
}
@Bean
@ConditionalOnMissingBean
public RedirectController redirectController() {
- log.warn("Initial bean: {}", RedirectController.class.getSimpleName());
+ log.warn("Initial bean: '{}'", RedirectController.class.getSimpleName());
return new RedirectController();
}
@Bean
@ConditionalOnMissingBean
public AccessLogFilter requestFilter(MafConfiguration mafConfiguration) {
- log.warn("Initial bean: {}", AccessLogFilter.class.getSimpleName());
+ log.warn("Initial bean: '{}'", AccessLogFilter.class.getSimpleName());
return new AccessLogFilter(mafConfiguration);
}
@Bean
@ConditionalOnMissingBean
public IpHelper ipHelper(Environment environment) {
- log.warn("Initial bean: {}", IpHelper.class.getSimpleName());
+ log.warn("Initial bean: '{}'", IpHelper.class.getSimpleName());
return new IpHelper(environment);
}
@Bean
@ConditionalOnMissingBean
public WebMvcConfiguration webMvcConfiguration() {
- log.warn("Initial bean: {}", WebMvcConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", WebMvcConfiguration.class.getSimpleName());
return new WebMvcConfiguration();
}
@@ -140,34 +142,34 @@ public WebMvcConfiguration webMvcConfiguration() {
public GlobalErrorController globalErrorController(ErrorAttributes errorAttributes,
ServerProperties serverProperties,
List errorViewResolvers) {
- log.warn("Initial bean: {}", GlobalErrorController.class.getSimpleName());
+ log.warn("Initial bean: '{}'", GlobalErrorController.class.getSimpleName());
return new GlobalErrorController(errorAttributes, serverProperties, errorViewResolvers);
}
@Bean
public HttpApiScanHelper httpApiScanHelper(RequestMappingHandlerMapping requestMappingHandlerMapping) {
- log.warn("Initial bean: {}", HttpApiScanHelper.class.getSimpleName());
+ log.warn("Initial bean: '{}'", HttpApiScanHelper.class.getSimpleName());
return new HttpApiScanHelper(requestMappingHandlerMapping);
}
@Bean
public HttpApiResourceRemoteApiController httpApiResourceRemoteController(MafConfiguration mafConfiguration,
HttpApiScanHelper httpApiScanHelper) {
- log.warn("Initial bean: {}", HttpApiResourceRemoteApiController.class.getSimpleName());
+ log.warn("Initial bean: '{}'", HttpApiResourceRemoteApiController.class.getSimpleName());
return new HttpApiResourceRemoteApiController(mafConfiguration, httpApiScanHelper);
}
@Bean
@ConditionalOnProperty(value = "maf.configuration.swagger-disabled", havingValue = "false")
public Swagger2Configuration swagger2Configuration(MafProjectProperty mafProjectProperty) {
- log.warn("Initial bean: {}", Swagger2Configuration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", Swagger2Configuration.class.getSimpleName());
return new Swagger2Configuration(mafProjectProperty);
}
@Bean
@ConditionalOnProperty(value = "maf.configuration.swagger-disabled", havingValue = "false")
public Docket docket(Swagger2Configuration swagger2Configuration, MafProjectProperty mafProjectProperty) {
- log.warn("Initial bean: {}", Docket.class.getSimpleName());
+ log.warn("Initial bean: '{}'", Docket.class.getSimpleName());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(swagger2Configuration.apiInfo())
.select()
@@ -178,56 +180,56 @@ public Docket docket(Swagger2Configuration swagger2Configuration, MafProjectProp
@Bean
public CommonService commonService(MafProjectProperty mafProjectProperty) {
- log.warn("Initial bean: {}", CommonServiceImpl.class.getSimpleName());
+ log.warn("Initial bean: '{}'", CommonServiceImpl.class.getSimpleName());
return new CommonServiceImpl(mafProjectProperty);
}
@Bean
public CommonController commonController(CommonService commonService) {
- log.warn("Initial bean: {}", CommonController.class.getSimpleName());
+ log.warn("Initial bean: '{}'", CommonController.class.getSimpleName());
return new CommonController(commonService);
}
@Bean
public JwtConfiguration jwtConfiguration(MafProjectProperty mafProjectProperty) {
- log.warn("Initial bean: {}", JwtConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", JwtConfiguration.class.getSimpleName());
return new JwtConfiguration(mafProjectProperty);
}
@Bean
public DruidConfiguration druidConfiguration(MafConfiguration mafConfiguration) {
- log.warn("Initial bean: {}", DruidConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", DruidConfiguration.class.getSimpleName());
return new DruidConfiguration(mafConfiguration);
}
@Bean
public MyBatisPlusConfiguration myBatisPlusConfiguration() {
- log.warn("Initial bean: {}", MyBatisPlusConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", MyBatisPlusConfiguration.class.getSimpleName());
return new MyBatisPlusConfiguration();
}
@Bean
public RedisCachingConfiguration redisCachingConfiguration(RedisConnectionFactory redisConnectionFactory) {
- log.warn("Initial bean: {}", RedisCachingConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", RedisCachingConfiguration.class.getSimpleName());
return new RedisCachingConfiguration(redisConnectionFactory);
}
@Bean
public RedisConfiguration redisConfiguration(ObjectMapper objectMapper) {
- log.warn("Initial bean: {}", RedisConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", RedisConfiguration.class.getSimpleName());
return new RedisConfiguration(objectMapper);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
- log.warn("Initial bean: {}", RestTemplate.class.getSimpleName());
+ log.warn("Initial bean: '{}'", RestTemplate.class.getSimpleName());
return new RestTemplate();
}
@Bean
public SftpClientConfiguration sftpClientConfiguration() {
- log.warn("Initial bean: {}", SftpClientConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", SftpClientConfiguration.class.getSimpleName());
return new SftpClientConfiguration();
}
@@ -247,6 +249,7 @@ public SessionFactory sftpSessionFactory(SftpClientConfigur
// We return a caching session factory, so that we don't have to reconnect to SFTP server for each time
val cachingSessionFactory = new CachingSessionFactory<>(factory, sftpClientConfiguration.getSessionCacheSize());
cachingSessionFactory.setSessionWaitTimeout(sftpClientConfiguration.getSessionWaitTimeout());
+ log.warn("Initial bean: '{}'", cachingSessionFactory.getClass().getSimpleName());
return cachingSessionFactory;
}
@@ -264,6 +267,7 @@ public MessageHandler messageHandler(SessionFactory sftpSes
throw new IllegalArgumentException("File expected as payload.");
}
});
+ log.warn("Initial bean: '{}'", handler.getClass().getSimpleName());
return handler;
}
@@ -276,19 +280,32 @@ public SftpRemoteFileTemplate sftpRemoteFileTemplate(SessionFactoryException handler.
@@ -45,96 +43,123 @@ public class ExceptionControllerAdvice {
* @param exception any kinds of exception occurred in controller
* @return custom exception info
*/
- @ResponseBody
- @ExceptionHandler(value = Exception.class)
- @SuppressWarnings("AlibabaMethodTooLong")
- public ResponseBodyBean> handleException(HttpServletRequest request, HttpServletResponse response,
- Exception exception) {
- log.error("Exception occurred when [{}] requested access. Request URL: [{}] {}",
- RequestUtil.getRequestIpAndPort(request), request.getMethod(), request.getRequestURL());
-
+ @ResponseStatus(HttpStatus.NOT_FOUND)
+ @ExceptionHandler(value = NoHandlerFoundException.class)
+ public ResponseBodyBean> handleException(HttpServletRequest request, NoHandlerFoundException exception) {
+ requestLog(request);
// ATTENTION: Use only ResponseBodyBean.ofStatus() in handleException() method and
// DON'T throw any exceptions in this method
- if (exception instanceof NoHandlerFoundException) {
- log.error("NoHandlerFoundException: Request URL = {}, HTTP method = {}",
- ((NoHandlerFoundException) exception).getRequestURL(),
- ((NoHandlerFoundException) exception).getHttpMethod());
- response.setStatus(HttpStatus.NOT_FOUND.value());
- return ResponseBodyBean.ofStatus(HttpStatus.NOT_FOUND);
- } else if (exception instanceof HttpRequestMethodNotSupportedException) {
- log.error("Exception occurred when the request handler does not support a specific request method. " +
- "Current method is {}, Support HTTP method = {}",
- ((HttpRequestMethodNotSupportedException) exception).getMethod(),
- ((HttpRequestMethodNotSupportedException) exception).getSupportedHttpMethods());
- response.setStatus(HttpStatus.METHOD_NOT_ALLOWED.value());
- return ResponseBodyBean.ofStatus(HttpStatus.METHOD_NOT_ALLOWED);
- } else if (exception instanceof MethodArgumentNotValidException) {
- log.error("Exception occurred when validation on an argument annotated with fails. Exception message: {}",
- exception.getMessage());
- response.setStatus(HttpStatus.BAD_REQUEST.value());
- return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST.value(),
- getFieldErrorMessageFromException(
- (MethodArgumentNotValidException) exception),
- null);
- } else if (exception instanceof ConstraintViolationException) {
- log.error("Constraint violations exception occurred. Exception message: {}", exception.getMessage());
- response.setStatus(HttpStatus.BAD_REQUEST.value());
- return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST.value(),
- CollUtil.getFirst(
- ((ConstraintViolationException) exception).getConstraintViolations()).getMessage(),
- null);
- } else if (exception instanceof MethodArgumentTypeMismatchException) {
- log.error("MethodArgumentTypeMismatchException: Parameter name = {}, Exception message: {}",
- ((MethodArgumentTypeMismatchException) exception).getName(), exception.getMessage());
- response.setStatus(HttpStatus.BAD_REQUEST.value());
- return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST, removeLineSeparator(exception.getMessage()));
- } else if (exception instanceof HttpMessageNotReadableException) {
- log.error("HttpMessageNotReadableException: {}",
- ((HttpMessageNotReadableException) exception).getMessage());
- response.setStatus(HttpStatus.BAD_REQUEST.value());
- return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST,
- removeLineSeparator(
- ((HttpMessageNotReadableException) exception).getMessage()));
- } else if (exception instanceof BaseException) {
- log.error("BaseException: Status code: {}, message: {}, data: {}", ((BaseException) exception).getCode(),
- exception.getMessage(), ((BaseException) exception).getData());
- response.setStatus(((BaseException) exception).getCode());
- return ResponseBodyBean.ofStatus(((BaseException) exception).getCode(),
- removeLineSeparator(exception.getMessage()),
- ((BaseException) exception).getData());
- } else if (exception instanceof BindException) {
- log.error("Exception message: {} ", exception.getMessage());
- response.setStatus(HttpStatus.BAD_REQUEST.value());
- return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST, removeLineSeparator(exception.getMessage()));
- } else if (exception instanceof IllegalArgumentException) {
- log.error("Exception message: {} ", exception.getMessage());
- response.setStatus(HttpStatus.BAD_REQUEST.value());
- return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST.value(),
- removeLineSeparator(exception.getMessage()), null);
- } else if (exception instanceof BadCredentialsException) {
- // IMPORTANT: org.springframework.security.authentication.BadCredentialsException only exists in the project
- // that depends on org.springframework.boot.spring-boot-starter-security
- log.error("Exception message: {} ", exception.getMessage());
- response.setStatus(HttpStatus.FORBIDDEN.value());
- return ResponseBodyBean.ofStatus(HttpStatus.FORBIDDEN.value(), removeLineSeparator(exception.getMessage()),
- null);
- } else if (exception instanceof InternalAuthenticationServiceException) {
- log.error("An authentication request could not be processed due to a system problem that occurred " +
- "internally. Exception message: {} ", exception.getMessage());
- if (exception.getCause() instanceof BaseException) {
- val exceptionCause = (BaseException) exception.getCause();
- val code = exceptionCause.getCode();
- response.setStatus(code);
- return ResponseBodyBean.ofStatus(HttpStatus.valueOf(code), removeLineSeparator(exception.getMessage()));
- }
- response.setStatus(HttpStatus.FORBIDDEN.value());
- return ResponseBodyBean.ofStatus(HttpStatus.FORBIDDEN.value(), removeLineSeparator(exception.getMessage()),
- null);
+ log.error("NoHandlerFoundException: Request URL = {}, HTTP method = {}", exception.getRequestURL(),
+ exception.getHttpMethod());
+ return ResponseBodyBean.ofStatus(HttpStatus.NOT_FOUND);
+ }
+
+ @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
+ @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
+ public ResponseBodyBean> handleException(HttpServletRequest request,
+ HttpRequestMethodNotSupportedException exception) {
+ requestLog(request);
+ log.error("Exception occurred when the request handler does not support a specific request method. " +
+ "Current method is {}, Support HTTP method = {}", exception.getMethod(),
+ exception.getSupportedHttpMethods());
+ return ResponseBodyBean.ofStatus(HttpStatus.METHOD_NOT_ALLOWED);
+ }
+
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
+ @ExceptionHandler(value = MethodArgumentNotValidException.class)
+ public ResponseBodyBean> handleException(HttpServletRequest request, MethodArgumentNotValidException exception) {
+ requestLog(request);
+ log.error("Exception occurred when validation on an argument annotated with fails. Exception message: {}",
+ exception.getMessage());
+ return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST.value(), getFieldErrorMessageFromException(exception),
+ null);
+ }
+
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
+ @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
+ public ResponseBodyBean> handleException(HttpServletRequest request,
+ MethodArgumentTypeMismatchException exception) {
+ requestLog(request);
+ log.error("MethodArgumentTypeMismatchException: Parameter name = {}, Exception message: {}",
+ exception.getName(), exception.getMessage());
+ return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST, removeLineSeparator(exception.getMessage()));
+ }
+
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
+ @ExceptionHandler(value = HttpMessageNotReadableException.class)
+ public ResponseBodyBean> handleException(HttpServletRequest request, HttpMessageNotReadableException exception) {
+ requestLog(request);
+ log.error("HttpMessageNotReadableException: {}", exception.getMessage());
+ return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST, removeLineSeparator(exception.getMessage()));
+ }
+
+ @ExceptionHandler(value = BaseException.class)
+ public ResponseBodyBean> handleException(HttpServletRequest request, HttpServletResponse response,
+ BaseException exception) {
+ requestLog(request);
+ log.error("BaseException: Status code: {}, message: {}, data: {}", exception.getCode(),
+ exception.getMessage(), exception.getData());
+ response.setStatus(exception.getCode());
+ return ResponseBodyBean.ofStatus(exception.getCode(), removeLineSeparator(exception.getMessage()),
+ exception.getData());
+ }
+
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
+ @ExceptionHandler(value = BindException.class)
+ public ResponseBodyBean> handleException(HttpServletRequest request, BindException exception) {
+ requestLog(request);
+ log.error("Exception message: {} ", exception.getMessage());
+ return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST, removeLineSeparator(exception.getMessage()));
+ }
+
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
+ @ExceptionHandler(value = IllegalArgumentException.class)
+ public ResponseBodyBean> handleException(HttpServletRequest request, IllegalArgumentException exception) {
+ requestLog(request);
+ log.error("Exception message: {} ", exception.getMessage());
+ return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST.value(), removeLineSeparator(exception.getMessage()),
+ null);
+ }
+
+ @ResponseStatus(HttpStatus.FORBIDDEN)
+ @ExceptionHandler(value = BadCredentialsException.class)
+ public ResponseBodyBean> handleException(HttpServletRequest request, BadCredentialsException exception) {
+ requestLog(request);
+ // IMPORTANT: org.springframework.security.authentication.BadCredentialsException only exists in the project
+ // that depends on org.springframework.boot.spring-boot-starter-security
+ log.error("Exception message: {} ", exception.getMessage());
+ return ResponseBodyBean.ofStatus(HttpStatus.FORBIDDEN.value(), removeLineSeparator(exception.getMessage()),
+ null);
+ }
+
+ @ExceptionHandler(value = InternalAuthenticationServiceException.class)
+ public ResponseBodyBean> handleException(HttpServletRequest request, HttpServletResponse response,
+ InternalAuthenticationServiceException exception) {
+ requestLog(request);
+ log.error("An authentication request could not be processed due to a system problem that occurred " +
+ "internally. Exception message: {} ", exception.getMessage());
+ if (exception.getCause() instanceof BaseException) {
+ val exceptionCause = (BaseException) exception.getCause();
+ val code = exceptionCause.getCode();
+ response.setStatus(code);
+ return ResponseBodyBean.ofStatus(HttpStatus.valueOf(code), removeLineSeparator(exception.getMessage()));
}
- log.error("Internal server exception occurred! Exception message: {} ", exception.getMessage(), exception);
- response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
+ response.setStatus(HttpStatus.FORBIDDEN.value());
+ return ResponseBodyBean.ofStatus(HttpStatus.FORBIDDEN.value(), removeLineSeparator(exception.getMessage()),
+ null);
+ }
+
+ @ExceptionHandler(value = {Throwable.class})
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+ public ResponseBodyBean> handleError(Throwable ex) {
+ log.error("Internal server exception occurred! Exception message: {} ", ex.getMessage(), ex);
return ResponseBodyBean.ofStatus(HttpStatus.INTERNAL_SERVER_ERROR,
- "Exception message: " + removeLineSeparator(exception.getMessage()));
+ String.format("Exception message: %s", removeLineSeparator(ex.getMessage())));
+ }
+
+ private void requestLog(HttpServletRequest request) {
+ log.error("Exception occurred when [{}] requested access. Request URL: [{}] {}",
+ RequestUtil.getRequestIpAndPort(request), request.getMethod(), request.getRequestURL());
}
/**
diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/security/configuration/WebSecurityConfiguration.java b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/configuration/WebSecurityConfiguration.java
similarity index 78%
rename from auth-center/src/main/java/com/jmsoftware/maf/authcenter/security/configuration/WebSecurityConfiguration.java
rename to spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/configuration/WebSecurityConfiguration.java
index 5f079920..07a895e6 100644
--- a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/security/configuration/WebSecurityConfiguration.java
+++ b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/configuration/WebSecurityConfiguration.java
@@ -1,13 +1,11 @@
-package com.jmsoftware.maf.authcenter.security.configuration;
+package com.jmsoftware.maf.springbootstarter.configuration;
import lombok.extern.slf4j.Slf4j;
-import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
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;
-import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* WebSecurityConfiguration
@@ -21,12 +19,6 @@
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
- @Bean
- public BCryptPasswordEncoder encoder() {
- return new BCryptPasswordEncoder();
- }
-
- @Bean
@Override
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManagerBean();
diff --git a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/redis/RedisCachingConfiguration.java b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/redis/RedisCachingConfiguration.java
index 311df255..ce01055d 100644
--- a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/redis/RedisCachingConfiguration.java
+++ b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/redis/RedisCachingConfiguration.java
@@ -35,7 +35,7 @@ public class RedisCachingConfiguration extends CachingConfigurerSupport {
@PostConstruct
private void postConstruct() {
- log.warn("Initial bean: {}", RedisCachingConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", RedisCachingConfiguration.class.getSimpleName());
}
@Bean
diff --git a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/redis/RedisConfiguration.java b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/redis/RedisConfiguration.java
index ecb65933..8b3ca117 100644
--- a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/redis/RedisConfiguration.java
+++ b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/redis/RedisConfiguration.java
@@ -29,7 +29,7 @@ public class RedisConfiguration {
@PostConstruct
private void postConstruct() {
- log.warn("Initial bean: {}", RedisConfiguration.class.getSimpleName());
+ log.warn("Initial bean: '{}'", RedisConfiguration.class.getSimpleName());
}
/**
@@ -44,7 +44,7 @@ private void postConstruct() {
*/
@Bean
public RedisTemplate