Spring Boot 无法拦截执行器访问 [英] Spring Boot can't intercept actuator access

查看:38
本文介绍了Spring Boot 无法拦截执行器访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SpringBoot 2.1.6版本无法拦截访问执行器请求现在我有了一个全局拦截器

In SpringBoot version 2.1.6 unable to intercept access actuator request Now I have a global interceptor

@Component
public class ServiceFilter implements HandlerInterceptor {
//log4j
static final Logger logger = LogManager.getLogger(ServiceFilter.class);
private final RateLimiter limiter = RateLimiter.create(Runtime.getRuntime().availableProcessors() * 2 + 1);
private final ThreadLocal<ExecuteRecordDto> executeRecord = new ThreadLocal<>();

public ServiceFilter() {

}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    ExecuteRecordDto recordDto =  ExecuteRecordDto.bulider(request);
    executeRecord.set(recordDto);
    if (!limiter.tryAcquire()) {
        logger.warn("rate limiter ; json logger :  {}",CommonUtil.toJSONString(recordDto));
        response.getWriter().print(CommonUtil.toJSONString(ResultStatus.status(407, "rate limiter")));
        return false;
    }

    if (ObjectUtils.isEmpty(request.getHeader("Authorization"))) {
        logger.warn("illegal request, json logger : {} ",CommonUtil.toJSONString(recordDto));
        response.getWriter().print(CommonUtil.toJSONString(ResultStatus.status(403, "Permission denied")));
        return false;
    }
    switch (TokenHandle.checkToken(request.getHeader("Authorization"))) {
        //正常放行token
        case 0:
            response.getWriter().print(CommonUtil.toJSONString(ResultStatus.status(407, "rate limiter")));
            return true;
        //token 过期
        case 1:
            response.getWriter().println(CommonUtil.toJSONString(ResultStatus.status(408, "Token expire")));
            break;
        //非法token
        case 2:
            logger.warn("illegal token, json logger : {} ",CommonUtil.toJSONString(recordDto));
            response.getWriter().print(CommonUtil.toJSONString(ResultStatus.status(409, "Illegal token ")));
            break;
        default:
            throw new RuntimeException("server runtime exception");
    }
    return true;
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    ExecuteRecordDto recordDto = executeRecord.get();
    logger.info("json logger : {}",CommonUtil.toJSONString(recordDto));
    executeRecord.remove();
}

}

让它发挥作用

@Configuration
public class ConfigFilter implements WebMvcConfigurer {
private final ServiceFilter filter;
@Autowired
public ConfigFilter(ServiceFilter filter){
    this.filter = filter;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(filter).addPathPatterns("/**");
}
}

我申请了自己的api,得到我想要的效果

I requested my own api, get the effect I want

SpringBoot 如何拦截对actuator的访问

How can SpringBoot intercept a visit to actuator

推荐答案

Actuator 正在使用不同的 HandlerMapping(请参阅:org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping).

Actuator is using a different HandlerMapping (see: org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping).

由于顺序(-100 vs 0),将在您配置的 RequestHandlerMapping 上选择此 Handlermapping.您可以在 DispatcherServlet 中准确地看到该方法 HandlerExecutionChain getHandler(HttpServletRequest request).

This Handlermapping will be chosen over your configured RequestHandlerMapping because of the order (-100 vs 0). You can see this in the DispatcherServlet precisely the method HandlerExecutionChain getHandler(HttpServletRequest request).

在我们的项目中,我们使用 spring security 配置了对执行器端点的访问,所以我不知道是否有任何推荐的方法来做到这一点,但是:

In our projects we configure the access to the actuator endpoints with spring security so i'm not aware if there are any recommended ways to do it but:

处理程序是按顺序选择的,所以这是一个需要考虑的事情,您也可以尝试操作执行器WebMvcEndpointHandlerMapping.

The handler are chose by order so this a thing to consider, you can also try to manipulate the actuator WebMvcEndpointHandlerMapping.

就像我说的,我不确定正确的解决方案,但我希望它为您指明正确的方向以找到合适的解决方案.

Like i said i'm not sure about the right solution, but i hope it points you in the right direction to find a proper solution.

问候,WiPu

这篇关于Spring Boot 无法拦截执行器访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆