【问题标题】:Configure actuator endpoints security配置执行器端点安全性
【发布时间】:2014-04-19 19:07:43
【问题描述】:

Spring Boot Actuator Endpoints 默认受基本 http 安全保护。

这可以更改为使用 Spring Security 吗? 我已成功设置 Spring Security 并使用它来保护我的其他页面。

我尝试了security.basic.enabled: false 并在我的授权请求中添加了.antMatchers("/manage/**").hasRole("ADMIN")(请注意,我使用不同的 url 作为端点的根)但这没有帮助。 我不断得到一个基本的 http auth 日志,它没有在 AuthenticationManager 中配置用户。

有什么想法吗?

编辑 - 提供更多细节 -

我的 Application.java 看起来像:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends WebMvcConfigurerAdapter {


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/app").setViewName("app/index");
        registry.addViewController("/app/login").setViewName("app/login");
    }

    @Bean
    public ApplicationSecurity applicationSecurity() {
        return new ApplicationSecurity();
    }

    @Order(Ordered.LOWEST_PRECEDENCE - 8)
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            // @formatter:off
            auth.inMemoryAuthentication()
                .withUser("test1")
                    .password("test1pw")
                    .roles("USER", "ADMIN")
                    .and()
                .withUser("test2")
                    .password("test2pw")
                    .roles("USER");
            // @formatter:on
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // @formatter:off
            http
                .csrf()
                    .disable()
                .authorizeRequests()
                    .antMatchers("/app/login").permitAll()
                    .antMatchers("/app/**").hasRole("USER")
                    .antMatchers("/manage/**").hasRole("ADMIN")
                    .and()
                .formLogin()
                    .loginPage("/app/login")
                    .failureUrl("/app/login?error")
                    .defaultSuccessUrl("/app")
                    .permitAll()
                    .and()
                .logout()
                    .logoutUrl("/app/logout")
                    .logoutSuccessUrl("/app/login?logout");
            // @formatter:on
        }

        @Override
        public void configure(WebSecurity web) throws Exception {
            // @formatter:off
            web
                .ignoring()
                    .antMatchers("/assets/**");
            // @formatter:on
        }
    }
}

在我的application.yml我也有:

management:
  context-path: /management

请注意,设置与您提到的指南相同。

现在我期望 - 或想要配置 - 是 /manage 端点(如运行状况、映射等)将受到来自自定义 AuthenticationManager 的用户的保护。

我还尝试添加management.security.enabled=false,这确实会关闭身份验证,例如/管理/映射。 然而,有问题的是我明确告诉 Spring Security 保护这些 url 的通过:

@Override
protected void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
        .authorizeRequests()
            .antMatchers("/app/login").permitAll()
            .antMatchers("/app/**").hasRole("USER")
            .antMatchers("/manage/**").hasRole("ADMIN")

但这不起作用。注意其他授权匹配器确实有效。 我想知道在时间/顺序内是否有事情要做。我从示例中复制了@Order(Ordered.LOWEST_PRECEDENCE - 8),但我不知道为什么 - 使用了 8。

为了更深入地研究,我自己也运行了示例 (https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-web-method-security),我在示例应用程序中看到了相同的行为。 管理安全似乎完全独立于示例内存身份验证中配置的useradmin 用户。

【问题讨论】:

    标签: spring-security spring-boot


    【解决方案1】:

    这可以改成使用 Spring Security 吗?

    Spring Security(您认为我们还会使用什么?)。如果您只想保留默认的安全规则并自定义 AuthenticationManager,那么如果您按照 Spring Security 团队的建议使用 AuthenticationManagerBuilder,它应该可以正常工作。 secure method sample 具有您正在寻找的行为,因此您可以从那里复制配置模式。关键是,如果要替换 Boot 默认身份验证策略,是在 GlobalAuthenticationConfigurerAdapter like in the sample 中配置 AuthenticationManager

    您可以使用management.security.enabled=false 关闭管理安全性(假设 Spring Security 在类路径上)。 user guide 中提到了它,但请随时提出澄清。

    【讨论】:

    • 我已经用更多细节更新了这个问题。顺便说一句,我在您提到的示例中看到了相同的行为(我的代码基于该示例)。
    • 谢谢。我暂时保留我的答案,因为这是预期的行为,但看起来情况并非如此。我在示例中添加了一些测试用例,看看它们是否可以帮助我们解释(github issue here)。
    • 我用指向示例中新的正确代码的指针更新了答案。
    • 抱歉,过了一段时间才再次回复。使用 GlobalAuthenticationConfigurerAdapter 更新的安全示例确实有效。然而,发生了一件奇怪的事情:当我通过 management.security.enabled=false 禁用管理安全性时,而是设置了一个自定义的 spring security antMatcher [例如.antMatchers("/management/**").hasRole("ADMIN")] 那么像 /management/mappings 这样的管理 URL 是不安全的。似乎 management.security.enabled=false 完全推翻了其他 spring 安全设置。
    • 是的,当我使用 @Order(Ordered.LOWEST_PRECEDENCE - 11) 而不是 -8 时,它可以工作。选择一个值感觉有点神奇,因为使用 -10 甚至停止启动,因为 @Order 不是 unqiue
    【解决方案2】:

    我会说,如果您有非常具体的情况,有时排除 Spring Boot 组件的自动配置并从头开始配置会更容易。在这种情况下,您可以使用:

    @EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})
    

    如果您想保留其余的引导安全配置,则只需 ManagementWebSecurityConfiguration.java。然后你可以使用类似的东西:

    @Configuration
    @EnableGlobalMethodSecurity(securedEnabled = true)
    public class SecurityConfiguration {
    
    @Configuration
    @Order(Ordered.HIGHEST_PRECEDENCE)
    protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {
    
        private final SecurityProperties securityProperties;
    
        @Autowired
        AuthenticationSecurity(SecurityProperties securityProperties) {
            this.securityProperties = securityProperties;
        }
    
        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            // configuration
        }
    }
    
    @Configuration
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
    
        private SecurityProperties security;
    
        @Autowired
        protected ApplicationSecurity(SecurityProperties security) {
            this.security = security;
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // you configuration
        }
    
    
    }
    
    }
    

    如您所见,我在这种情况下重用了 SecurityProperties 以避免自己创建。

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 1970-01-01
      • 2016-08-26
      • 2019-12-28
      • 2018-09-11
      • 1970-01-01
      • 2016-02-05
      • 2020-01-03
      • 1970-01-01
      相关资源
      最近更新 更多