spring boot actuator 禁用后,/actuator仍可正常访问

spring boot actuator 禁用后,/actuator仍可正常访问

项目场景:

项目上线后,被测试出actuator没有关闭,关闭后,仍可正常访问/actuator端点,只是类似/actuator/env这样的无法访问,现在就想把/actuator端点也给禁用了。


问题描述

spring boot 2.x关闭actuator配置,关闭后,仍可正常访问/actuator端点

management:
  endpoints:
    enabled-by-default: false

原因分析:

说明spring boot 2.x无法通过配置的方式禁用/actuator端点


解决方案1-nginx配置:

大部分项目都用到nginx,则直接在nginx中配置禁用该端点即可。deny all 和 return 403是一个效果。

        location /actuator {
            deny all;
        }
       location /server-a/actuator {
            return 403;
        }

解决方案2-网关:

如果使用了网关服务,可以在网关中创建一个过滤器继承 OncePerRequestFilter

@***ponent
@Slf4j
public class myFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                    FilterChain filterChain) throws ServletException, IOException {
 

        StringBuffer requestURL = request.getRequestURL();
        if(requestURL.toString().contains("/actuator")){
            response.getWriter().write("error");
            return;
        }
        filterChain.doFilter(request, response);

    }
}

解决方案3-spring security

如果使用了spring security则不会出现这个问题。 

转载请说明出处内容投诉
CSS教程_站长资源网 » spring boot actuator 禁用后,/actuator仍可正常访问

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买