【发布时间】:2021-10-03 04:58:45
【问题描述】:
我有一个对所有端点使用 JWT 的 Spring Boot 应用程序。现在我想添加一个/actuator 端点,它使用基本身份验证来启用普罗米修斯抓取指标。
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
class SecurityConfig(
val userService: UserService
) {
@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain? {
return http {
csrf { disable() }
formLogin { disable() }
httpBasic { disable() }
authorizeExchange {
authorize(ServerWebExchangeMatchers.pathMatchers(HttpMethod.OPTIONS, "/**"), permitAll)
// the following should not use JWT but basic auth
authorize(ServerWebExchangeMatchers.pathMatchers("/actuator"), authenticated)
authorize(anyExchange, authenticated)
}
oauth2ResourceServer {
jwt {
jwtAuthenticationConverter = customConverter()
}
}
}
}
}
在 MVC 堆栈中,我会使用这样的东西:
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Configuration
@Order(1)
public static class ActuatorWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Value("${management.endpoints.web.base-path}")
private String managementPath;
@Value("${config.actuator.user.name}")
private String actuatorUser;
@Value("${config.actuator.user.password}")
private String actuatorPassword;
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser(actuatorUser)
.password(passwordEncoder().encode(actuatorPassword))
.authorities("ROLE_ACTUATOR");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new Argon2PasswordEncoder();
}
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher(managementPath + "/**")
.cors().and()
.csrf().disable()
.authorizeRequests()
.anyRequest()
.hasRole("ACTUATOR")
.and()
.httpBasic();
}
}
@Configuration
@Order(2)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.csrf().disable()
.authenticationProvider(...)
.authorizeRequests()
// ...
}
}
}
这如何转化为 webflux?
【问题讨论】:
-
您尝试制作另一个 bean 并为其设置订单吗?
-
@Toerktumlare 我一直在努力解决它,但在发现它需要在 webflux 中使用
securityMatchers 之后,它就可以工作了!详情见我的回答。
标签: spring-boot spring-security spring-webflux