2
0

x509.adoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. [[reactive-x509]]
  2. = Reactive X.509 Authentication
  3. Similar to xref:servlet/authentication/x509.adoc#servlet-x509[Servlet X.509 authentication], the reactive x509 authentication filter allows extracting an authentication token from a certificate provided by a client.
  4. The following example shows 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 preceding configuration, when neither `principalExtractor` nor `authenticationManager` is provided, defaults are 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 a user account with a name extracted by `principalExtractor` exists and that it is not locked, disabled, or expired.
  34. The following 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 the previous 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 is authenticated.
  83. For an example of configuring Netty and `WebClient` or `curl` command-line tool to use mutual TLS and enable X.509 authentication, see https://github.com/spring-projects/spring-security-samples/tree/main/servlet/java-configuration/authentication/x509.