x509.adoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. [tabs]
  6. ======
  7. Java::
  8. +
  9. [source,java,role="primary"]
  10. ----
  11. @Bean
  12. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  13. http
  14. .x509(withDefaults())
  15. .authorizeExchange(exchanges -> exchanges
  16. .anyExchange().permitAll()
  17. );
  18. return http.build();
  19. }
  20. ----
  21. Kotlin::
  22. +
  23. [source,kotlin,role="secondary"]
  24. ----
  25. @Bean
  26. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  27. return http {
  28. x509 { }
  29. authorizeExchange {
  30. authorize(anyExchange, authenticated)
  31. }
  32. }
  33. }
  34. ----
  35. ======
  36. 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.
  37. The following example demonstrates how these defaults can be overridden:
  38. [tabs]
  39. ======
  40. Java::
  41. +
  42. [source,java,role="primary"]
  43. ----
  44. @Bean
  45. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  46. SubjectDnX509PrincipalExtractor principalExtractor =
  47. new SubjectDnX509PrincipalExtractor();
  48. principalExtractor.setSubjectDnRegex("OU=(.*?)(?:,|$)");
  49. ReactiveAuthenticationManager authenticationManager = authentication -> {
  50. authentication.setAuthenticated("Trusted Org Unit".equals(authentication.getName()));
  51. return Mono.just(authentication);
  52. };
  53. http
  54. .x509(x509 -> x509
  55. .principalExtractor(principalExtractor)
  56. .authenticationManager(authenticationManager)
  57. )
  58. .authorizeExchange(exchanges -> exchanges
  59. .anyExchange().authenticated()
  60. );
  61. return http.build();
  62. }
  63. ----
  64. Kotlin::
  65. +
  66. [source,kotlin,role="secondary"]
  67. ----
  68. @Bean
  69. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain? {
  70. val customPrincipalExtractor = SubjectDnX509PrincipalExtractor()
  71. customPrincipalExtractor.setSubjectDnRegex("OU=(.*?)(?:,|$)")
  72. val customAuthenticationManager = ReactiveAuthenticationManager { authentication: Authentication ->
  73. authentication.isAuthenticated = "Trusted Org Unit" == authentication.name
  74. Mono.just(authentication)
  75. }
  76. return http {
  77. x509 {
  78. principalExtractor = customPrincipalExtractor
  79. authenticationManager = customAuthenticationManager
  80. }
  81. authorizeExchange {
  82. authorize(anyExchange, authenticated)
  83. }
  84. }
  85. }
  86. ----
  87. ======
  88. 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.
  89. 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.