Ult*_*der 2 java spring-security oauth-2.0 spring-boot spring-webflux
我的 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)
它工作正常,当我的访问令牌过期或不正确时,请求应该被拒绝。但是,当预授权允许请求时,我的用户主体将永远不会在我的交换中得到解决...
在反应式应用程序中,身份验证信息存储在反应式流中,并可从Mono
/访问Flux
。您可以用来ReactiveSecurityContextHolder
获取当前经过身份验证的主体或身份验证请求令牌。
@PreAuthorize("hasRole('ROLE_USER')")
public Mono<String> details() {
return ReactiveSecurityContextHolder.getContext()
.map(ctx -> ((Principal) ctx.getAuthentication().getPrincipal()).getName());
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5910 次 |
最近记录: |