我想在Spring 2.0应用程序中使用。框架本身就像预期的那样工作,因此我能够到达例如我的/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
当然,我试图搜索它,但我总是只获得描述不同场景或使用SpringBoot1.x框架的安全配置的页面。这里有人能帮我吗?
发布于 2018-04-26 16:25:20
从我的头顶上看,最简单的方法应该是让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。
发布于 2020-08-14 21:02:13
虽然来自Twin的答案已经收到了很好的答案,而且这个问题已经过时了:如果您想通过使用Basic来保护Actuator端点,您还需要提供一个合适的AuthenticationManager。AuthenticationManager针对某种存储库对username+password进行身份验证。
在下面的示例中,根据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这还没有准备好生产,您不应该将密码放在源代码中。
https://stackoverflow.com/questions/50043626
复制相似问题