x509.adoc 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. [[reactive-x509]]
  2. = Reactive X.509 Authentication
  3. Similar to xref:servlet/authentication/x509.adoc#servlet-x509[Servlet X.509 authentication], reactive x509 authentication filter allows extracting an authentication token from a certificate provided by a client.
  4. Below is an example of a reactive x509 security configuration:
  5. ====
  6. .Java
  7. [source,java,role="primary"]
  8. ----
  9. @Bean
  10. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  11. http
  12. .x509(withDefaults())
  13. .authorizeExchange(exchanges -> exchanges
  14. .anyExchange().permitAll()
  15. );
  16. return http.build();
  17. }
  18. ----
  19. .Kotlin
  20. [source,kotlin,role="secondary"]
  21. ----
  22. @Bean
  23. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  24. return http {
  25. x509 { }
  26. authorizeExchange {
  27. authorize(anyExchange, authenticated)
  28. }
  29. }
  30. }
  31. ----
  32. ====
  33. 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.
  34. The next example demonstrates how these defaults can be overridden.
  35. ====
  36. .Java
  37. [source,java,role="primary"]
  38. ----
  39. @Bean
  40. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  41. SubjectDnX509PrincipalExtractor principalExtractor =
  42. new SubjectDnX509PrincipalExtractor();
  43. principalExtractor.setSubjectDnRegex("OU=(.*?)(?:,|$)");
  44. ReactiveAuthenticationManager authenticationManager = authentication -> {
  45. authentication.setAuthenticated("Trusted Org Unit".equals(authentication.getName()));
  46. return Mono.just(authentication);
  47. };
  48. http
  49. .x509(x509 -> x509
  50. .principalExtractor(principalExtractor)
  51. .authenticationManager(authenticationManager)
  52. )
  53. .authorizeExchange(exchanges -> exchanges
  54. .anyExchange().authenticated()
  55. );
  56. return http.build();
  57. }
  58. ----
  59. .Kotlin
  60. [source,kotlin,role="secondary"]
  61. ----
  62. @Bean
  63. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain? {
  64. val customPrincipalExtractor = SubjectDnX509PrincipalExtractor()
  65. customPrincipalExtractor.setSubjectDnRegex("OU=(.*?)(?:,|$)")
  66. val customAuthenticationManager = ReactiveAuthenticationManager { authentication: Authentication ->
  67. authentication.isAuthenticated = "Trusted Org Unit" == authentication.name
  68. Mono.just(authentication)
  69. }
  70. return http {
  71. x509 {
  72. principalExtractor = customPrincipalExtractor
  73. authenticationManager = customAuthenticationManager
  74. }
  75. authorizeExchange {
  76. authorize(anyExchange, authenticated)
  77. }
  78. }
  79. }
  80. ----
  81. ====
  82. 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.
  83. 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-samples/tree/main/servlet/java-configuration/authentication/x509.