鉴于在https://github.com/spring-projects/spring-security-oauth/issues/1271和https://github.com/spring-projects/spring-security-oauth/issues/1290中完成的日志记录更改,我认为使用令牌端点中存在的日志记录可能很难取悦每个人。例如,我想捕获任何属于简单@ExceptionHandler(Exception.class)
的任何内容,使其成为带有堆栈跟踪的日志的错误语句。
拦截错误端点中发生的异常的最佳方法是什么,以便可以应用自定义日志记录?
答案 0 :(得分:0)
我们可以使用HandlerExceptionResolverComposite
中定义的覆盖WebMvcConfigurationSupport
异常处理程序。这会将所有异常解析器合成为一个异常解析器。然后我们可以定义您自己的异常解析器。
我们可以使用的一个异常解析器是ExceptionHandlerExceptionResolver
,这将通过涉及具有@ControllerAdvice
注释的类来启用基于AOP的异常处理。
在我们的自定义控制器建议中,我们可以将处理程序用于不同的异常:
@ExceptionHandler({OAuth2Exception.class})
public ResponseEntity<Object> handleOAuth2Exception(final OAuth2Exception exception, final WebRequest request) {
LOGGER.debug("OAuth failed on request processing", exception);
答案 1 :(得分:0)
我们最终解决这个问题的方法是使用spring-aop
。我们只是截获了正确的位置并在其中记录错误消息:
@Slf4j
@Aspect
@Component
public class OAuthErrorLoggingAspect {
private static final String ERROR_MESSAGE = "Error during token generation: ";
@Before("execution("
+ "public * "
+ "org.springframework.security.oauth2.provider.endpoint"
+ ".TokenEndpoint.handleException(Exception)) && args(ex))")
public void handleExceptionLogging(Exception ex) {
if (ex instanceof ClientAbortException) {
log.debug(ERROR_MESSAGE, ex);
} else {
log.error(ERROR_MESSAGE, ex);
}
}
@Before("execution("
+ "public * "
+ "org.springframework.security.oauth2.provider.endpoint"
+ ".TokenEndpoint.handleHttpRequestMethodNotSupportedException("
+ "org.springframework.web.HttpRequestMethodNotSupportedException)) && args(ex))")
public void handleHttpRequestMethodNotSupportedLogging(HttpRequestMethodNotSupportedException ex) {
log.debug(ERROR_MESSAGE, ex);
}
@Before("execution("
+ "public * "
+ "org.springframework.security.oauth2.provider.endpoint"
+ ".TokenEndpoint.handleClientRegistrationException("
+ "Exception)) && args(ex))")
public void handleClientRegistrationErrorLogging(Exception ex) {
log.debug(ERROR_MESSAGE, ex);
}
@Before("execution("
+ "public * "
+ "org.springframework.security.oauth2.provider.endpoint"
+ ".TokenEndpoint.handleException("
+ "org.springframework.security.oauth2.common.exceptions.OAuth2Exception)) && args(ex))")
public void handleOAuth2ExceptionLogging(OAuth2Exception ex) {
log.debug(ERROR_MESSAGE, ex);
}
}