配置Spring Boot 2.0 acuator框架的安全性

时间:2018-04-26 12:58:40

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

我想在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不能为null

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

2 个答案:

答案 0 :(得分:2)

在我的头脑中,最简单的方法应该是让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

答案 1 :(得分:1)

尽管Twin的答案已经收到好评,而且问题很旧:如果您想使用Basic Auth保护Actuator端点,则还需要提供合适的AuthenticationManager。 AuthenticationManager针对某种存储库对用户名和密码进行身份验证。

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

SecurityConfig

@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

这还没准备好投入生产,您不应在源代码中放置密码。