1

我想主要记录所有 http 请求,减去执行器请求,spring.log因为我想通过执行器 API使它们在Spring Boot Admin中可见。我知道我可以配置 Spring Boot 的嵌入式 Tomcat 来记录 http 请求,但这些不会在执行器 API 或 Spring Boot Admin 中显示。我也意识到我可以使用默认/trace配置,但我想要的不仅仅是最后 100 个请求。

目前,我的策略是使用 aHandlerInterceptorAdapter来记录请求。这里的问题是,如果一个请求没有经过身份验证,它就永远不会到达拦截器。

拦截器:

@Component
class HttpRequestLoggingInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(HttpRequestLoggingInterceptor.class)

    @Override
    void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.info("HTTP Request - ${request.localAddr} - \"${request.method} ${request.servletPath}\" ${response.status}")
    }

}

AuthorizationServerConfigurerAdapter:

@Configuration
@EnableAuthorizationServer
class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private HttpRequestLoggingInterceptor interceptor

    @Override
    void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.addInterceptor(interceptor)
    }

}

WebMvcConfigurerAdapter:

@Configuration
@Order
class ApplicationConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private HttpRequestLoggingInterceptor interceptor

    @Override
    void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor).excludePathPatterns("/actuator/**")
    }
}

谢谢!

4

0 回答 0