oauth2.adoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. == Provide an AuthenticationConverter to BearerTokenAuthenticationFilter
  74. In Spring Security 7, `BearerTokenAuthenticationFilter#setBearerTokenResolver` and `#setAuthenticaionDetailsSource` are deprecated in favor of configuring those on `BearerTokenAuthenticationConverter`.
  75. The `oauth2ResourceServer` DSL addresses most use cases and you need to nothing.
  76. If you are setting a `BearerTokenResolver` or `AuthenticationDetailsSource` directly on `BearerTokenAuthenticationFilter` similar to the following:
  77. [tabs]
  78. ======
  79. Java::
  80. +
  81. [source,java,role="primary"]
  82. ----
  83. BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(authenticationManager);
  84. filter.setBearerTokenResolver(myBearerTokenResolver);
  85. filter.setAuthenticationDetailsSource(myAuthenticationDetailsSource);
  86. ----
  87. Kotlin::
  88. +
  89. [source,kotlin,role="secondary"]
  90. ----
  91. val filter = BearerTokenAuthenticationFilter(authenticationManager)
  92. filter.setBearerTokenResolver(myBearerTokenResolver)
  93. filter.setAuthenticationDetailsSource(myAuthenticationDetailsSource)
  94. ----
  95. ======
  96. you are encouraged to use `BearerTokenAuthenticationConverter` to specify both:
  97. [tabs]
  98. ======
  99. Java::
  100. +
  101. [source,java,role="primary"]
  102. ----
  103. BearerTokenAuthenticationConverter authenticationConverter =
  104. new BearerTokenAuthenticationConverter();
  105. authenticationConverter.setBearerTokenResolver(myBearerTokenResolver);
  106. authenticationConverter.setAuthenticationDetailsSource(myAuthenticationDetailsSource);
  107. BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(authenticationManager, authenicationConverter);
  108. ----
  109. Kotlin::
  110. +
  111. [source,kotlin,role="secondary"]
  112. ----
  113. val authenticationConverter = BearerTokenAuthenticationConverter()
  114. authenticationConverter.setBearerTokenResolver(myBearerTokenResolver)
  115. authenticationConverter.setAuthenticationDetailsSource(myAuthenticationDetailsSource)
  116. val filter = BearerTokenAuthenticationFilter(authenticationManager, authenticationConverter)
  117. ----
  118. ======