【问题标题】:Configure security for Spring Boot 2.0 acuator framework为 Spring Boot 2.0 acuator 框架配置安全性
【发布时间】:2018-10-07 04:35:00
【问题描述】:

我想在我的 Spring Boot 2.0 应用程序中使用 Spring Actuator Framework。框架本身按预期工作,因此我能够达到例如我的/actuator/health 端点。那里出现了一个登录对话框。我想摆脱它并尝试以下方法:

@Configuration
@EnableWebFluxSecurity
public class SecurityConfiguration {

  @Bean
  public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {

    return http
               .authorizeExchange()
                   .matchers(EndpointRequest.to("prometheus")).permitAll()
                   .matchers(EndpointRequest.toAnyEndpoint()).authenticated()
                   .anyExchange().permitAll()
                   .and()
               .formLogin()
                  .and()
               .httpBasic()
                  .and()
               .build();
  }

但是,在应用程序启动期间,我收到以下错误:

无法实例化 [org.springframework.security.web.server.SecurityWebFilterChain]:工厂方法 'securityWebFilterChain' 抛出异常;嵌套异常是 java.lang.IllegalArgumentException: authenticationManager cannot be null

当然我尝试过搜索,但我总是只能得到描述不同场景或使用 Spring Boot 1.x 框架的安全配置的页面。有人可以帮我吗?

【问题讨论】:

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


    【解决方案1】:

    在我看来,最简单的方法应该是让您的 SecurityConfiguration 扩展 WebSecurityConfigurerAdapter 并覆盖 configure(WebSecurity web),如下所示:

    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring()
                    .antMatchers("/actuator/**");
        }
    
        // ...
    
    }
    

    另外,我不熟悉 @EnableWebFluxSecurity,但要使上述方法正常工作,您需要 @Configuration@EnableWebSecurity 注释。

    现在至于您的配置设置方式,它并不是最佳的。为了补充上述建议,您应该使用HttpSecurity 而不是当前的SecurityWebFilterChain 来配置您的安全性。

    【讨论】:

    • 您好,感谢您的建议。 为了补充上述建议,您应该使用 HttpSecurity 而不是使用当前的 SecurityWebFilterChain 配置您的安全性 您能告诉我,该怎么做吗?我从来没有为使用 SecurityWebFilterChain 配置过任何东西
    【解决方案2】:

    虽然 Twin 的回答已经收到了赞成票,而且问题很老了:如果您想通过使用 Basic Auth 来保护 Actuator 端点,您还需要提供合适的 AuthenticationManager。 AuthenticationManager 针对某种存储库对用户名+密码进行身份验证。

    在以下示例中,请求针对 application.yml 中定义的用户进行身份验证:

    安全配置

    @RequiredArgsConstructor
    @Configuration
    public class SecurityConfig {
    
      private final SecurityProperties securityProperties;
    
      @Bean
      public SecurityWebFilterChain actuator(ServerHttpSecurity http) {
        return http
            .authorizeExchange()
            .matchers(EndpointRequest.toAnyEndpoint().excluding("prometheus")).hasRole("ACTUATOR")
            .anyExchange().permitAll()
            .and()
            .httpBasic()
            .authenticationManager(new UserDetailsRepositoryReactiveAuthenticationManager(
                new MapReactiveUserDetailsService(new User(securityProperties.getUser().getName(),
                    "{noop}".concat(securityProperties.getUser().getPassword()),
                    securityProperties.getUser().getRoles().stream().map("ROLE_"::concat).map(SimpleGrantedAuthority::new)
                        .collect(Collectors.toList())))))
            .securityContextRepository(NoOpServerSecurityContextRepository.getInstance())
            .and()
            .requestCache().disable() // disables websession creation
            .build();
      }
    }
    

    application.yml

    spring:
      security:
        user:
          name: username
          password: password
          roles: ACTUATOR
    

    这还没有准备好生产,你不应该把密码放在你的源代码中。

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 2018-08-21
      • 1970-01-01
      • 2018-04-26
      • 2014-02-05
      • 1970-01-01
      • 2017-07-01
      • 2014-03-30
      相关资源
      最近更新 更多