【问题标题】:Spring Boot Actuator - Cannot disable /info endpointSpring Boot Actuator - 无法禁用 /info 端点
【发布时间】:2016-03-27 18:44:25
【问题描述】:

我尝试在application.yml 配置文件中为生产环境禁用所有执行器端点:

endpoints.enabled: false

它适用于除 /info 之外的所有端点。 如何关闭给定环境的所有端点?

更新:

我正在从事的项目也是 Eureka 的客户。 在 Status Page and Health Indicator (http://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html) 部分的 Spring Cloud Netflix 文档中,它说“Eureka 实例分别默认为“/info”和“/health””。

是否有任何解决方案可以禁用这些端点?

我可以使用endpoints.enabled: false 禁用 /health 端点,但不能禁用 /info 端点。

【问题讨论】:

  • 保护端点可能是您唯一的选择。在生产中禁用似乎是一个奇怪的选择,因为您正在关闭使用执行器的能力。
  • 我能够使用额外的网络安全配置(在执行器的默认安全配置旁边工作)来保护 /info 端点。我不喜欢的是除了 /info 之外的所有执行器端点都可以通过执行器配置来保护,即management.security.enabled: true。但为了保护 /info 端点,我只需要为此端点创建单独的 Web 安全配置。似乎我在代码中做了一些修改。

标签: java spring-boot spring-boot-actuator


【解决方案1】:

最后我设法解决了我的问题。我在执行器中仅启用了 /info 和 /health 端点。为了只允许具有 ADMIN 角色的用户访问 /info 端点,我需要混合执行器管理安全性和弹簧安全性配置。

所以我的 application.yml 看起来像这样:

endpoints.enabled: false

endpoints:
    info.enabled: true
    health.enabled: true

management.security.role: ADMIN

像这样的 spring 安全配置(我需要更改 ManagementSecurityConfig 的顺序以获得更高的优先级):

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration {


    @Configuration
    protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {

        @Autowired
        private AuthenticationProvider authenticationProvider;

        public AuthenticationSecurity() {
            super();
        }

        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
             auth.inMemoryAuthentication().withUser("admin").password("secret").roles("ADMIN");
        }
    }

    @Configuration
    @Order(Ordered.HIGHEST_PRECEDENCE + 2)
    public static class ManagementSecurityConfig extends WebSecurityConfigurerAdapter {


        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                    .requestMatchers()
                    .antMatchers("/info/**")
                    .and()
                    .authorizeRequests()
                    .anyRequest().hasRole("ADMIN")
                    .and()
                    .httpBasic();
        }
    }

    @Configuration
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

        protected void configure(HttpSecurity http) throws Exception {
            // API security configuration
        }

    }
}

【讨论】:

    【解决方案2】:

    您的示例配置在我看来很可疑。我猜你的意思是

    endpoints:
      enabled: true
    

    无论如何,我只是尝试将它添加到一个普通的 Spring Boot 应用程序中(使用 1.3.1 并且所有端点都被禁用(如预期的那样)。

    【讨论】:

    • 我刚刚更新了我的问题。问题是项目中也使用了 Eureka。无论如何,我需要禁用端点,所以我需要将端点设置为 false,即endpoints.enabled: false。问题是它适用于 /health 端点,但不适用于 /info。
    猜你喜欢
    • 2021-08-25
    • 2016-06-01
    • 2020-09-15
    • 2019-01-08
    • 2020-02-07
    • 2016-06-19
    • 1970-01-01
    • 2016-09-07
    • 2021-04-24
    相关资源
    最近更新 更多