配置执行器端点安全

2024-02-28

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

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

I tried security.basic.enabled: false并添加.antMatchers("/manage/**").hasRole("ADMIN")在我的授权请求中(请注意,我使用不同的 url 作为端点的根),但这没有帮助。 我不断收到基本的 http 身份验证日志,该日志没有在 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
        }
    }
}

In my 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 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-web-method-security)我和我在示例应用程序中看到了相同的行为。 管理安全似乎完全独立于user and admin在示例的内存身份验证中配置的用户。


可以更改为使用 Spring Security 吗?

It isSpring Security(您认为我们还会使用什么?)。如果您只想保留默认的安全规则并自定义AuthenticationManager如果你使用它应该就可以工作AuthenticationManagerBuilder正如 Spring Security 团队所推荐的。这安全方法示例 https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-method-security具有您正在寻找的行为,因此您可以从那里复制配置模式。如果要替换 Boot 默认身份验证策略,关键是要获取AuthenticationManager配置在一个GlobalAuthenticationConfigurerAdapter 就像样本中一样 https://github.com/spring-projects/spring-boot/blob/0aa93036fa34eee08efcff90af3711a69577ef05/spring-boot-samples/spring-boot-sample-web-method-security/src/main/java/sample/ui/method/SampleMethodSecurityApplication.java#L77.

您可以使用以下命令关闭管理安全性management.security.enabled=false(假设 Spring Security 位于类路径上)。中提到了用户指南 https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/production-ready-features.html#production-ready-endpoints-security,但请随时提出澄清。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

配置执行器端点安全 的相关文章

随机推荐