2
0

oauth2.adoc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. = OAuth Migrations
  2. The following steps relate to changes around how to configure OAuth 2.0.
  3. == Change Default `oauth2Login()` Authorities
  4. In Spring Security 5, the default `GrantedAuthority` given to a user that authenticates with an OAuth2 or OpenID Connect 1.0 provider (via `oauth2Login()`) is `ROLE_USER`.
  5. [NOTE]
  6. ====
  7. See xref:servlet/oauth2/login/advanced.adoc#oauth2login-advanced-map-authorities[Mapping User Authorities] for more information.
  8. ====
  9. In Spring Security 6, the default authority given to a user authenticating with an OAuth2 provider is `OAUTH2_USER`.
  10. The default authority given to a user authenticating with an OpenID Connect 1.0 provider is `OIDC_USER`.
  11. These defaults allow clearer distinction of users that have authenticated with an OAuth2 or OpenID Connect 1.0 provider.
  12. If you are using authorization rules or expressions such as `hasRole("USER")` or `hasAuthority("ROLE_USER")` to authorize users with this specific authority, the new defaults in Spring Security 6 will impact your application.
  13. To opt into the new Spring Security 6 defaults, the following configuration can be used.
  14. .Configure oauth2Login() with 6.0 defaults
  15. ====
  16. .Java
  17. [source,java,role="primary"]
  18. ----
  19. @Bean
  20. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  21. http
  22. // ...
  23. .oauth2Login((oauth2Login) -> oauth2Login
  24. .userInfoEndpoint((userInfo) -> userInfo
  25. .userAuthoritiesMapper(grantedAuthoritiesMapper())
  26. )
  27. );
  28. return http.build();
  29. }
  30. private GrantedAuthoritiesMapper grantedAuthoritiesMapper() {
  31. return (authorities) -> {
  32. Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
  33. authorities.forEach((authority) -> {
  34. GrantedAuthority mappedAuthority;
  35. if (authority instanceof OidcUserAuthority) {
  36. OidcUserAuthority userAuthority = (OidcUserAuthority) authority;
  37. mappedAuthority = new OidcUserAuthority(
  38. "OIDC_USER", userAuthority.getIdToken(), userAuthority.getUserInfo());
  39. } else if (authority instanceof OAuth2UserAuthority) {
  40. OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) authority;
  41. mappedAuthority = new OAuth2UserAuthority(
  42. "OAUTH2_USER", userAuthority.getAttributes());
  43. } else {
  44. mappedAuthority = authority;
  45. }
  46. mappedAuthorities.add(mappedAuthority);
  47. });
  48. return mappedAuthorities;
  49. };
  50. }
  51. ----
  52. .Kotlin
  53. [source,kotlin,role="secondary"]
  54. ----
  55. @Bean
  56. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  57. http {
  58. // ...
  59. oauth2Login {
  60. userInfoEndpoint {
  61. userAuthoritiesMapper = grantedAuthoritiesMapper()
  62. }
  63. }
  64. }
  65. return http.build()
  66. }
  67. private fun grantedAuthoritiesMapper(): GrantedAuthoritiesMapper {
  68. return GrantedAuthoritiesMapper { authorities ->
  69. authorities.map { authority ->
  70. when (authority) {
  71. is OidcUserAuthority ->
  72. OidcUserAuthority("OIDC_USER", authority.idToken, authority.userInfo)
  73. is OAuth2UserAuthority ->
  74. OAuth2UserAuthority("OAUTH2_USER", authority.attributes)
  75. else -> authority
  76. }
  77. }
  78. }
  79. }
  80. ----
  81. .XML
  82. [source,xml,role="secondary"]
  83. ----
  84. <http>
  85. <oauth2-login user-authorities-mapper-ref="userAuthoritiesMapper" ... />
  86. </http>
  87. ----
  88. ====
  89. [[servlet-oauth2-login-authorities-opt-out]]
  90. === Opt-out Steps
  91. If configuring the new authorities gives you trouble, you can opt out and explicitly use the 5.8 authority of `ROLE_USER` with the following configuration.
  92. .Configure oauth2Login() with 5.8 defaults
  93. ====
  94. .Java
  95. [source,java,role="primary"]
  96. ----
  97. @Bean
  98. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  99. http
  100. // ...
  101. .oauth2Login((oauth2Login) -> oauth2Login
  102. .userInfoEndpoint((userInfo) -> userInfo
  103. .userAuthoritiesMapper(grantedAuthoritiesMapper())
  104. )
  105. );
  106. return http.build();
  107. }
  108. private GrantedAuthoritiesMapper grantedAuthoritiesMapper() {
  109. return (authorities) -> {
  110. Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
  111. authorities.forEach((authority) -> {
  112. GrantedAuthority mappedAuthority;
  113. if (authority instanceof OidcUserAuthority) {
  114. OidcUserAuthority userAuthority = (OidcUserAuthority) authority;
  115. mappedAuthority = new OidcUserAuthority(
  116. "ROLE_USER", userAuthority.getIdToken(), userAuthority.getUserInfo());
  117. } else if (authority instanceof OAuth2UserAuthority) {
  118. OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) authority;
  119. mappedAuthority = new OAuth2UserAuthority(
  120. "ROLE_USER", userAuthority.getAttributes());
  121. } else {
  122. mappedAuthority = authority;
  123. }
  124. mappedAuthorities.add(mappedAuthority);
  125. });
  126. return mappedAuthorities;
  127. };
  128. }
  129. ----
  130. .Kotlin
  131. [source,kotlin,role="secondary"]
  132. ----
  133. @Bean
  134. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  135. http {
  136. // ...
  137. oauth2Login {
  138. userInfoEndpoint {
  139. userAuthoritiesMapper = grantedAuthoritiesMapper()
  140. }
  141. }
  142. }
  143. return http.build()
  144. }
  145. private fun grantedAuthoritiesMapper(): GrantedAuthoritiesMapper {
  146. return GrantedAuthoritiesMapper { authorities ->
  147. authorities.map { authority ->
  148. when (authority) {
  149. is OidcUserAuthority ->
  150. OidcUserAuthority("ROLE_USER", authority.idToken, authority.userInfo)
  151. is OAuth2UserAuthority ->
  152. OAuth2UserAuthority("ROLE_USER", authority.attributes)
  153. else -> authority
  154. }
  155. }
  156. }
  157. }
  158. ----
  159. .XML
  160. [source,xml,role="secondary"]
  161. ----
  162. <http>
  163. <oauth2-login user-authorities-mapper-ref="userAuthoritiesMapper" ... />
  164. </http>
  165. ----
  166. ====
  167. == Address OAuth2 Client Deprecations
  168. In Spring Security 6, deprecated classes and methods were removed from xref:servlet/oauth2/client/index.adoc[OAuth2 Client].
  169. Each deprecation is listed below, along with a direct replacement.
  170. === `ServletOAuth2AuthorizedClientExchangeFilterFunction`
  171. The method `setAccessTokenExpiresSkew(...)` can be replaced with one of:
  172. * `ClientCredentialsOAuth2AuthorizedClientProvider#setClockSkew(...)`
  173. * `RefreshTokenOAuth2AuthorizedClientProvider#setClockSkew(...)`
  174. * `JwtBearerOAuth2AuthorizedClientProvider#setClockSkew(...)`
  175. The method `setClientCredentialsTokenResponseClient(...)` can be replaced with the constructor `ServletOAuth2AuthorizedClientExchangeFilterFunction(OAuth2AuthorizedClientManager)`.
  176. [NOTE]
  177. ====
  178. See xref:servlet/oauth2/client/authorization-grants.adoc#oauth2Client-client-creds-grant[Client Credentials] for more information.
  179. ====
  180. === `OidcUserInfo`
  181. The method `phoneNumberVerified(String)` can be replaced with `phoneNumberVerified(Boolean)`.
  182. === `OAuth2AuthorizedClientArgumentResolver`
  183. The method `setClientCredentialsTokenResponseClient(...)` can be replaced with the constructor `OAuth2AuthorizedClientArgumentResolver(OAuth2AuthorizedClientManager)`.
  184. [NOTE]
  185. ====
  186. See xref:servlet/oauth2/client/authorization-grants.adoc#oauth2Client-client-creds-grant[Client Credentials] for more information.
  187. ====
  188. === `ClaimAccessor`
  189. The method `containsClaim(...)` can be replaced with `hasClaim(...)`.
  190. === `OidcClientInitiatedLogoutSuccessHandler`
  191. The method `setPostLogoutRedirectUri(URI)` can be replaced with `setPostLogoutRedirectUri(String)`.
  192. === `HttpSessionOAuth2AuthorizationRequestRepository`
  193. The method `setAllowMultipleAuthorizationRequests(...)` has no direct replacement.
  194. === `AuthorizationRequestRepository`
  195. The method `removeAuthorizationRequest(HttpServletRequest)` can be replaced with `removeAuthorizationRequest(HttpServletRequest, HttpServletResponse)`.
  196. === `ClientRegistration`
  197. The method `getRedirectUriTemplate()` can be replaced with `getRedirectUri()`.
  198. === `ClientRegistration.Builder`
  199. The method `redirectUriTemplate(...)` can be replaced with `redirectUri(...)`.
  200. === `AbstractOAuth2AuthorizationGrantRequest`
  201. The constructor `AbstractOAuth2AuthorizationGrantRequest(AuthorizationGrantType)` can be replaced with `AbstractOAuth2AuthorizationGrantRequest(AuthorizationGrantType, ClientRegistration)`.
  202. === `ClientAuthenticationMethod`
  203. The static field `BASIC` can be replaced with `CLIENT_SECRET_BASIC`.
  204. The static field `POST` can be replaced with `CLIENT_SECRET_POST`.
  205. === `OAuth2AccessTokenResponseHttpMessageConverter`
  206. The field `tokenResponseConverter` has no direct replacement.
  207. The method `setTokenResponseConverter(...)` can be replaced with `setAccessTokenResponseConverter(...)`.
  208. The field `tokenResponseParametersConverter` has no direct replacement.
  209. The method `setTokenResponseParametersConverter(...)` can be replaced with `setAccessTokenResponseParametersConverter(...)`.
  210. === `NimbusAuthorizationCodeTokenResponseClient`
  211. The class `NimbusAuthorizationCodeTokenResponseClient` can be replaced with `DefaultAuthorizationCodeTokenResponseClient`.
  212. === `NimbusJwtDecoderJwkSupport`
  213. The class `NimbusJwtDecoderJwkSupport` can be replaced with `NimbusJwtDecoder` or `JwtDecoders`.
  214. === `ImplicitGrantConfigurer`
  215. The class `ImplicitGrantConfigurer` has no direct replacement.
  216. [WARNING]
  217. ====
  218. Use of the `implicit` grant type is not recommended and all related support is removed in Spring Security 6.
  219. ====
  220. === `AuthorizationGrantType`
  221. The static field `IMPLICIT` has no direct replacement.
  222. [WARNING]
  223. ====
  224. Use of the `implicit` grant type is not recommended and all related support is removed in Spring Security 6.
  225. ====
  226. === `OAuth2AuthorizationResponseType`
  227. The static field `TOKEN` has no direct replacement.
  228. [WARNING]
  229. ====
  230. Use of the `implicit` grant type is not recommended and all related support is removed in Spring Security 6.
  231. ====
  232. === `OAuth2AuthorizationRequest`
  233. The static method `implicit()` has no direct replacement.
  234. [WARNING]
  235. ====
  236. Use of the `implicit` grant type is not recommended and all related support is removed in Spring Security 6.
  237. ====
  238. == Address `JwtAuthenticationConverter` Deprecation
  239. The method `extractAuthorities` will be removed.
  240. Instead of extending `JwtAuthenticationConverter`, please supply a custom granted authorities converter with `JwtAuthenticationConverter#setJwtGrantedAuthoritiesConverter`.