advanced.adoc 31 KB

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