advanced.adoc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. [[oauth2login-advanced]]
  2. = Advanced Configuration
  3. `HttpSecurity.oauth2Login()` provides a number of configuration options for customizing OAuth 2.0 Login.
  4. The main configuration options are grouped into their protocol endpoint counterparts.
  5. For example, `oauth2Login().authorizationEndpoint()` allows configuring the _Authorization Endpoint_, whereas `oauth2Login().tokenEndpoint()` allows configuring the _Token Endpoint_.
  6. The following code shows an example:
  7. .Advanced OAuth2 Login Configuration
  8. ====
  9. .Java
  10. [source,java,role="primary"]
  11. ----
  12. @EnableWebSecurity
  13. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  14. @Override
  15. protected void configure(HttpSecurity http) throws Exception {
  16. http
  17. .oauth2Login(oauth2 -> oauth2
  18. .authorizationEndpoint(authorization -> authorization
  19. ...
  20. )
  21. .redirectionEndpoint(redirection -> redirection
  22. ...
  23. )
  24. .tokenEndpoint(token -> token
  25. ...
  26. )
  27. .userInfoEndpoint(userInfo -> userInfo
  28. ...
  29. )
  30. );
  31. }
  32. }
  33. ----
  34. .Kotlin
  35. [source,kotlin,role="secondary"]
  36. ----
  37. @EnableWebSecurity
  38. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  39. override fun configure(http: HttpSecurity) {
  40. http {
  41. oauth2Login {
  42. authorizationEndpoint {
  43. ...
  44. }
  45. redirectionEndpoint {
  46. ...
  47. }
  48. tokenEndpoint {
  49. ...
  50. }
  51. userInfoEndpoint {
  52. ...
  53. }
  54. }
  55. }
  56. }
  57. }
  58. ----
  59. ====
  60. The main goal of the `oauth2Login()` DSL was to closely align with the naming, as defined in the specifications.
  61. The OAuth 2.0 Authorization Framework defines the https://tools.ietf.org/html/rfc6749#section-3[Protocol Endpoints] as follows:
  62. The authorization process utilizes two authorization server endpoints (HTTP resources):
  63. * Authorization Endpoint: Used by the client to obtain authorization from the resource owner via user-agent redirection.
  64. * Token Endpoint: Used by the client to exchange an authorization grant for an access token, typically with client authentication.
  65. As well as one client endpoint:
  66. * Redirection Endpoint: Used by the authorization server to return responses containing authorization credentials to the client via the resource owner user-agent.
  67. The OpenID Connect Core 1.0 specification defines the https://openid.net/specs/openid-connect-core-1_0.html#UserInfo[UserInfo Endpoint] as follows:
  68. The UserInfo Endpoint is an OAuth 2.0 Protected Resource that returns claims about the authenticated end-user.
  69. To obtain the requested claims about the end-user, the client makes a request to the UserInfo Endpoint by using an access token obtained through OpenID Connect Authentication.
  70. These claims are normally represented by a JSON object that contains a collection of name-value pairs for the claims.
  71. The following code shows the complete configuration options available for the `oauth2Login()` DSL:
  72. .OAuth2 Login Configuration Options
  73. ====
  74. .Java
  75. [source,java,role="primary"]
  76. ----
  77. @EnableWebSecurity
  78. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  79. @Override
  80. protected void configure(HttpSecurity http) throws Exception {
  81. http
  82. .oauth2Login(oauth2 -> oauth2
  83. .clientRegistrationRepository(this.clientRegistrationRepository())
  84. .authorizedClientRepository(this.authorizedClientRepository())
  85. .authorizedClientService(this.authorizedClientService())
  86. .loginPage("/login")
  87. .authorizationEndpoint(authorization -> authorization
  88. .baseUri(this.authorizationRequestBaseUri())
  89. .authorizationRequestRepository(this.authorizationRequestRepository())
  90. .authorizationRequestResolver(this.authorizationRequestResolver())
  91. )
  92. .redirectionEndpoint(redirection -> redirection
  93. .baseUri(this.authorizationResponseBaseUri())
  94. )
  95. .tokenEndpoint(token -> token
  96. .accessTokenResponseClient(this.accessTokenResponseClient())
  97. )
  98. .userInfoEndpoint(userInfo -> userInfo
  99. .userAuthoritiesMapper(this.userAuthoritiesMapper())
  100. .userService(this.oauth2UserService())
  101. .oidcUserService(this.oidcUserService())
  102. )
  103. );
  104. }
  105. }
  106. ----
  107. .Kotlin
  108. [source,kotlin,role="secondary"]
  109. ----
  110. @EnableWebSecurity
  111. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  112. override fun configure(http: HttpSecurity) {
  113. http {
  114. oauth2Login {
  115. clientRegistrationRepository = clientRegistrationRepository()
  116. authorizedClientRepository = authorizedClientRepository()
  117. authorizedClientService = authorizedClientService()
  118. loginPage = "/login"
  119. authorizationEndpoint {
  120. baseUri = authorizationRequestBaseUri()
  121. authorizationRequestRepository = authorizationRequestRepository()
  122. authorizationRequestResolver = authorizationRequestResolver()
  123. }
  124. redirectionEndpoint {
  125. baseUri = authorizationResponseBaseUri()
  126. }
  127. tokenEndpoint {
  128. accessTokenResponseClient = accessTokenResponseClient()
  129. }
  130. userInfoEndpoint {
  131. userAuthoritiesMapper = userAuthoritiesMapper()
  132. userService = oauth2UserService()
  133. oidcUserService = oidcUserService()
  134. }
  135. }
  136. }
  137. }
  138. }
  139. ----
  140. ====
  141. In addition to the `oauth2Login()` DSL, XML configuration is also supported.
  142. The following code shows the complete configuration options available in the xref:servlet/appendix/namespace/http.adoc#nsa-oauth2-login[ security namespace]:
  143. .OAuth2 Login XML Configuration Options
  144. ====
  145. [source,xml]
  146. ----
  147. <http>
  148. <oauth2-login client-registration-repository-ref="clientRegistrationRepository"
  149. authorized-client-repository-ref="authorizedClientRepository"
  150. authorized-client-service-ref="authorizedClientService"
  151. authorization-request-repository-ref="authorizationRequestRepository"
  152. authorization-request-resolver-ref="authorizationRequestResolver"
  153. access-token-response-client-ref="accessTokenResponseClient"
  154. user-authorities-mapper-ref="userAuthoritiesMapper"
  155. user-service-ref="oauth2UserService"
  156. oidc-user-service-ref="oidcUserService"
  157. login-processing-url="/login/oauth2/code/*"
  158. login-page="/login"
  159. authentication-success-handler-ref="authenticationSuccessHandler"
  160. authentication-failure-handler-ref="authenticationFailureHandler"
  161. jwt-decoder-factory-ref="jwtDecoderFactory"/>
  162. </http>
  163. ----
  164. ====
  165. The following sections go into more detail on each of the configuration options available:
  166. * <<oauth2login-advanced-login-page, OAuth 2.0 Login Page>>
  167. * <<oauth2login-advanced-redirection-endpoint, Redirection Endpoint>>
  168. * <<oauth2login-advanced-userinfo-endpoint, UserInfo Endpoint>>
  169. [[oauth2login-advanced-login-page]]
  170. == OAuth 2.0 Login Page
  171. By default, the OAuth 2.0 Login Page is auto-generated by the `DefaultLoginPageGeneratingFilter`.
  172. The default login page shows each configured OAuth Client with its `ClientRegistration.clientName` as a link, which is capable of initiating the Authorization Request (or OAuth 2.0 Login).
  173. [NOTE]
  174. In order for `DefaultLoginPageGeneratingFilter` to show links for configured OAuth Clients, the registered `ClientRegistrationRepository` needs to also implement `Iterable<ClientRegistration>`.
  175. See `InMemoryClientRegistrationRepository` for reference.
  176. The link's destination for each OAuth Client defaults to the following:
  177. `+OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/{registrationId}"+`
  178. The following line shows an example:
  179. [source,html]
  180. ----
  181. <a href="/oauth2/authorization/google">Google</a>
  182. ----
  183. To override the default login page, configure `oauth2Login().loginPage()` and (optionally) `oauth2Login().authorizationEndpoint().baseUri()`.
  184. The following listing shows an example:
  185. .OAuth2 Login Page Configuration
  186. ====
  187. .Java
  188. [source,java,role="primary"]
  189. ----
  190. @EnableWebSecurity
  191. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  192. @Override
  193. protected void configure(HttpSecurity http) throws Exception {
  194. http
  195. .oauth2Login(oauth2 -> oauth2
  196. .loginPage("/login/oauth2")
  197. ...
  198. .authorizationEndpoint(authorization -> authorization
  199. .baseUri("/login/oauth2/authorization")
  200. ...
  201. )
  202. );
  203. }
  204. }
  205. ----
  206. .Kotlin
  207. [source,kotlin,role="secondary"]
  208. ----
  209. @EnableWebSecurity
  210. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  211. override fun configure(http: HttpSecurity) {
  212. http {
  213. oauth2Login {
  214. loginPage = "/login/oauth2"
  215. authorizationEndpoint {
  216. baseUri = "/login/oauth2/authorization"
  217. }
  218. }
  219. }
  220. }
  221. }
  222. ----
  223. .Xml
  224. [source,xml,role="secondary"]
  225. ----
  226. <http>
  227. <oauth2-login login-page="/login/oauth2"
  228. ...
  229. />
  230. </http>
  231. ----
  232. ====
  233. [IMPORTANT]
  234. You need to provide a `@Controller` with a `@RequestMapping("/login/oauth2")` that is capable of rendering the custom login page.
  235. [TIP]
  236. ====
  237. As noted earlier, configuring `oauth2Login().authorizationEndpoint().baseUri()` is optional.
  238. However, if you choose to customize it, ensure the link to each OAuth Client matches the `authorizationEndpoint().baseUri()`.
  239. The following line shows an example:
  240. [source,html]
  241. ----
  242. <a href="/login/oauth2/authorization/google">Google</a>
  243. ----
  244. ====
  245. [[oauth2login-advanced-redirection-endpoint]]
  246. == Redirection Endpoint
  247. The Redirection Endpoint is used by the Authorization Server for returning the Authorization Response (which contains the authorization credentials) to the client via the Resource Owner user-agent.
  248. [TIP]
  249. OAuth 2.0 Login leverages the Authorization Code Grant.
  250. Therefore, the authorization credential is the authorization code.
  251. The default Authorization Response `baseUri` (redirection endpoint) is `*/login/oauth2/code/**`, which is defined in `OAuth2LoginAuthenticationFilter.DEFAULT_FILTER_PROCESSES_URI`.
  252. If you would like to customize the Authorization Response `baseUri`, configure it as shown in the following example:
  253. .Redirection Endpoint Configuration
  254. ====
  255. .Java
  256. [source,java,role="primary"]
  257. ----
  258. @EnableWebSecurity
  259. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  260. @Override
  261. protected void configure(HttpSecurity http) throws Exception {
  262. http
  263. .oauth2Login(oauth2 -> oauth2
  264. .redirectionEndpoint(redirection -> redirection
  265. .baseUri("/login/oauth2/callback/*")
  266. ...
  267. )
  268. );
  269. }
  270. }
  271. ----
  272. .Kotlin
  273. [source,kotlin,role="secondary"]
  274. ----
  275. @EnableWebSecurity
  276. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  277. override fun configure(http: HttpSecurity) {
  278. http {
  279. oauth2Login {
  280. redirectionEndpoint {
  281. baseUri = "/login/oauth2/callback/*"
  282. }
  283. }
  284. }
  285. }
  286. }
  287. ----
  288. .Xml
  289. [source,xml,role="secondary"]
  290. ----
  291. <http>
  292. <oauth2-login login-processing-url="/login/oauth2/callback/*"
  293. ...
  294. />
  295. </http>
  296. ----
  297. ====
  298. [IMPORTANT]
  299. ====
  300. You also need to ensure the `ClientRegistration.redirectUri` matches the custom Authorization Response `baseUri`.
  301. The following listing shows an example:
  302. .Java
  303. [source,java,role="primary",attrs="-attributes"]
  304. ----
  305. return CommonOAuth2Provider.GOOGLE.getBuilder("google")
  306. .clientId("google-client-id")
  307. .clientSecret("google-client-secret")
  308. .redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
  309. .build();
  310. ----
  311. .Kotlin
  312. [source,kotlin,role="secondary",attrs="-attributes"]
  313. ----
  314. return CommonOAuth2Provider.GOOGLE.getBuilder("google")
  315. .clientId("google-client-id")
  316. .clientSecret("google-client-secret")
  317. .redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
  318. .build()
  319. ----
  320. ====
  321. [[oauth2login-advanced-userinfo-endpoint]]
  322. == UserInfo Endpoint
  323. The UserInfo Endpoint includes a number of configuration options, as described in the following sub-sections:
  324. * <<oauth2login-advanced-map-authorities, Mapping User Authorities>>
  325. * <<oauth2login-advanced-oauth2-user-service, OAuth 2.0 UserService>>
  326. * <<oauth2login-advanced-oidc-user-service, OpenID Connect 1.0 UserService>>
  327. [[oauth2login-advanced-map-authorities]]
  328. === Mapping User Authorities
  329. After the user successfully authenticates with the OAuth 2.0 Provider, the `OAuth2User.getAuthorities()` (or `OidcUser.getAuthorities()`) may be mapped to a new set of `GrantedAuthority` instances, which will be supplied to `OAuth2AuthenticationToken` when completing the authentication.
  330. [TIP]
  331. `OAuth2AuthenticationToken.getAuthorities()` is used for authorizing requests, such as in `hasRole('USER')` or `hasRole('ADMIN')`.
  332. There are a couple of options to choose from when mapping user authorities:
  333. * <<oauth2login-advanced-map-authorities-grantedauthoritiesmapper, Using a GrantedAuthoritiesMapper>>
  334. * <<oauth2login-advanced-map-authorities-oauth2userservice, Delegation-based strategy with OAuth2UserService>>
  335. [[oauth2login-advanced-map-authorities-grantedauthoritiesmapper]]
  336. ==== Using a GrantedAuthoritiesMapper
  337. Provide an implementation of `GrantedAuthoritiesMapper` and configure it as shown in the following example:
  338. .Granted Authorities Mapper Configuration
  339. ====
  340. .Java
  341. [source,java,role="primary"]
  342. ----
  343. @EnableWebSecurity
  344. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  345. @Override
  346. protected void configure(HttpSecurity http) throws Exception {
  347. http
  348. .oauth2Login(oauth2 -> oauth2
  349. .userInfoEndpoint(userInfo -> userInfo
  350. .userAuthoritiesMapper(this.userAuthoritiesMapper())
  351. ...
  352. )
  353. );
  354. }
  355. private GrantedAuthoritiesMapper userAuthoritiesMapper() {
  356. return (authorities) -> {
  357. Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
  358. authorities.forEach(authority -> {
  359. if (OidcUserAuthority.class.isInstance(authority)) {
  360. OidcUserAuthority oidcUserAuthority = (OidcUserAuthority)authority;
  361. OidcIdToken idToken = oidcUserAuthority.getIdToken();
  362. OidcUserInfo userInfo = oidcUserAuthority.getUserInfo();
  363. // Map the claims found in idToken and/or userInfo
  364. // to one or more GrantedAuthority's and add it to mappedAuthorities
  365. } else if (OAuth2UserAuthority.class.isInstance(authority)) {
  366. OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority)authority;
  367. Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();
  368. // Map the attributes found in userAttributes
  369. // to one or more GrantedAuthority's and add it to mappedAuthorities
  370. }
  371. });
  372. return mappedAuthorities;
  373. };
  374. }
  375. }
  376. ----
  377. .Kotlin
  378. [source,kotlin,role="secondary"]
  379. ----
  380. @EnableWebSecurity
  381. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  382. override fun configure(http: HttpSecurity) {
  383. http {
  384. oauth2Login {
  385. userInfoEndpoint {
  386. userAuthoritiesMapper = userAuthoritiesMapper()
  387. }
  388. }
  389. }
  390. }
  391. private fun userAuthoritiesMapper(): GrantedAuthoritiesMapper = GrantedAuthoritiesMapper { authorities: Collection<GrantedAuthority> ->
  392. val mappedAuthorities = emptySet<GrantedAuthority>()
  393. authorities.forEach { authority ->
  394. if (authority is OidcUserAuthority) {
  395. val idToken = authority.idToken
  396. val userInfo = authority.userInfo
  397. // Map the claims found in idToken and/or userInfo
  398. // to one or more GrantedAuthority's and add it to mappedAuthorities
  399. } else if (authority is OAuth2UserAuthority) {
  400. val userAttributes = authority.attributes
  401. // Map the attributes found in userAttributes
  402. // to one or more GrantedAuthority's and add it to mappedAuthorities
  403. }
  404. }
  405. mappedAuthorities
  406. }
  407. }
  408. ----
  409. .Xml
  410. [source,xml,role="secondary"]
  411. ----
  412. <http>
  413. <oauth2-login user-authorities-mapper-ref="userAuthoritiesMapper"
  414. ...
  415. />
  416. </http>
  417. ----
  418. ====
  419. Alternatively, you may register a `GrantedAuthoritiesMapper` `@Bean` to have it automatically applied to the configuration, as shown in the following example:
  420. .Granted Authorities Mapper Bean Configuration
  421. ====
  422. .Java
  423. [source,java,role="primary"]
  424. ----
  425. @EnableWebSecurity
  426. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  427. @Override
  428. protected void configure(HttpSecurity http) throws Exception {
  429. http
  430. .oauth2Login(withDefaults());
  431. }
  432. @Bean
  433. public GrantedAuthoritiesMapper userAuthoritiesMapper() {
  434. ...
  435. }
  436. }
  437. ----
  438. .Kotlin
  439. [source,kotlin,role="secondary"]
  440. ----
  441. @EnableWebSecurity
  442. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  443. override fun configure(http: HttpSecurity) {
  444. http {
  445. oauth2Login { }
  446. }
  447. }
  448. @Bean
  449. fun userAuthoritiesMapper(): GrantedAuthoritiesMapper {
  450. ...
  451. }
  452. }
  453. ----
  454. ====
  455. [[oauth2login-advanced-map-authorities-oauth2userservice]]
  456. ==== Delegation-based strategy with OAuth2UserService
  457. This strategy is advanced compared to using a `GrantedAuthoritiesMapper`, however, it's also more flexible as it gives you access to the `OAuth2UserRequest` and `OAuth2User` (when using an OAuth 2.0 UserService) or `OidcUserRequest` and `OidcUser` (when using an OpenID Connect 1.0 UserService).
  458. The `OAuth2UserRequest` (and `OidcUserRequest`) provides you access to the associated `OAuth2AccessToken`, which is very useful in the cases where the _delegator_ needs to fetch authority information from a protected resource before it can map the custom authorities for the user.
  459. The following example shows how to implement and configure a delegation-based strategy using an OpenID Connect 1.0 UserService:
  460. .OAuth2UserService Configuration
  461. ====
  462. .Java
  463. [source,java,role="primary"]
  464. ----
  465. @EnableWebSecurity
  466. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  467. @Override
  468. protected void configure(HttpSecurity http) throws Exception {
  469. http
  470. .oauth2Login(oauth2 -> oauth2
  471. .userInfoEndpoint(userInfo -> userInfo
  472. .oidcUserService(this.oidcUserService())
  473. ...
  474. )
  475. );
  476. }
  477. private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
  478. final OidcUserService delegate = new OidcUserService();
  479. return (userRequest) -> {
  480. // Delegate to the default implementation for loading a user
  481. OidcUser oidcUser = delegate.loadUser(userRequest);
  482. OAuth2AccessToken accessToken = userRequest.getAccessToken();
  483. Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
  484. // TODO
  485. // 1) Fetch the authority information from the protected resource using accessToken
  486. // 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities
  487. // 3) Create a copy of oidcUser but use the mappedAuthorities instead
  488. oidcUser = new DefaultOidcUser(mappedAuthorities, oidcUser.getIdToken(), oidcUser.getUserInfo());
  489. return oidcUser;
  490. };
  491. }
  492. }
  493. ----
  494. .Kotlin
  495. [source,kotlin,role="secondary"]
  496. ----
  497. @EnableWebSecurity
  498. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  499. override fun configure(http: HttpSecurity) {
  500. http {
  501. oauth2Login {
  502. userInfoEndpoint {
  503. oidcUserService = oidcUserService()
  504. }
  505. }
  506. }
  507. }
  508. @Bean
  509. fun oidcUserService(): OAuth2UserService<OidcUserRequest, OidcUser> {
  510. val delegate = OidcUserService()
  511. return OAuth2UserService { userRequest ->
  512. // Delegate to the default implementation for loading a user
  513. var oidcUser = delegate.loadUser(userRequest)
  514. val accessToken = userRequest.accessToken
  515. val mappedAuthorities = HashSet<GrantedAuthority>()
  516. // TODO
  517. // 1) Fetch the authority information from the protected resource using accessToken
  518. // 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities
  519. // 3) Create a copy of oidcUser but use the mappedAuthorities instead
  520. oidcUser = DefaultOidcUser(mappedAuthorities, oidcUser.idToken, oidcUser.userInfo)
  521. oidcUser
  522. }
  523. }
  524. }
  525. ----
  526. .Xml
  527. [source,xml,role="secondary"]
  528. ----
  529. <http>
  530. <oauth2-login oidc-user-service-ref="oidcUserService"
  531. ...
  532. />
  533. </http>
  534. ----
  535. ====
  536. [[oauth2login-advanced-oauth2-user-service]]
  537. === OAuth 2.0 UserService
  538. `DefaultOAuth2UserService` is an implementation of an `OAuth2UserService` that supports standard OAuth 2.0 Provider's.
  539. [NOTE]
  540. `OAuth2UserService` obtains the user attributes of the end-user (the resource owner) from the UserInfo Endpoint (by using the access token granted to the client during the authorization flow) and returns an `AuthenticatedPrincipal` in the form of an `OAuth2User`.
  541. `DefaultOAuth2UserService` uses a `RestOperations` when requesting the user attributes at the UserInfo Endpoint.
  542. If you need to customize the pre-processing of the UserInfo Request, you can provide `DefaultOAuth2UserService.setRequestEntityConverter()` with a custom `Converter<OAuth2UserRequest, RequestEntity<?>>`.
  543. The default implementation `OAuth2UserRequestEntityConverter` builds a `RequestEntity` representation of a UserInfo Request that sets the `OAuth2AccessToken` in the `Authorization` header by default.
  544. On the other end, if you need to customize the post-handling of the UserInfo Response, you will need to provide `DefaultOAuth2UserService.setRestOperations()` with a custom configured `RestOperations`.
  545. The default `RestOperations` is configured as follows:
  546. [source,java]
  547. ----
  548. RestTemplate restTemplate = new RestTemplate();
  549. restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  550. ----
  551. `OAuth2ErrorResponseErrorHandler` is a `ResponseErrorHandler` that can handle an OAuth 2.0 Error (400 Bad Request).
  552. It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error parameters to an `OAuth2Error`.
  553. Whether you customize `DefaultOAuth2UserService` or provide your own implementation of `OAuth2UserService`, you'll need to configure it as shown in the following example:
  554. ====
  555. .Java
  556. [source,java,role="primary"]
  557. ----
  558. @EnableWebSecurity
  559. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  560. @Override
  561. protected void configure(HttpSecurity http) throws Exception {
  562. http
  563. .oauth2Login(oauth2 -> oauth2
  564. .userInfoEndpoint(userInfo -> userInfo
  565. .userService(this.oauth2UserService())
  566. ...
  567. )
  568. );
  569. }
  570. private OAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService() {
  571. ...
  572. }
  573. }
  574. ----
  575. .Kotlin
  576. [source,kotlin,role="secondary"]
  577. ----
  578. @EnableWebSecurity
  579. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  580. override fun configure(http: HttpSecurity) {
  581. http {
  582. oauth2Login {
  583. userInfoEndpoint {
  584. userService = oauth2UserService()
  585. // ...
  586. }
  587. }
  588. }
  589. }
  590. private fun oauth2UserService(): OAuth2UserService<OAuth2UserRequest, OAuth2User> {
  591. // ...
  592. }
  593. }
  594. ----
  595. ====
  596. [[oauth2login-advanced-oidc-user-service]]
  597. === OpenID Connect 1.0 UserService
  598. `OidcUserService` is an implementation of an `OAuth2UserService` that supports OpenID Connect 1.0 Provider's.
  599. The `OidcUserService` leverages the `DefaultOAuth2UserService` when requesting the user attributes at the UserInfo Endpoint.
  600. If you need to customize the pre-processing of the UserInfo Request and/or the post-handling of the UserInfo Response, you will need to provide `OidcUserService.setOauth2UserService()` with a custom configured `DefaultOAuth2UserService`.
  601. Whether you customize `OidcUserService` or provide your own implementation of `OAuth2UserService` for OpenID Connect 1.0 Provider's, you'll need to configure it as shown in the following example:
  602. ====
  603. .Java
  604. [source,java,role="primary"]
  605. ----
  606. @EnableWebSecurity
  607. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  608. @Override
  609. protected void configure(HttpSecurity http) throws Exception {
  610. http
  611. .oauth2Login(oauth2 -> oauth2
  612. .userInfoEndpoint(userInfo -> userInfo
  613. .oidcUserService(this.oidcUserService())
  614. ...
  615. )
  616. );
  617. }
  618. private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
  619. ...
  620. }
  621. }
  622. ----
  623. .Kotlin
  624. [source,kotlin,role="secondary"]
  625. ----
  626. @EnableWebSecurity
  627. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  628. override fun configure(http: HttpSecurity) {
  629. http {
  630. oauth2Login {
  631. userInfoEndpoint {
  632. oidcUserService = oidcUserService()
  633. // ...
  634. }
  635. }
  636. }
  637. }
  638. private fun oidcUserService(): OAuth2UserService<OidcUserRequest, OidcUser> {
  639. // ...
  640. }
  641. }
  642. ----
  643. ====
  644. [[oauth2login-advanced-idtoken-verify]]
  645. == ID Token Signature Verification
  646. OpenID Connect 1.0 Authentication introduces the https://openid.net/specs/openid-connect-core-1_0.html#IDToken[ID Token], which is a security token that contains Claims about the Authentication of an End-User by an Authorization Server when used by a Client.
  647. The ID Token is represented as a https://tools.ietf.org/html/rfc7519[JSON Web Token] (JWT) and MUST be signed using https://tools.ietf.org/html/rfc7515[JSON Web Signature] (JWS).
  648. The `OidcIdTokenDecoderFactory` provides a `JwtDecoder` used for `OidcIdToken` signature verification. The default algorithm is `RS256` but may be different when assigned during client registration.
  649. For these cases, a resolver may be configured to return the expected JWS algorithm assigned for a specific client.
  650. The JWS algorithm resolver is a `Function` that accepts a `ClientRegistration` and returns the expected `JwsAlgorithm` for the client, eg. `SignatureAlgorithm.RS256` or `MacAlgorithm.HS256`
  651. The following code shows how to configure the `OidcIdTokenDecoderFactory` `@Bean` to default to `MacAlgorithm.HS256` for all `ClientRegistration`:
  652. ====
  653. .Java
  654. [source,java,role="primary"]
  655. ----
  656. @Bean
  657. public JwtDecoderFactory<ClientRegistration> idTokenDecoderFactory() {
  658. OidcIdTokenDecoderFactory idTokenDecoderFactory = new OidcIdTokenDecoderFactory();
  659. idTokenDecoderFactory.setJwsAlgorithmResolver(clientRegistration -> MacAlgorithm.HS256);
  660. return idTokenDecoderFactory;
  661. }
  662. ----
  663. .Kotlin
  664. [source,kotlin,role="secondary"]
  665. ----
  666. @Bean
  667. fun idTokenDecoderFactory(): JwtDecoderFactory<ClientRegistration?> {
  668. val idTokenDecoderFactory = OidcIdTokenDecoderFactory()
  669. idTokenDecoderFactory.setJwsAlgorithmResolver { MacAlgorithm.HS256 }
  670. return idTokenDecoderFactory
  671. }
  672. ----
  673. ====
  674. [NOTE]
  675. For MAC based algorithms such as `HS256`, `HS384` or `HS512`, the `client-secret` corresponding to the `client-id` is used as the symmetric key for signature verification.
  676. [TIP]
  677. If more than one `ClientRegistration` is configured for OpenID Connect 1.0 Authentication, the JWS algorithm resolver may evaluate the provided `ClientRegistration` to determine which algorithm to return.
  678. [[oauth2login-advanced-oidc-logout]]
  679. == OpenID Connect 1.0 Logout
  680. OpenID Connect Session Management 1.0 allows the ability to log out the End-User at the Provider using the Client.
  681. One of the strategies available is https://openid.net/specs/openid-connect-session-1_0.html#RPLogout[RP-Initiated Logout].
  682. If the OpenID Provider supports both Session Management and https://openid.net/specs/openid-connect-discovery-1_0.html[Discovery], the client may obtain the `end_session_endpoint` `URL` from the OpenID Provider's https://openid.net/specs/openid-connect-session-1_0.html#OPMetadata[Discovery Metadata].
  683. This can be achieved by configuring the `ClientRegistration` with the `issuer-uri`, as in the following example:
  684. [source,yaml]
  685. ----
  686. spring:
  687. security:
  688. oauth2:
  689. client:
  690. registration:
  691. okta:
  692. client-id: okta-client-id
  693. client-secret: okta-client-secret
  694. ...
  695. provider:
  696. okta:
  697. issuer-uri: https://dev-1234.oktapreview.com
  698. ----
  699. ...and the `OidcClientInitiatedLogoutSuccessHandler`, which implements RP-Initiated Logout, may be configured as follows:
  700. ====
  701. .Java
  702. [source,java,role="primary"]
  703. ----
  704. @EnableWebSecurity
  705. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  706. @Autowired
  707. private ClientRegistrationRepository clientRegistrationRepository;
  708. @Override
  709. protected void configure(HttpSecurity http) throws Exception {
  710. http
  711. .authorizeRequests(authorize -> authorize
  712. .anyRequest().authenticated()
  713. )
  714. .oauth2Login(withDefaults())
  715. .logout(logout -> logout
  716. .logoutSuccessHandler(oidcLogoutSuccessHandler())
  717. );
  718. }
  719. private LogoutSuccessHandler oidcLogoutSuccessHandler() {
  720. OidcClientInitiatedLogoutSuccessHandler oidcLogoutSuccessHandler =
  721. new OidcClientInitiatedLogoutSuccessHandler(this.clientRegistrationRepository);
  722. // Sets the location that the End-User's User Agent will be redirected to
  723. // after the logout has been performed at the Provider
  724. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}");
  725. return oidcLogoutSuccessHandler;
  726. }
  727. }
  728. NOTE: `OidcClientInitiatedLogoutSuccessHandler` supports the `{baseUrl}` placeholder.
  729. If used, the application's base URL, like `https://app.example.org`, will replace it at request time.
  730. ----
  731. .Kotlin
  732. [source,kotlin,role="secondary"]
  733. ----
  734. @EnableWebSecurity
  735. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  736. @Autowired
  737. private lateinit var clientRegistrationRepository: ClientRegistrationRepository
  738. override fun configure(http: HttpSecurity) {
  739. http {
  740. authorizeRequests {
  741. authorize(anyRequest, authenticated)
  742. }
  743. oauth2Login { }
  744. logout {
  745. logoutSuccessHandler = oidcLogoutSuccessHandler()
  746. }
  747. }
  748. }
  749. private fun oidcLogoutSuccessHandler(): LogoutSuccessHandler {
  750. val oidcLogoutSuccessHandler = OidcClientInitiatedLogoutSuccessHandler(clientRegistrationRepository)
  751. // Sets the location that the End-User's User Agent will be redirected to
  752. // after the logout has been performed at the Provider
  753. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}")
  754. return oidcLogoutSuccessHandler
  755. }
  756. }
  757. NOTE: `OidcClientInitiatedLogoutSuccessHandler` supports the `{baseUrl}` placeholder.
  758. If used, the application's base URL, like `https://app.example.org`, will replace it at request time.
  759. ----
  760. ====