|
| 1 | +[[reactive-x509]] |
| 2 | += Reactive X.509 Authentication |
| 3 | + |
| 4 | +Similar to <<x509,Servlet X.509 authentication>>, reactive x509 authentication filter allows extracting an authentication token from a certificate provided by a client. |
| 5 | + |
| 6 | +Below is an example of a reactive x509 security configuration: |
| 7 | +[source,java] |
| 8 | +---- |
| 9 | +@Bean |
| 10 | +public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) { |
| 11 | + http |
| 12 | + .x509() |
| 13 | + .and() |
| 14 | + .authorizeExchange() |
| 15 | + .anyExchange().permitAll(); |
| 16 | +
|
| 17 | + return http.build(); |
| 18 | +} |
| 19 | +---- |
| 20 | + |
| 21 | +In the configuration above, when neither `principalExtractor` nor `authenticationManager` is provided defaults will be used. The default principal extractor is `SubjectDnX509PrincipalExtractor` which extracts the CN (common name) field from a certificate provided by a client. The default authentication manager is `ReactivePreAuthenticatedAuthenticationManager` which performs user account validation, checking that user account with a name extracted by `principalExtractor` exists and it is not locked, disabled, or expired. |
| 22 | + |
| 23 | +The next example demonstrates how these defaults can be overridden. |
| 24 | + |
| 25 | +[source,java] |
| 26 | +---- |
| 27 | +@Bean |
| 28 | +public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) { |
| 29 | + SubjectDnX509PrincipalExtractor principalExtractor = |
| 30 | + new SubjectDnX509PrincipalExtractor(); |
| 31 | +
|
| 32 | + principalExtractor.setSubjectDnRegex("OU=(.*?)(?:,|$)"); |
| 33 | +
|
| 34 | + ReactiveAuthenticationManager authenticationManager = authentication -> { |
| 35 | + authentication.setAuthenticated("Trusted Org Unit".equals(authentication.getName())); |
| 36 | + return Mono.just(authentication); |
| 37 | + }; |
| 38 | +
|
| 39 | + // @formatter:off |
| 40 | + http |
| 41 | + .x509() |
| 42 | + .principalExtractor(principalExtractor) |
| 43 | + .authenticationManager(authenticationManager) |
| 44 | + .and() |
| 45 | + .authorizeExchange() |
| 46 | + .anyExchange().authenticated(); |
| 47 | + // @formatter:on |
| 48 | +
|
| 49 | + return http.build(); |
| 50 | +} |
| 51 | +---- |
| 52 | + |
| 53 | +In this example, a username is extracted from the OU field of a client certificate instead of CN, and account lookup using `ReactiveUserDetailsService` is not performed at all. Instead, if the provided certificate issued to an OU named "Trusted Org Unit", a request will be authenticated. |
| 54 | + |
| 55 | +For an example of configuring Netty and `WebClient` or `curl` command-line tool to use mutual TLS and enable X.509 authentication, please refer to https://github.com/spring-projects/spring-security/tree/master/samples/boot/webflux-x509. |
0 commit comments