为了使用 Webflux 对基于 Spring MVC 的应用程序进行现代化改造,我需要更新我的自定义 PreAuthentication 场景。我已经使用FilterBean
s 构建了一个很好的解决方案(如https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#preauth中所述)。
但是,我找不到将此解决方案迁移到反应性场景的良好起点。谁能帮我指出正确的方向?
为了使用 Webflux 对基于 Spring MVC 的应用程序进行现代化改造,我需要更新我的自定义 PreAuthentication 场景。我已经使用FilterBean
s 构建了一个很好的解决方案(如https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#preauth中所述)。
但是,我找不到将此解决方案迁移到反应性场景的良好起点。谁能帮我指出正确的方向?
我也有同样的情况。尝试使用容器身份验证(preauth.j2ee
在 Spring MVC 中)但尚未找到完整的解决方案。
我无法访问java.security.Principal
通过我的 CAS 身份验证系统进行身份验证的身份。serverWebExchange.getPrincipal()
总是空的。
由于您要求起点,因此这是我的配置:
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
@Slf4j
public class SecurityConfig {
@Bean
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
return http
.csrf().disable()
.httpBasic().disable()
.formLogin().disable()
.logout().disable()
.authenticationManager(this.authenticationManager())
.securityContextRepository(this.securityContextRepository())
.authorizeExchange().pathMatchers("/public/**").permitAll()
.and().authorizeExchange().anyExchange().authenticated()
.and().build();
}
@Bean
ReactiveAuthenticationManager authenticationManager() {
return authentication -> {
log.debug("Autentication: " + authentication.toString());
if (authentication instanceof CustomPreAuthenticationToken) {
authentication.setAuthenticated(true);
}
return Mono.just(authentication);
};
}
@Bean
ServerSecurityContextRepository securityContextRepository() {
return new ServerSecurityContextRepository() {
@Override
public Mono<Void> save(ServerWebExchange serverWebExchange, SecurityContext securityContext) {
return null;
}
@Override
public Mono<SecurityContext> load(ServerWebExchange serverWebExchange) {
return serverWebExchange.getPrincipal()
.defaultIfEmpty(() -> "empty principal")
.flatMap(principal -> Mono.just(new SecurityContextImpl(new CustomPreAuthenticationToken(principal.getName(), principal, AuthorityUtils.createAuthorityList("ROLE_USER") ))));
}
};
}
}
public class CustomPreAuthenticationToken extends UsernamePasswordAuthenticationToken {
public CustomPreAuthenticationToken(String key, Object principal, Collection<? extends GrantedAuthority> authorities) {
super(key, principal, authorities);
}
}
希望这里有人可以完成这个答案。