2
0

advanced.adoc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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 uses two authorization server endpoints (HTTP resources):
  63. * Authorization Endpoint: Used by the client to obtain authorization from the resource owner through user-agent redirection.
  64. * Token Endpoint: Used by the client to exchange an authorization grant for an access token, typically with client authentication.
  65. The authorization process also uses one client endpoint:
  66. * Redirection Endpoint: Used by the authorization server to return responses that contain authorization credentials to the client through 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>>
  167. * <<oauth2login-advanced-redirection-endpoint>>
  168. * <<oauth2login-advanced-userinfo-endpoint>>
  169. * <<oauth2login-advanced-idtoken-verify>>
  170. * <<oauth2login-advanced-oidc-logout>>
  171. [[oauth2login-advanced-login-page]]
  172. == OAuth 2.0 Login Page
  173. By default, the OAuth 2.0 Login Page is auto-generated by the `DefaultLoginPageGeneratingFilter`.
  174. 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).
  175. [NOTE]
  176. ====
  177. For `DefaultLoginPageGeneratingFilter` to show links for configured OAuth Clients, the registered `ClientRegistrationRepository` needs to also implement `Iterable<ClientRegistration>`.
  178. See `InMemoryClientRegistrationRepository` for reference.
  179. ====
  180. The link's destination for each OAuth Client defaults to the following:
  181. `+OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/{registrationId}"+`
  182. The following line shows an example:
  183. ====
  184. [source,html]
  185. ----
  186. <a href="/oauth2/authorization/google">Google</a>
  187. ----
  188. ====
  189. To override the default login page, configure `oauth2Login().loginPage()` and (optionally) `oauth2Login().authorizationEndpoint().baseUri()`.
  190. The following listing shows an example:
  191. .OAuth2 Login Page Configuration
  192. ====
  193. .Java
  194. [source,java,role="primary"]
  195. ----
  196. @EnableWebSecurity
  197. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  198. @Override
  199. protected void configure(HttpSecurity http) throws Exception {
  200. http
  201. .oauth2Login(oauth2 -> oauth2
  202. .loginPage("/login/oauth2")
  203. ...
  204. .authorizationEndpoint(authorization -> authorization
  205. .baseUri("/login/oauth2/authorization")
  206. ...
  207. )
  208. );
  209. }
  210. }
  211. ----
  212. .Kotlin
  213. [source,kotlin,role="secondary"]
  214. ----
  215. @EnableWebSecurity
  216. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  217. override fun configure(http: HttpSecurity) {
  218. http {
  219. oauth2Login {
  220. loginPage = "/login/oauth2"
  221. authorizationEndpoint {
  222. baseUri = "/login/oauth2/authorization"
  223. }
  224. }
  225. }
  226. }
  227. }
  228. ----
  229. .Xml
  230. [source,xml,role="secondary"]
  231. ----
  232. <http>
  233. <oauth2-login login-page="/login/oauth2"
  234. ...
  235. />
  236. </http>
  237. ----
  238. ====
  239. [IMPORTANT]
  240. ====
  241. You need to provide a `@Controller` with a `@RequestMapping("/login/oauth2")` that is capable of rendering the custom login page.
  242. ====
  243. [TIP]
  244. =====
  245. As noted earlier, configuring `oauth2Login().authorizationEndpoint().baseUri()` is optional.
  246. However, if you choose to customize it, ensure the link to each OAuth Client matches the `authorizationEndpoint().baseUri()`.
  247. The following line shows an example:
  248. ====
  249. [source,html]
  250. ----
  251. <a href="/login/oauth2/authorization/google">Google</a>
  252. ----
  253. ====
  254. =====
  255. [[oauth2login-advanced-redirection-endpoint]]
  256. == Redirection Endpoint
  257. The Redirection Endpoint is used by the Authorization Server for returning the Authorization Response (which contains the authorization credentials) to the client through the Resource Owner user-agent.
  258. [TIP]
  259. ====
  260. OAuth 2.0 Login leverages the Authorization Code Grant.
  261. Therefore, the authorization credential is the authorization code.
  262. ====
  263. The default Authorization Response `baseUri` (redirection endpoint) is `*/login/oauth2/code/**`, which is defined in `OAuth2LoginAuthenticationFilter.DEFAULT_FILTER_PROCESSES_URI`.
  264. If you would like to customize the Authorization Response `baseUri`, configure it as follows:
  265. .Redirection Endpoint Configuration
  266. ====
  267. .Java
  268. [source,java,role="primary"]
  269. ----
  270. @EnableWebSecurity
  271. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  272. @Override
  273. protected void configure(HttpSecurity http) throws Exception {
  274. http
  275. .oauth2Login(oauth2 -> oauth2
  276. .redirectionEndpoint(redirection -> redirection
  277. .baseUri("/login/oauth2/callback/*")
  278. ...
  279. )
  280. );
  281. }
  282. }
  283. ----
  284. .Kotlin
  285. [source,kotlin,role="secondary"]
  286. ----
  287. @EnableWebSecurity
  288. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  289. override fun configure(http: HttpSecurity) {
  290. http {
  291. oauth2Login {
  292. redirectionEndpoint {
  293. baseUri = "/login/oauth2/callback/*"
  294. }
  295. }
  296. }
  297. }
  298. }
  299. ----
  300. .Xml
  301. [source,xml,role="secondary"]
  302. ----
  303. <http>
  304. <oauth2-login login-processing-url="/login/oauth2/callback/*"
  305. ...
  306. />
  307. </http>
  308. ----
  309. ====
  310. [IMPORTANT]
  311. =====
  312. You also need to ensure the `ClientRegistration.redirectUri` matches the custom Authorization Response `baseUri`.
  313. The following listing shows an example:
  314. ====
  315. .Java
  316. [source,java,role="primary",subs="-attributes"]
  317. ----
  318. return CommonOAuth2Provider.GOOGLE.getBuilder("google")
  319. .clientId("google-client-id")
  320. .clientSecret("google-client-secret")
  321. .redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
  322. .build();
  323. ----
  324. .Kotlin
  325. [source,kotlin,role="secondary",subs="-attributes"]
  326. ----
  327. return CommonOAuth2Provider.GOOGLE.getBuilder("google")
  328. .clientId("google-client-id")
  329. .clientSecret("google-client-secret")
  330. .redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
  331. .build()
  332. ----
  333. ====
  334. =====
  335. [[oauth2login-advanced-userinfo-endpoint]]
  336. == UserInfo Endpoint
  337. The UserInfo Endpoint includes a number of configuration options, as described in the following sub-sections:
  338. * <<oauth2login-advanced-map-authorities>>
  339. * <<oauth2login-advanced-oauth2-user-service>>
  340. * <<oauth2login-advanced-oidc-user-service>>
  341. [[oauth2login-advanced-map-authorities]]
  342. === Mapping User Authorities
  343. After the user successfully authenticates with the OAuth 2.0 Provider, the `OAuth2User.getAuthorities()` (or `OidcUser.getAuthorities()`) can be mapped to a new set of `GrantedAuthority` instances, which are supplied to `OAuth2AuthenticationToken` when completing the authentication.
  344. [TIP]
  345. `OAuth2AuthenticationToken.getAuthorities()` is used for authorizing requests, such as in `hasRole('USER')` or `hasRole('ADMIN')`.
  346. There are a couple of options to choose from when mapping user authorities:
  347. * <<oauth2login-advanced-map-authorities-grantedauthoritiesmapper>>
  348. * <<oauth2login-advanced-map-authorities-oauth2userservice>>
  349. [[oauth2login-advanced-map-authorities-grantedauthoritiesmapper]]
  350. ==== Using a GrantedAuthoritiesMapper
  351. Provide an implementation of `GrantedAuthoritiesMapper` and configure it, as follows:
  352. .Granted Authorities Mapper Configuration
  353. ====
  354. .Java
  355. [source,java,role="primary"]
  356. ----
  357. @EnableWebSecurity
  358. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  359. @Override
  360. protected void configure(HttpSecurity http) throws Exception {
  361. http
  362. .oauth2Login(oauth2 -> oauth2
  363. .userInfoEndpoint(userInfo -> userInfo
  364. .userAuthoritiesMapper(this.userAuthoritiesMapper())
  365. ...
  366. )
  367. );
  368. }
  369. private GrantedAuthoritiesMapper userAuthoritiesMapper() {
  370. return (authorities) -> {
  371. Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
  372. authorities.forEach(authority -> {
  373. if (OidcUserAuthority.class.isInstance(authority)) {
  374. OidcUserAuthority oidcUserAuthority = (OidcUserAuthority)authority;
  375. OidcIdToken idToken = oidcUserAuthority.getIdToken();
  376. OidcUserInfo userInfo = oidcUserAuthority.getUserInfo();
  377. // Map the claims found in idToken and/or userInfo
  378. // to one or more GrantedAuthority's and add it to mappedAuthorities
  379. } else if (OAuth2UserAuthority.class.isInstance(authority)) {
  380. OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority)authority;
  381. Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();
  382. // Map the attributes found in userAttributes
  383. // to one or more GrantedAuthority's and add it to mappedAuthorities
  384. }
  385. });
  386. return mappedAuthorities;
  387. };
  388. }
  389. }
  390. ----
  391. .Kotlin
  392. [source,kotlin,role="secondary"]
  393. ----
  394. @EnableWebSecurity
  395. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  396. override fun configure(http: HttpSecurity) {
  397. http {
  398. oauth2Login {
  399. userInfoEndpoint {
  400. userAuthoritiesMapper = userAuthoritiesMapper()
  401. }
  402. }
  403. }
  404. }
  405. private fun userAuthoritiesMapper(): GrantedAuthoritiesMapper = GrantedAuthoritiesMapper { authorities: Collection<GrantedAuthority> ->
  406. val mappedAuthorities = emptySet<GrantedAuthority>()
  407. authorities.forEach { authority ->
  408. if (authority is OidcUserAuthority) {
  409. val idToken = authority.idToken
  410. val userInfo = authority.userInfo
  411. // Map the claims found in idToken and/or userInfo
  412. // to one or more GrantedAuthority's and add it to mappedAuthorities
  413. } else if (authority is OAuth2UserAuthority) {
  414. val userAttributes = authority.attributes
  415. // Map the attributes found in userAttributes
  416. // to one or more GrantedAuthority's and add it to mappedAuthorities
  417. }
  418. }
  419. mappedAuthorities
  420. }
  421. }
  422. ----
  423. .Xml
  424. [source,xml,role="secondary"]
  425. ----
  426. <http>
  427. <oauth2-login user-authorities-mapper-ref="userAuthoritiesMapper"
  428. ...
  429. />
  430. </http>
  431. ----
  432. ====
  433. Alternatively, you can register a `GrantedAuthoritiesMapper` `@Bean` to have it automatically applied to the configuration, as follows:
  434. .Granted Authorities Mapper Bean Configuration
  435. ====
  436. .Java
  437. [source,java,role="primary"]
  438. ----
  439. @EnableWebSecurity
  440. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  441. @Override
  442. protected void configure(HttpSecurity http) throws Exception {
  443. http
  444. .oauth2Login(withDefaults());
  445. }
  446. @Bean
  447. public GrantedAuthoritiesMapper userAuthoritiesMapper() {
  448. ...
  449. }
  450. }
  451. ----
  452. .Kotlin
  453. [source,kotlin,role="secondary"]
  454. ----
  455. @EnableWebSecurity
  456. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  457. override fun configure(http: HttpSecurity) {
  458. http {
  459. oauth2Login { }
  460. }
  461. }
  462. @Bean
  463. fun userAuthoritiesMapper(): GrantedAuthoritiesMapper {
  464. ...
  465. }
  466. }
  467. ----
  468. ====
  469. [[oauth2login-advanced-map-authorities-oauth2userservice]]
  470. ==== Delegation-based Strategy with OAuth2UserService
  471. This strategy is advanced compared to using a `GrantedAuthoritiesMapper`. However, it is 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).
  472. The `OAuth2UserRequest` (and `OidcUserRequest`) provides you access to the associated `OAuth2AccessToken`, which is very useful in cases where the _delegator_ needs to fetch authority information from a protected resource before it can map the custom authorities for the user.
  473. The following example shows how to implement and configure a delegation-based strategy using an OpenID Connect 1.0 UserService:
  474. .OAuth2UserService Configuration
  475. ====
  476. .Java
  477. [source,java,role="primary"]
  478. ----
  479. @EnableWebSecurity
  480. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  481. @Override
  482. protected void configure(HttpSecurity http) throws Exception {
  483. http
  484. .oauth2Login(oauth2 -> oauth2
  485. .userInfoEndpoint(userInfo -> userInfo
  486. .oidcUserService(this.oidcUserService())
  487. ...
  488. )
  489. );
  490. }
  491. private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
  492. final OidcUserService delegate = new OidcUserService();
  493. return (userRequest) -> {
  494. // Delegate to the default implementation for loading a user
  495. OidcUser oidcUser = delegate.loadUser(userRequest);
  496. OAuth2AccessToken accessToken = userRequest.getAccessToken();
  497. Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
  498. // TODO
  499. // 1) Fetch the authority information from the protected resource using accessToken
  500. // 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities
  501. // 3) Create a copy of oidcUser but use the mappedAuthorities instead
  502. oidcUser = new DefaultOidcUser(mappedAuthorities, oidcUser.getIdToken(), oidcUser.getUserInfo());
  503. return oidcUser;
  504. };
  505. }
  506. }
  507. ----
  508. .Kotlin
  509. [source,kotlin,role="secondary"]
  510. ----
  511. @EnableWebSecurity
  512. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  513. override fun configure(http: HttpSecurity) {
  514. http {
  515. oauth2Login {
  516. userInfoEndpoint {
  517. oidcUserService = oidcUserService()
  518. }
  519. }
  520. }
  521. }
  522. @Bean
  523. fun oidcUserService(): OAuth2UserService<OidcUserRequest, OidcUser> {
  524. val delegate = OidcUserService()
  525. return OAuth2UserService { userRequest ->
  526. // Delegate to the default implementation for loading a user
  527. var oidcUser = delegate.loadUser(userRequest)
  528. val accessToken = userRequest.accessToken
  529. val mappedAuthorities = HashSet<GrantedAuthority>()
  530. // TODO
  531. // 1) Fetch the authority information from the protected resource using accessToken
  532. // 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities
  533. // 3) Create a copy of oidcUser but use the mappedAuthorities instead
  534. oidcUser = DefaultOidcUser(mappedAuthorities, oidcUser.idToken, oidcUser.userInfo)
  535. oidcUser
  536. }
  537. }
  538. }
  539. ----
  540. .Xml
  541. [source,xml,role="secondary"]
  542. ----
  543. <http>
  544. <oauth2-login oidc-user-service-ref="oidcUserService"
  545. ...
  546. />
  547. </http>
  548. ----
  549. ====
  550. [[oauth2login-advanced-oauth2-user-service]]
  551. === OAuth 2.0 UserService
  552. `DefaultOAuth2UserService` is an implementation of an `OAuth2UserService` that supports standard OAuth 2.0 Provider's.
  553. [NOTE]
  554. ====
  555. `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`.
  556. ====
  557. `DefaultOAuth2UserService` uses a `RestOperations` instance when requesting the user attributes at the UserInfo Endpoint.
  558. If you need to customize the pre-processing of the UserInfo Request, you can provide `DefaultOAuth2UserService.setRequestEntityConverter()` with a custom `Converter<OAuth2UserRequest, RequestEntity<?>>`.
  559. The default implementation `OAuth2UserRequestEntityConverter` builds a `RequestEntity` representation of a UserInfo Request that sets the `OAuth2AccessToken` in the `Authorization` header by default.
  560. On the other end, if you need to customize the post-handling of the UserInfo Response, you need to provide `DefaultOAuth2UserService.setRestOperations()` with a custom configured `RestOperations`.
  561. The default `RestOperations` is configured as follows:
  562. ====
  563. [source,java]
  564. ----
  565. RestTemplate restTemplate = new RestTemplate();
  566. restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  567. ----
  568. ====
  569. `OAuth2ErrorResponseErrorHandler` is a `ResponseErrorHandler` that can handle an OAuth 2.0 Error (400 Bad Request).
  570. It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error parameters to an `OAuth2Error`.
  571. Whether you customize `DefaultOAuth2UserService` or provide your own implementation of `OAuth2UserService`, you need to configure it as follows:
  572. ====
  573. .Java
  574. [source,java,role="primary"]
  575. ----
  576. @EnableWebSecurity
  577. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  578. @Override
  579. protected void configure(HttpSecurity http) throws Exception {
  580. http
  581. .oauth2Login(oauth2 -> oauth2
  582. .userInfoEndpoint(userInfo -> userInfo
  583. .userService(this.oauth2UserService())
  584. ...
  585. )
  586. );
  587. }
  588. private OAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService() {
  589. ...
  590. }
  591. }
  592. ----
  593. .Kotlin
  594. [source,kotlin,role="secondary"]
  595. ----
  596. @EnableWebSecurity
  597. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  598. override fun configure(http: HttpSecurity) {
  599. http {
  600. oauth2Login {
  601. userInfoEndpoint {
  602. userService = oauth2UserService()
  603. // ...
  604. }
  605. }
  606. }
  607. }
  608. private fun oauth2UserService(): OAuth2UserService<OAuth2UserRequest, OAuth2User> {
  609. // ...
  610. }
  611. }
  612. ----
  613. ====
  614. [[oauth2login-advanced-oidc-user-service]]
  615. === OpenID Connect 1.0 UserService
  616. `OidcUserService` is an implementation of an `OAuth2UserService` that supports OpenID Connect 1.0 Provider's.
  617. The `OidcUserService` leverages the `DefaultOAuth2UserService` when requesting the user attributes at the UserInfo Endpoint.
  618. If you need to customize the pre-processing of the UserInfo Request or the post-handling of the UserInfo Response, you need to provide `OidcUserService.setOauth2UserService()` with a custom configured `DefaultOAuth2UserService`.
  619. Whether you customize `OidcUserService` or provide your own implementation of `OAuth2UserService` for OpenID Connect 1.0 Provider's, you need to configure it as follows:
  620. ====
  621. .Java
  622. [source,java,role="primary"]
  623. ----
  624. @EnableWebSecurity
  625. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  626. @Override
  627. protected void configure(HttpSecurity http) throws Exception {
  628. http
  629. .oauth2Login(oauth2 -> oauth2
  630. .userInfoEndpoint(userInfo -> userInfo
  631. .oidcUserService(this.oidcUserService())
  632. ...
  633. )
  634. );
  635. }
  636. private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
  637. ...
  638. }
  639. }
  640. ----
  641. .Kotlin
  642. [source,kotlin,role="secondary"]
  643. ----
  644. @EnableWebSecurity
  645. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  646. override fun configure(http: HttpSecurity) {
  647. http {
  648. oauth2Login {
  649. userInfoEndpoint {
  650. oidcUserService = oidcUserService()
  651. // ...
  652. }
  653. }
  654. }
  655. }
  656. private fun oidcUserService(): OAuth2UserService<OidcUserRequest, OidcUser> {
  657. // ...
  658. }
  659. }
  660. ----
  661. ====
  662. [[oauth2login-advanced-idtoken-verify]]
  663. == ID Token Signature Verification
  664. 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.
  665. The ID Token is represented as a https://tools.ietf.org/html/rfc7519[JSON Web Token] (JWT) and MUST be signed by using https://tools.ietf.org/html/rfc7515[JSON Web Signature] (JWS).
  666. The `OidcIdTokenDecoderFactory` provides a `JwtDecoder` used for `OidcIdToken` signature verification. The default algorithm is `RS256` but may be different when assigned during client registration.
  667. For these cases, you can configure a resolver to return the expected JWS algorithm assigned for a specific client.
  668. The JWS algorithm resolver is a `Function` that accepts a `ClientRegistration` and returns the expected `JwsAlgorithm` for the client, such as `SignatureAlgorithm.RS256` or `MacAlgorithm.HS256`
  669. The following code shows how to configure the `OidcIdTokenDecoderFactory` `@Bean` to default to `MacAlgorithm.HS256` for all `ClientRegistration` instances:
  670. ====
  671. .Java
  672. [source,java,role="primary"]
  673. ----
  674. @Bean
  675. public JwtDecoderFactory<ClientRegistration> idTokenDecoderFactory() {
  676. OidcIdTokenDecoderFactory idTokenDecoderFactory = new OidcIdTokenDecoderFactory();
  677. idTokenDecoderFactory.setJwsAlgorithmResolver(clientRegistration -> MacAlgorithm.HS256);
  678. return idTokenDecoderFactory;
  679. }
  680. ----
  681. .Kotlin
  682. [source,kotlin,role="secondary"]
  683. ----
  684. @Bean
  685. fun idTokenDecoderFactory(): JwtDecoderFactory<ClientRegistration?> {
  686. val idTokenDecoderFactory = OidcIdTokenDecoderFactory()
  687. idTokenDecoderFactory.setJwsAlgorithmResolver { MacAlgorithm.HS256 }
  688. return idTokenDecoderFactory
  689. }
  690. ----
  691. ====
  692. [NOTE]
  693. ====
  694. For MAC-based algorithms (such as `HS256`, `HS384`, or `HS512`), the `client-secret` that corresponds to the `client-id` is used as the symmetric key for signature verification.
  695. ====
  696. [TIP]
  697. ====
  698. 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.
  699. ====
  700. [[oauth2login-advanced-oidc-logout]]
  701. == OpenID Connect 1.0 Logout
  702. OpenID Connect Session Management 1.0 allows the ability to log out the end user at the Provider by using the Client.
  703. One of the strategies available is https://openid.net/specs/openid-connect-session-1_0.html#RPLogout[RP-Initiated Logout].
  704. If the OpenID Provider supports both Session Management and https://openid.net/specs/openid-connect-discovery-1_0.html[Discovery], the client can obtain the `end_session_endpoint` `URL` from the OpenID Provider's https://openid.net/specs/openid-connect-session-1_0.html#OPMetadata[Discovery Metadata].
  705. You can do so by configuring the `ClientRegistration` with the `issuer-uri`, as follows:
  706. ====
  707. [source,yaml]
  708. ----
  709. spring:
  710. security:
  711. oauth2:
  712. client:
  713. registration:
  714. okta:
  715. client-id: okta-client-id
  716. client-secret: okta-client-secret
  717. ...
  718. provider:
  719. okta:
  720. issuer-uri: https://dev-1234.oktapreview.com
  721. ----
  722. ====
  723. Also, you can configure `OidcClientInitiatedLogoutSuccessHandler`, which implements RP-Initiated Logout, as follows:
  724. ====
  725. .Java
  726. [source,java,role="primary"]
  727. ----
  728. @EnableWebSecurity
  729. public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
  730. @Autowired
  731. private ClientRegistrationRepository clientRegistrationRepository;
  732. @Override
  733. protected void configure(HttpSecurity http) throws Exception {
  734. http
  735. .authorizeHttpRequests(authorize -> authorize
  736. .anyRequest().authenticated()
  737. )
  738. .oauth2Login(withDefaults())
  739. .logout(logout -> logout
  740. .logoutSuccessHandler(oidcLogoutSuccessHandler())
  741. );
  742. }
  743. private LogoutSuccessHandler oidcLogoutSuccessHandler() {
  744. OidcClientInitiatedLogoutSuccessHandler oidcLogoutSuccessHandler =
  745. new OidcClientInitiatedLogoutSuccessHandler(this.clientRegistrationRepository);
  746. // Sets the location that the End-User's User Agent will be redirected to
  747. // after the logout has been performed at the Provider
  748. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}");
  749. return oidcLogoutSuccessHandler;
  750. }
  751. }
  752. ----
  753. .Kotlin
  754. [source,kotlin,role="secondary"]
  755. ----
  756. @EnableWebSecurity
  757. class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
  758. @Autowired
  759. private lateinit var clientRegistrationRepository: ClientRegistrationRepository
  760. override fun configure(http: HttpSecurity) {
  761. http {
  762. authorizeRequests {
  763. authorize(anyRequest, authenticated)
  764. }
  765. oauth2Login { }
  766. logout {
  767. logoutSuccessHandler = oidcLogoutSuccessHandler()
  768. }
  769. }
  770. }
  771. private fun oidcLogoutSuccessHandler(): LogoutSuccessHandler {
  772. val oidcLogoutSuccessHandler = OidcClientInitiatedLogoutSuccessHandler(clientRegistrationRepository)
  773. // Sets the location that the End-User's User Agent will be redirected to
  774. // after the logout has been performed at the Provider
  775. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}")
  776. return oidcLogoutSuccessHandler
  777. }
  778. }
  779. ----
  780. ====
  781. [NOTE]
  782. ====
  783. `OidcClientInitiatedLogoutSuccessHandler` supports the `+{baseUrl}+` placeholder.
  784. If used, the application's base URL, such as `https://app.example.org`, replaces it at request time.
  785. ====