如何拦截和记录命中 TokenEndpoint 时发生的错误?

How can I intercept and log errors that occur when hitting the TokenEndpoint?

鉴于在 https://github.com/spring-projects/spring-security-oauth/issues/1271 and https://github.com/spring-projects/spring-security-oauth/issues/1290 中所做的日志记录更改,我认为令牌端点中存在的日志记录可能很难取悦所有人。例如,我想捕获任何属于简单 @ExceptionHandler(Exception.class) 的内容,作为带有堆栈跟踪日志的错误语句。

拦截错误端点中发生的异常的最佳方法是什么,以便可以应用自定义日志记录?

我们可以使用 WebMvcConfigurationSupport 中定义的覆盖 HandlerExceptionResolverComposite 异常处理程序。这会将所有异常解析器组合成一个异常解析器。然后我们可以定义您自己的异常解析器。

我们可以使用的异常解析器之一是 ExceptionHandlerExceptionResolver,这将通过使用 类 和 @ControllerAdvice 注释启用基于 AOP 的异常处理。

在我们的自定义控制器建议中,我们可以为不同的异常使用处理程序:

@ExceptionHandler({OAuth2Exception.class})
public ResponseEntity<Object> handleOAuth2Exception(final OAuth2Exception exception, final WebRequest request) {
    LOGGER.debug("OAuth failed on request processing", exception);

我们最终解决这个问题的方法是使用 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);
    }

}