【发布时间】:2019-12-28 01:37:02
【问题描述】:
我有一个包含 WebFlux、Health actuator 和 Spring security 的项目。我正在尝试构建自定义身份验证,但该身份验证也适用于健康执行器端点。如何禁用它?
根据文档,我实现了一个自定义 ServerSecurityContextRepository,这是它看起来有点像的基本版本:
@Component
class MySecurityContextRepository: ServerSecurityContextRepository {
override fun save(exchange: ServerWebExchange?, context: SecurityContext?) = Mono.empty()
override fun load(exchange: ServerWebExchange) = Mono.error(ResponseStatusException(HttpStatus.UNAUTHORIZED, "Access denied"))
}
根据文档,我不应该被要求做任何额外的配置来禁用健康端点上的身份验证。这是来自application.yml的配置:
management:
metrics:
web:
server:
auto-time-requests: true
requests-metric-name: xxx-xxx-xxx
export:
statsd:
enabled: xxxx
host: xxxxxxxxxxxx
flavor: xxx
endpoint:
health:
enabled: true
endpoints:
web:
base-path: /application
这不起作用,因为我从 /application/health 端点看到了 401。所以我也将它添加到我的安全配置中:
@EnableWebFluxSecurity
class SecurityConfig @Autowired constructor(
private val myRepository: MySecurityContextRepository
) {
@Bean
fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
http.securityContextRepository(myRepository)
http.authorizeExchange()
.pathMatchers("/application/health").permitAll()
.anyExchange().permitAll()
http.cors().disable()
http.csrf().disable()
http.formLogin().disable()
return http.build()
}
}
尽管添加此操作 curl http://localhost:8080/application/health/ 会导致 {"name":"xxxxxx","message":"Unknown Error","response":"401 UNAUTHORIZED \"Access denied\""} 并且状态代码也是 401。如何禁用对我的健康端点的授权?
【问题讨论】:
标签: spring-boot spring-security spring-boot-actuator