|
此版本仍在开发中,尚未被视为稳定版本。如需最新稳定版本,请使用 Spring Security 7.0.4! |
Reactive X.509 认证
类似于Servlet X.509 认证,响应式X.509认证过滤器允许从客户端提供的证书中提取一个认证Tokens。
以下示例展示了一个响应式的 x509 安全配置:
-
Java
-
Kotlin
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
http
.x509(withDefaults())
.authorizeExchange(exchanges -> exchanges
.anyExchange().permitAll()
);
return http.build();
}
@Bean
fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http {
x509 { }
authorizeExchange {
authorize(anyExchange, authenticated)
}
}
}
在上述配置中,当 neither principalExtractor nor authenticationManager 未提供时,将使用默认值。默认的 principal 提取器是 SubjectDnX509PrincipalExtractor,它从客户端提供的证书中提取 CN(通用名称)字段。默认的认证管理器是 ReactivePreAuthenticatedAuthenticationManager,它执行用户账户验证,检查由 principalExtractor 提取出的用户名是否存在,并且该用户账户未被锁定、禁用或过期。
以下示例演示了如何覆盖这些默认值:
-
Java
-
Kotlin
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
SubjectDnX509PrincipalExtractor principalExtractor =
new SubjectDnX509PrincipalExtractor();
principalExtractor.setSubjectDnRegex("OU=(.*?)(?:,|$)");
ReactiveAuthenticationManager authenticationManager = authentication -> {
authentication.setAuthenticated("Trusted Org Unit".equals(authentication.getName()));
return Mono.just(authentication);
};
http
.x509(x509 -> x509
.principalExtractor(principalExtractor)
.authenticationManager(authenticationManager)
)
.authorizeExchange(exchanges -> exchanges
.anyExchange().authenticated()
);
return http.build();
}
@Bean
fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain? {
val customPrincipalExtractor = SubjectDnX509PrincipalExtractor()
customPrincipalExtractor.setSubjectDnRegex("OU=(.*?)(?:,|$)")
val customAuthenticationManager = ReactiveAuthenticationManager { authentication: Authentication ->
authentication.isAuthenticated = "Trusted Org Unit" == authentication.name
Mono.just(authentication)
}
return http {
x509 {
principalExtractor = customPrincipalExtractor
authenticationManager = customAuthenticationManager
}
authorizeExchange {
authorize(anyExchange, authenticated)
}
}
}
在之前的示例中,用户名是从客户端证书的OU字段中提取出来的而不是CN,并且完全不进行使用ReactiveUserDetailsService进行的账户查找。相反,如果提供的证书颁发给了一个名为“受信任组织单位”(Trusted Org Unit)的OU,则请求会被验证通过。
有关如何配置 Netty 以及 WebClient 或 curl 命令行工具以使用双向 TLS 并启用 X.509 认证的示例,请参见 github.com/spring-projects/spring-security-samples/tree/main/servlet/java-configuration/authentication/x509。