oauth2.adoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. = OAuth 2.0 Migrations
  2. == Validate `typ` Header with `JwtTypeValidator`
  3. If when following the 6.5 preparatory steps you set `validateTypes` to `false`, you can now remove it.
  4. You can also remove explicitly adding `JwtTypeValidator` to the list of defaults.
  5. For example, change this:
  6. [tabs]
  7. ======
  8. Java::
  9. +
  10. [source,java,role="primary"]
  11. ----
  12. @Bean
  13. JwtDecoder jwtDecoder() {
  14. NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
  15. .validateTypes(false) <1>
  16. // ... your remaining configuration
  17. .build();
  18. jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithValidators(
  19. new JwtIssuerValidator(location), JwtTypeValidator.jwt())); <2>
  20. return jwtDecoder;
  21. }
  22. ----
  23. Kotlin::
  24. +
  25. [source,kotlin,role="secondary"]
  26. ----
  27. @Bean
  28. fun jwtDecoder(): JwtDecoder {
  29. val jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
  30. .validateTypes(false) <1>
  31. // ... your remaining configuration
  32. .build()
  33. jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithValidators(
  34. JwtIssuerValidator(location), JwtTypeValidator.jwt())) <2>
  35. return jwtDecoder
  36. }
  37. ----
  38. ======
  39. <1> - Switch off Nimbus verifying the `typ`
  40. <2> - Add the default `typ` validator
  41. to this:
  42. [tabs]
  43. ======
  44. Java::
  45. +
  46. [source,java,role="primary"]
  47. ----
  48. @Bean
  49. JwtDecoder jwtDecoder() {
  50. NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
  51. // ... your remaining configuration <1>
  52. .build();
  53. jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithIssuer(location)); <2>
  54. return jwtDecoder;
  55. }
  56. ----
  57. Kotlin::
  58. +
  59. [source,kotlin,role="secondary"]
  60. ----
  61. @Bean
  62. fun jwtDecoder(): JwtDecoder {
  63. val jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
  64. // ... your remaining configuration
  65. .build()
  66. jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithIssuer(location)) <2>
  67. return jwtDecoder
  68. }
  69. ----
  70. ======
  71. <1> - `validateTypes` now defaults to `false`
  72. <2> - `JwtTypeValidator#jwt` is added by all `createDefaultXXX` methods
  73. == Do Not Process `<saml2:Response>` GET Requests with `Saml2AuthenticationTokenConverter`
  74. Spring Security does not support processing `<saml2:Response>` payloads over GET as this is not supported by the SAML 2.0 spec.
  75. To better comply with this, `Saml2AuthenticationTokenConverter` and `OpenSaml5AuthenticationTokenConverter` will not process GET requests by default as of Spring Security 8.
  76. To prepare for this, the property `shouldConvertGetRequests` is available.
  77. To use it, publish your own converter like so:
  78. [tabs]
  79. ======
  80. Java::
  81. +
  82. [source,java,role="primary"]
  83. ----
  84. @Bean
  85. OpenSaml5AuthenticationTokenConverter authenticationConverter(RelyingPartyRegistrationRepository registrations) {
  86. OpenSaml5AuthenticationTokenConverter authenticationConverter = new OpenSaml5AuthenticationTokenConverter(registrations);
  87. authenticationConverter.setShouldConvertGetRequests(false);
  88. return authenticationConverter;
  89. }
  90. ----
  91. Kotlin::
  92. +
  93. [source,kotlin,role="secondary"]
  94. ----
  95. @Bean
  96. fun authenticationConverter(val registrations: RelyingPartyRegistrationRepository): Saml2AuthenticationTokenConverter {
  97. val authenticationConverter = Saml2AuthenticationTokenConverter(registrations)
  98. authenticationConverter.setShouldConvertGetRequests(false)
  99. return authenticationConverter
  100. }
  101. ----
  102. ======
  103. If you must continue using `Saml2AuthenticationTokenConverter` or `OpenSaml5AuthenticationTokenConverter` to process GET requests, you can call `setShouldConvertGetRequests` to `true.`
  104. == Provide an AuthenticationConverter to BearerTokenAuthenticationFilter
  105. In Spring Security 7, `BearerTokenAuthenticationFilter#setBearerTokenResolver` and `#setAuthenticaionDetailsSource` are deprecated in favor of configuring those on `BearerTokenAuthenticationConverter`.
  106. The `oauth2ResourceServer` DSL addresses most use cases and you need to nothing.
  107. If you are setting a `BearerTokenResolver` or `AuthenticationDetailsSource` directly on `BearerTokenAuthenticationFilter` similar to the following:
  108. [tabs]
  109. ======
  110. Java::
  111. +
  112. [source,java,role="primary"]
  113. ----
  114. BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(authenticationManager);
  115. filter.setBearerTokenResolver(myBearerTokenResolver);
  116. filter.setAuthenticationDetailsSource(myAuthenticationDetailsSource);
  117. ----
  118. Kotlin::
  119. +
  120. [source,kotlin,role="secondary"]
  121. ----
  122. val filter = BearerTokenAuthenticationFilter(authenticationManager)
  123. filter.setBearerTokenResolver(myBearerTokenResolver)
  124. filter.setAuthenticationDetailsSource(myAuthenticationDetailsSource)
  125. ----
  126. ======
  127. you are encouraged to use `BearerTokenAuthenticationConverter` to specify both:
  128. [tabs]
  129. ======
  130. Java::
  131. +
  132. [source,java,role="primary"]
  133. ----
  134. BearerTokenAuthenticationConverter authenticationConverter =
  135. new BearerTokenAuthenticationConverter();
  136. authenticationConverter.setBearerTokenResolver(myBearerTokenResolver);
  137. authenticationConverter.setAuthenticationDetailsSource(myAuthenticationDetailsSource);
  138. BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(authenticationManager, authenicationConverter);
  139. ----
  140. Kotlin::
  141. +
  142. [source,kotlin,role="secondary"]
  143. ----
  144. val authenticationConverter = BearerTokenAuthenticationConverter()
  145. authenticationConverter.setBearerTokenResolver(myBearerTokenResolver)
  146. authenticationConverter.setAuthenticationDetailsSource(myAuthenticationDetailsSource)
  147. val filter = BearerTokenAuthenticationFilter(authenticationManager, authenticationConverter)
  148. ----
  149. ======