项目场景:

项目上线后,被测试出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

@Component
@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则不会出现这个问题。 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐