首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >配置SpringBoot2.0acuator框架的安全性

配置SpringBoot2.0acuator框架的安全性
EN

Stack Overflow用户
提问于 2018-04-26 12:58:39
回答 2查看 3K关注 0票数 2

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

代码语言:javascript
复制
@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

当然,我试图搜索它,但我总是只获得描述不同场景或使用SpringBoot1.x框架的安全配置的页面。这里有人能帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-26 16:25:20

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

代码语言:javascript
复制
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

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

    // ...

}

另外,我不熟悉@EnableWebFluxSecurity,但是要使上面的工作正常进行,您需要@Configuration@EnableWebSecurity注释。

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

票数 2
EN

Stack Overflow用户

发布于 2020-08-14 21:02:13

虽然来自Twin的答案已经收到了很好的答案,而且这个问题已经过时了:如果您想通过使用Basic来保护Actuator端点,您还需要提供一个合适的AuthenticationManager。AuthenticationManager针对某种存储库对username+password进行身份验证。

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

SecurityConfig

代码语言:javascript
复制
@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

代码语言:javascript
复制
spring:
  security:
    user:
      name: username
      password: password
      roles: ACTUATOR

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50043626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档