我的 Spring Boot 应用程序(版本 2.6.3)有问题。我已经配置了反应式弹簧安全性,如下所示:
我的应用程序.java:
@SpringBootApplication
@EnableWebFlux
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
@Bean
public SecurityWebFilterChain springSecurityFilterChain(final ServerHttpSecurity http, final ReactiveOpaqueTokenIntrospector reactiveOpaqueTokenIntrospector) {
return http.authorizeExchange()
.anyExchange().authenticated()
.and()
.httpBasic().disable()
.cors().and()
.logout().disable()
.formLogin().disable()
.oauth2ResourceServer()
.opaqueToken()
.introspector(reactiveOpaqueTokenIntrospector)
.and().and()
.csrf()
.disable()
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的网络资源(控制器):
MyWebResource.java:
@RestController
public class MyWebResource implements MyWebResourceApi {
@PreAuthorize("hasRole('ROLE_USER')")
@Override
public Mono<String> details(String userId, ServerWebExchange exchange) {
return exchange.getPrincipal().map(Principal::getName);
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,当我的访问令牌过期或不正确时,请求应该被拒绝。但是,当预授权允许请求时,我的用户主体将永远不会在我的交换中得到解决...