gpt4 book ai didi

java - Spring Boot 无法拦截执行器访问

转载 作者:行者123 更新时间:2023-12-02 09:40:27 24 4
gpt4 key购买 nike

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

@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,得到了我想要的效果 1

SpringBoot如何拦截对执行器的访问 2

最佳答案

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

由于顺序(-100 与 0),将选择此 Handlermapping 而不是您配置的 RequestHandlerMapping。您可以在 DispatcherServlet 中的 HandlerExecutionChain getHandler(HttpServletRequest request) 方法中看到这一点。

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

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

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

问候,WiPu

关于java - Spring Boot 无法拦截执行器访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57125304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com