【发布时间】:2023-03-10 17:53:01
【问题描述】:
我已经在 Spring WebFlux 应用程序中设置了身份验证。身份验证机制似乎工作正常。例如下面的代码用于设置安全网络过滤链:
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http.authorizeExchange()
.pathMatchers("/path/to/resource").hasAuthority("A_ROLE")
.anyExchange().authenticated()
.and().httpBasic()
.and().build();
}
这可以与 UserDetailsRepositoryReactiveAuthenticationManager 和 MapReactiveUserDetailsService 一起按预期工作。如果用户没有所需的权限,则返回禁止的错误代码,否则将请求传递给处理程序。
我需要在处理程序本身内应用细粒度的权限检查,并认为我应该能够从请求中检索权限,如下所示:
public Mono<ServerResponse> getMyResource(ServerRequest serverRequest) {
Authentication authentication = (Authentication)serverRequest.principal().block();
...
}
但是,我发现本金始终为空。首先,这是处理当局的正确方法吗?如果是,我可能缺少一些上游配置吗?
【问题讨论】:
标签: spring-security spring-webflux