advanced.adoc 30 KB

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