login.adoc 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. [[webflux-oauth2-login]]
  2. = OAuth 2.0 Login
  3. The OAuth 2.0 Login feature provides an application with the capability to have users log in to the application by using their existing account at an OAuth 2.0 Provider (e.g. GitHub) or OpenID Connect 1.0 Provider (such as Google).
  4. OAuth 2.0 Login implements the use cases: "Login with Google" or "Login with GitHub".
  5. NOTE: OAuth 2.0 Login is implemented by using the *Authorization Code Grant*, as specified in the https://tools.ietf.org/html/rfc6749#section-4.1[OAuth 2.0 Authorization Framework] and https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth[OpenID Connect Core 1.0].
  6. [[webflux-oauth2-login-sample]]
  7. == Spring Boot 2.x Sample
  8. Spring Boot 2.x brings full auto-configuration capabilities for OAuth 2.0 Login.
  9. This section shows how to configure the {gh-samples-url}/reactive/webflux/java/oauth2/login[*OAuth 2.0 Login WebFlux sample*] using _Google_ as the _Authentication Provider_ and covers the following topics:
  10. * <<webflux-oauth2-login-sample-setup,Initial setup>>
  11. * <<webflux-oauth2-login-sample-redirect,Setting the redirect URI>>
  12. * <<webflux-oauth2-login-sample-config,Configure `application.yml`>>
  13. * <<webflux-oauth2-login-sample-start,Boot up the application>>
  14. [[webflux-oauth2-login-sample-setup]]
  15. === Initial setup
  16. To use Google's OAuth 2.0 authentication system for login, you must set up a project in the Google API Console to obtain OAuth 2.0 credentials.
  17. NOTE: https://developers.google.com/identity/protocols/OpenIDConnect[Google's OAuth 2.0 implementation] for authentication conforms to the https://openid.net/connect/[OpenID Connect 1.0] specification and is https://openid.net/certification/[OpenID Certified].
  18. Follow the instructions on the https://developers.google.com/identity/protocols/OpenIDConnect[OpenID Connect] page, starting in the section, "Setting up OAuth 2.0".
  19. After completing the "Obtain OAuth 2.0 credentials" instructions, you should have a new OAuth Client with credentials consisting of a Client ID and a Client Secret.
  20. [[webflux-oauth2-login-sample-redirect]]
  21. === Setting the redirect URI
  22. The redirect URI is the path in the application that the end-user's user-agent is redirected back to after they have authenticated with Google and have granted access to the OAuth Client _(<<webflux-oauth2-login-sample-setup,created in the previous step>>)_ on the Consent page.
  23. In the "Set a redirect URI" sub-section, ensure that the *Authorized redirect URIs* field is set to `http://localhost:8080/login/oauth2/code/google`.
  24. TIP: The default redirect URI template is `+{baseUrl}/login/oauth2/code/{registrationId}+`.
  25. The *_registrationId_* is a unique identifier for the xref:reactive/oauth2/client/core.adoc#oauth2Client-client-registration[ClientRegistration].
  26. For our example, the `registrationId` is `google`.
  27. IMPORTANT: If the OAuth Client is running behind a proxy server, it is recommended to check xref:features/exploits/http.adoc#http-proxy-server[Proxy Server Configuration] to ensure the application is correctly configured.
  28. Also, see the supported xref:reactive/oauth2/client/authorization-grants.adoc#oauth2Client-auth-code-redirect-uri[ `URI` template variables] for `redirect-uri`.
  29. [[webflux-oauth2-login-sample-config]]
  30. === Configure `application.yml`
  31. Now that you have a new OAuth Client with Google, you need to configure the application to use the OAuth Client for the _authentication flow_.
  32. To do so:
  33. . Go to `application.yml` and set the following configuration:
  34. +
  35. [source,yaml]
  36. ----
  37. spring:
  38. security:
  39. oauth2:
  40. client:
  41. registration: <1>
  42. google: <2>
  43. client-id: google-client-id
  44. client-secret: google-client-secret
  45. ----
  46. +
  47. .OAuth Client properties
  48. ====
  49. <1> `spring.security.oauth2.client.registration` is the base property prefix for OAuth Client properties.
  50. <2> Following the base property prefix is the ID for the xref:reactive/oauth2/client/core.adoc#oauth2Client-client-registration[`ClientRegistration`], such as google.
  51. ====
  52. . Replace the values in the `client-id` and `client-secret` property with the OAuth 2.0 credentials you created earlier.
  53. [[webflux-oauth2-login-sample-start]]
  54. === Boot up the application
  55. Launch the Spring Boot 2.x sample and go to `http://localhost:8080`.
  56. You are then redirected to the default _auto-generated_ login page, which displays a link for Google.
  57. Click on the Google link, and you are then redirected to Google for authentication.
  58. After authenticating with your Google account credentials, the next page presented to you is the Consent screen.
  59. The Consent screen asks you to either allow or deny access to the OAuth Client you created earlier.
  60. Click *Allow* to authorize the OAuth Client to access your email address and basic profile information.
  61. At this point, the OAuth Client retrieves your email address and basic profile information from the https://openid.net/specs/openid-connect-core-1_0.html#UserInfo[UserInfo Endpoint] and establishes an authenticated session.
  62. [[oauth2login-boot-property-mappings]]
  63. == Spring Boot 2.x Property Mappings
  64. The following table outlines the mapping of the Spring Boot 2.x OAuth Client properties to the xref:reactive/oauth2/client/core.adoc#oauth2Client-client-registration[ClientRegistration] properties.
  65. |===
  66. |Spring Boot 2.x |ClientRegistration
  67. |`spring.security.oauth2.client.registration._[registrationId]_`
  68. |`registrationId`
  69. |`spring.security.oauth2.client.registration._[registrationId]_.client-id`
  70. |`clientId`
  71. |`spring.security.oauth2.client.registration._[registrationId]_.client-secret`
  72. |`clientSecret`
  73. |`spring.security.oauth2.client.registration._[registrationId]_.client-authentication-method`
  74. |`clientAuthenticationMethod`
  75. |`spring.security.oauth2.client.registration._[registrationId]_.authorization-grant-type`
  76. |`authorizationGrantType`
  77. |`spring.security.oauth2.client.registration._[registrationId]_.redirect-uri`
  78. |`redirectUri`
  79. |`spring.security.oauth2.client.registration._[registrationId]_.scope`
  80. |`scopes`
  81. |`spring.security.oauth2.client.registration._[registrationId]_.client-name`
  82. |`clientName`
  83. |`spring.security.oauth2.client.provider._[providerId]_.authorization-uri`
  84. |`providerDetails.authorizationUri`
  85. |`spring.security.oauth2.client.provider._[providerId]_.token-uri`
  86. |`providerDetails.tokenUri`
  87. |`spring.security.oauth2.client.provider._[providerId]_.jwk-set-uri`
  88. |`providerDetails.jwkSetUri`
  89. |`spring.security.oauth2.client.provider._[providerId]_.issuer-uri`
  90. |`providerDetails.issuerUri`
  91. |`spring.security.oauth2.client.provider._[providerId]_.user-info-uri`
  92. |`providerDetails.userInfoEndpoint.uri`
  93. |`spring.security.oauth2.client.provider._[providerId]_.user-info-authentication-method`
  94. |`providerDetails.userInfoEndpoint.authenticationMethod`
  95. |`spring.security.oauth2.client.provider._[providerId]_.user-name-attribute`
  96. |`providerDetails.userInfoEndpoint.userNameAttributeName`
  97. |===
  98. [TIP]
  99. A `ClientRegistration` can be initially configured using discovery of an OpenID Connect Provider's https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig[Configuration endpoint] or an Authorization Server's https://tools.ietf.org/html/rfc8414#section-3[Metadata endpoint], by specifying the `spring.security.oauth2.client.provider._[providerId]_.issuer-uri` property.
  100. [[webflux-oauth2-login-common-oauth2-provider]]
  101. == CommonOAuth2Provider
  102. `CommonOAuth2Provider` pre-defines a set of default client properties for a number of well known providers: Google, GitHub, Facebook, and Okta.
  103. For example, the `authorization-uri`, `token-uri`, and `user-info-uri` do not change often for a Provider.
  104. Therefore, it makes sense to provide default values in order to reduce the required configuration.
  105. As demonstrated previously, when we <<webflux-oauth2-login-sample-config,configured a Google client>>, only the `client-id` and `client-secret` properties are required.
  106. The following listing shows an example:
  107. [source,yaml]
  108. ----
  109. spring:
  110. security:
  111. oauth2:
  112. client:
  113. registration:
  114. google:
  115. client-id: google-client-id
  116. client-secret: google-client-secret
  117. ----
  118. [TIP]
  119. The auto-defaulting of client properties works seamlessly here because the `registrationId` (`google`) matches the `GOOGLE` `enum` (case-insensitive) in `CommonOAuth2Provider`.
  120. For cases where you may want to specify a different `registrationId`, such as `google-login`, you can still leverage auto-defaulting of client properties by configuring the `provider` property.
  121. The following listing shows an example:
  122. [source,yaml]
  123. ----
  124. spring:
  125. security:
  126. oauth2:
  127. client:
  128. registration:
  129. google-login: <1>
  130. provider: google <2>
  131. client-id: google-client-id
  132. client-secret: google-client-secret
  133. ----
  134. <1> The `registrationId` is set to `google-login`.
  135. <2> The `provider` property is set to `google`, which will leverage the auto-defaulting of client properties set in `CommonOAuth2Provider.GOOGLE.getBuilder()`.
  136. [[webflux-oauth2-login-custom-provider-properties]]
  137. == Configuring Custom Provider Properties
  138. There are some OAuth 2.0 Providers that support multi-tenancy, which results in different protocol endpoints for each tenant (or sub-domain).
  139. For example, an OAuth Client registered with Okta is assigned to a specific sub-domain and have their own protocol endpoints.
  140. For these cases, Spring Boot 2.x provides the following base property for configuring custom provider properties: `spring.security.oauth2.client.provider._[providerId]_`.
  141. The following listing shows an example:
  142. [source,yaml]
  143. ----
  144. spring:
  145. security:
  146. oauth2:
  147. client:
  148. registration:
  149. okta:
  150. client-id: okta-client-id
  151. client-secret: okta-client-secret
  152. provider:
  153. okta: <1>
  154. authorization-uri: https://your-subdomain.oktapreview.com/oauth2/v1/authorize
  155. token-uri: https://your-subdomain.oktapreview.com/oauth2/v1/token
  156. user-info-uri: https://your-subdomain.oktapreview.com/oauth2/v1/userinfo
  157. user-name-attribute: sub
  158. jwk-set-uri: https://your-subdomain.oktapreview.com/oauth2/v1/keys
  159. ----
  160. <1> The base property (`spring.security.oauth2.client.provider.okta`) allows for custom configuration of protocol endpoint locations.
  161. [[webflux-oauth2-login-override-boot-autoconfig]]
  162. == Overriding Spring Boot 2.x Auto-configuration
  163. The Spring Boot 2.x auto-configuration class for OAuth Client support is `ReactiveOAuth2ClientAutoConfiguration`.
  164. It performs the following tasks:
  165. * Registers a `ReactiveClientRegistrationRepository` `@Bean` composed of `ClientRegistration`(s) from the configured OAuth Client properties.
  166. * Registers a `SecurityWebFilterChain` `@Bean` and enables OAuth 2.0 Login through `serverHttpSecurity.oauth2Login()`.
  167. If you need to override the auto-configuration based on your specific requirements, you may do so in the following ways:
  168. * <<webflux-oauth2-login-register-reactiveclientregistrationrepository-bean,Register a ReactiveClientRegistrationRepository @Bean>>
  169. * <<webflux-oauth2-login-register-securitywebfilterchain-bean,Register a SecurityWebFilterChain @Bean>>
  170. * <<webflux-oauth2-login-completely-override-autoconfiguration,Completely Override the Auto-configuration>>
  171. [[webflux-oauth2-login-register-reactiveclientregistrationrepository-bean]]
  172. === Register a ReactiveClientRegistrationRepository @Bean
  173. The following example shows how to register a `ReactiveClientRegistrationRepository` `@Bean`:
  174. ====
  175. .Java
  176. [source,java,role="primary",attrs="-attributes"]
  177. ----
  178. @Configuration
  179. public class OAuth2LoginConfig {
  180. @Bean
  181. public ReactiveClientRegistrationRepository clientRegistrationRepository() {
  182. return new InMemoryReactiveClientRegistrationRepository(this.googleClientRegistration());
  183. }
  184. private ClientRegistration googleClientRegistration() {
  185. return ClientRegistration.withRegistrationId("google")
  186. .clientId("google-client-id")
  187. .clientSecret("google-client-secret")
  188. .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
  189. .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
  190. .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
  191. .scope("openid", "profile", "email", "address", "phone")
  192. .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
  193. .tokenUri("https://www.googleapis.com/oauth2/v4/token")
  194. .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
  195. .userNameAttributeName(IdTokenClaimNames.SUB)
  196. .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
  197. .clientName("Google")
  198. .build();
  199. }
  200. }
  201. ----
  202. .Kotlin
  203. [source,kotlin,role="secondary",attrs="-attributes"]
  204. ----
  205. @Configuration
  206. class OAuth2LoginConfig {
  207. @Bean
  208. fun clientRegistrationRepository(): ReactiveClientRegistrationRepository {
  209. return InMemoryReactiveClientRegistrationRepository(googleClientRegistration())
  210. }
  211. private fun googleClientRegistration(): ClientRegistration {
  212. return ClientRegistration.withRegistrationId("google")
  213. .clientId("google-client-id")
  214. .clientSecret("google-client-secret")
  215. .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
  216. .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
  217. .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
  218. .scope("openid", "profile", "email", "address", "phone")
  219. .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
  220. .tokenUri("https://www.googleapis.com/oauth2/v4/token")
  221. .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
  222. .userNameAttributeName(IdTokenClaimNames.SUB)
  223. .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
  224. .clientName("Google")
  225. .build()
  226. }
  227. }
  228. ----
  229. ====
  230. [[webflux-oauth2-login-register-securitywebfilterchain-bean]]
  231. === Register a SecurityWebFilterChain @Bean
  232. The following example shows how to register a `SecurityWebFilterChain` `@Bean` with `@EnableWebFluxSecurity` and enable OAuth 2.0 login through `serverHttpSecurity.oauth2Login()`:
  233. .OAuth2 Login Configuration
  234. ====
  235. .Java
  236. [source,java,role="primary"]
  237. ----
  238. @EnableWebFluxSecurity
  239. public class OAuth2LoginSecurityConfig {
  240. @Bean
  241. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  242. http
  243. .authorizeExchange(authorize -> authorize
  244. .anyExchange().authenticated()
  245. )
  246. .oauth2Login(withDefaults());
  247. return http.build();
  248. }
  249. }
  250. ----
  251. .Kotlin
  252. [source,kotlin,role="secondary"]
  253. ----
  254. @EnableWebFluxSecurity
  255. class OAuth2LoginSecurityConfig {
  256. @Bean
  257. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  258. http {
  259. authorizeExchange {
  260. authorize(anyExchange, authenticated)
  261. }
  262. oauth2Login { }
  263. }
  264. return http.build()
  265. }
  266. }
  267. ----
  268. ====
  269. [[webflux-oauth2-login-completely-override-autoconfiguration]]
  270. === Completely Override the Auto-configuration
  271. The following example shows how to completely override the auto-configuration by registering a `ReactiveClientRegistrationRepository` `@Bean` and a `SecurityWebFilterChain` `@Bean`.
  272. .Overriding the auto-configuration
  273. ====
  274. .Java
  275. [source,java,role="primary",attrs="-attributes"]
  276. ----
  277. @EnableWebFluxSecurity
  278. public class OAuth2LoginConfig {
  279. @Bean
  280. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  281. http
  282. .authorizeExchange(authorize -> authorize
  283. .anyExchange().authenticated()
  284. )
  285. .oauth2Login(withDefaults());
  286. return http.build();
  287. }
  288. @Bean
  289. public ClientRegistrationRepository clientRegistrationRepository() {
  290. return new InMemoryClientRegistrationRepository(this.googleClientRegistration());
  291. }
  292. private ClientRegistration googleClientRegistration() {
  293. return ClientRegistration.withRegistrationId("google")
  294. .clientId("google-client-id")
  295. .clientSecret("google-client-secret")
  296. .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
  297. .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
  298. .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
  299. .scope("openid", "profile", "email", "address", "phone")
  300. .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
  301. .tokenUri("https://www.googleapis.com/oauth2/v4/token")
  302. .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
  303. .userNameAttributeName(IdTokenClaimNames.SUB)
  304. .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
  305. .clientName("Google")
  306. .build();
  307. }
  308. }
  309. ----
  310. .Kotlin
  311. [source,kotlin,role="secondary",attrs="-attributes"]
  312. ----
  313. @EnableWebFluxSecurity
  314. class OAuth2LoginConfig {
  315. @Bean
  316. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  317. http {
  318. authorizeExchange {
  319. authorize(anyExchange, authenticated)
  320. }
  321. oauth2Login { }
  322. }
  323. return http.build()
  324. }
  325. @Bean
  326. fun clientRegistrationRepository(): ReactiveClientRegistrationRepository {
  327. return InMemoryReactiveClientRegistrationRepository(googleClientRegistration())
  328. }
  329. private fun googleClientRegistration(): ClientRegistration {
  330. return ClientRegistration.withRegistrationId("google")
  331. .clientId("google-client-id")
  332. .clientSecret("google-client-secret")
  333. .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
  334. .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
  335. .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
  336. .scope("openid", "profile", "email", "address", "phone")
  337. .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
  338. .tokenUri("https://www.googleapis.com/oauth2/v4/token")
  339. .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
  340. .userNameAttributeName(IdTokenClaimNames.SUB)
  341. .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
  342. .clientName("Google")
  343. .build()
  344. }
  345. }
  346. ----
  347. ====
  348. [[webflux-oauth2-login-javaconfig-wo-boot]]
  349. == Java Configuration without Spring Boot 2.x
  350. If you are not able to use Spring Boot 2.x and would like to configure one of the pre-defined providers in `CommonOAuth2Provider` (for example, Google), apply the following configuration:
  351. .OAuth2 Login Configuration
  352. ====
  353. .Java
  354. [source,java,role="primary"]
  355. ----
  356. @EnableWebFluxSecurity
  357. public class OAuth2LoginConfig {
  358. @Bean
  359. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  360. http
  361. .authorizeExchange(authorize -> authorize
  362. .anyExchange().authenticated()
  363. )
  364. .oauth2Login(withDefaults());
  365. return http.build();
  366. }
  367. @Bean
  368. public ReactiveClientRegistrationRepository clientRegistrationRepository() {
  369. return new InMemoryReactiveClientRegistrationRepository(this.googleClientRegistration());
  370. }
  371. @Bean
  372. public ReactiveOAuth2AuthorizedClientService authorizedClientService(
  373. ReactiveClientRegistrationRepository clientRegistrationRepository) {
  374. return new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrationRepository);
  375. }
  376. @Bean
  377. public ServerOAuth2AuthorizedClientRepository authorizedClientRepository(
  378. ReactiveOAuth2AuthorizedClientService authorizedClientService) {
  379. return new AuthenticatedPrincipalServerOAuth2AuthorizedClientRepository(authorizedClientService);
  380. }
  381. private ClientRegistration googleClientRegistration() {
  382. return CommonOAuth2Provider.GOOGLE.getBuilder("google")
  383. .clientId("google-client-id")
  384. .clientSecret("google-client-secret")
  385. .build();
  386. }
  387. }
  388. ----
  389. .Kotlin
  390. [source,kotlin,role="secondary"]
  391. ----
  392. @EnableWebFluxSecurity
  393. class OAuth2LoginConfig {
  394. @Bean
  395. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  396. http {
  397. authorizeExchange {
  398. authorize(anyExchange, authenticated)
  399. }
  400. oauth2Login { }
  401. }
  402. return http.build()
  403. }
  404. @Bean
  405. fun clientRegistrationRepository(): ReactiveClientRegistrationRepository {
  406. return InMemoryReactiveClientRegistrationRepository(googleClientRegistration())
  407. }
  408. @Bean
  409. fun authorizedClientService(
  410. clientRegistrationRepository: ReactiveClientRegistrationRepository
  411. ): ReactiveOAuth2AuthorizedClientService {
  412. return InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrationRepository)
  413. }
  414. @Bean
  415. fun authorizedClientRepository(
  416. authorizedClientService: ReactiveOAuth2AuthorizedClientService
  417. ): ServerOAuth2AuthorizedClientRepository {
  418. return AuthenticatedPrincipalServerOAuth2AuthorizedClientRepository(authorizedClientService)
  419. }
  420. private fun googleClientRegistration(): ClientRegistration {
  421. return CommonOAuth2Provider.GOOGLE.getBuilder("google")
  422. .clientId("google-client-id")
  423. .clientSecret("google-client-secret")
  424. .build()
  425. }
  426. }
  427. ----
  428. ====
  429. [[webflux-oauth2-login-advanced]]
  430. == Advanced Configuration
  431. The OAuth 2.0 Authorization Framework defines the https://tools.ietf.org/html/rfc6749#section-3[Protocol Endpoints] as follows:
  432. The authorization process utilizes two authorization server endpoints (HTTP resources):
  433. * Authorization Endpoint: Used by the client to obtain authorization from the resource owner via user-agent redirection.
  434. * Token Endpoint: Used by the client to exchange an authorization grant for an access token, typically with client authentication.
  435. As well as one client endpoint:
  436. * Redirection Endpoint: Used by the authorization server to return responses containing authorization credentials to the client via the resource owner user-agent.
  437. The OpenID Connect Core 1.0 specification defines the https://openid.net/specs/openid-connect-core-1_0.html#UserInfo[UserInfo Endpoint] as follows:
  438. The UserInfo Endpoint is an OAuth 2.0 Protected Resource that returns claims about the authenticated end-user.
  439. 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.
  440. These claims are normally represented by a JSON object that contains a collection of name-value pairs for the claims.
  441. `ServerHttpSecurity.oauth2Login()` provides a number of configuration options for customizing OAuth 2.0 Login.
  442. The following code shows the complete configuration options available for the `oauth2Login()` DSL:
  443. .OAuth2 Login Configuration Options
  444. ====
  445. .Java
  446. [source,java,role="primary"]
  447. ----
  448. @EnableWebFluxSecurity
  449. public class OAuth2LoginSecurityConfig {
  450. @Bean
  451. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  452. http
  453. .oauth2Login(oauth2 -> oauth2
  454. .authenticationConverter(this.authenticationConverter())
  455. .authenticationMatcher(this.authenticationMatcher())
  456. .authenticationManager(this.authenticationManager())
  457. .authenticationSuccessHandler(this.authenticationSuccessHandler())
  458. .authenticationFailureHandler(this.authenticationFailureHandler())
  459. .clientRegistrationRepository(this.clientRegistrationRepository())
  460. .authorizedClientRepository(this.authorizedClientRepository())
  461. .authorizedClientService(this.authorizedClientService())
  462. .authorizationRequestResolver(this.authorizationRequestResolver())
  463. .authorizationRequestRepository(this.authorizationRequestRepository())
  464. .securityContextRepository(this.securityContextRepository())
  465. );
  466. return http.build();
  467. }
  468. }
  469. ----
  470. .Kotlin
  471. [source,kotlin,role="secondary"]
  472. ----
  473. @EnableWebFluxSecurity
  474. class OAuth2LoginSecurityConfig {
  475. @Bean
  476. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  477. http {
  478. oauth2Login {
  479. authenticationConverter = authenticationConverter()
  480. authenticationMatcher = authenticationMatcher()
  481. authenticationManager = authenticationManager()
  482. authenticationSuccessHandler = authenticationSuccessHandler()
  483. authenticationFailureHandler = authenticationFailureHandler()
  484. clientRegistrationRepository = clientRegistrationRepository()
  485. authorizedClientRepository = authorizedClientRepository()
  486. authorizedClientService = authorizedClientService()
  487. authorizationRequestResolver = authorizationRequestResolver()
  488. authorizationRequestRepository = authorizationRequestRepository()
  489. securityContextRepository = securityContextRepository()
  490. }
  491. }
  492. return http.build()
  493. }
  494. }
  495. ----
  496. ====
  497. The following sections go into more detail on each of the configuration options available:
  498. * <<webflux-oauth2-login-advanced-login-page, OAuth 2.0 Login Page>>
  499. * <<webflux-oauth2-login-advanced-redirection-endpoint, Redirection Endpoint>>
  500. * <<webflux-oauth2-login-advanced-userinfo-endpoint, UserInfo Endpoint>>
  501. * <<webflux-oauth2-login-advanced-idtoken-verify, ID Token Signature Verification>>
  502. * <<webflux-oauth2-login-advanced-oidc-logout, OpenID Connect 1.0 Logout>>
  503. [[webflux-oauth2-login-advanced-login-page]]
  504. === OAuth 2.0 Login Page
  505. By default, the OAuth 2.0 Login Page is auto-generated by the `LoginPageGeneratingWebFilter`.
  506. 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).
  507. [NOTE]
  508. In order for `LoginPageGeneratingWebFilter` to show links for configured OAuth Clients, the registered `ReactiveClientRegistrationRepository` needs to also implement `Iterable<ClientRegistration>`.
  509. See `InMemoryReactiveClientRegistrationRepository` for reference.
  510. The link's destination for each OAuth Client defaults to the following:
  511. `+"/oauth2/authorization/{registrationId}"+`
  512. The following line shows an example:
  513. [source,html]
  514. ----
  515. <a href="/oauth2/authorization/google">Google</a>
  516. ----
  517. To override the default login page, configure the `exceptionHandling().authenticationEntryPoint()` and (optionally) `oauth2Login().authorizationRequestResolver()`.
  518. The following listing shows an example:
  519. .OAuth2 Login Page Configuration
  520. ====
  521. .Java
  522. [source,java,role="primary"]
  523. ----
  524. @EnableWebFluxSecurity
  525. public class OAuth2LoginSecurityConfig {
  526. @Bean
  527. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  528. http
  529. .exceptionHandling(exceptions -> exceptions
  530. .authenticationEntryPoint(new RedirectServerAuthenticationEntryPoint("/login/oauth2"))
  531. )
  532. .oauth2Login(oauth2 -> oauth2
  533. .authorizationRequestResolver(this.authorizationRequestResolver())
  534. );
  535. return http.build();
  536. }
  537. private ServerOAuth2AuthorizationRequestResolver authorizationRequestResolver() {
  538. ServerWebExchangeMatcher authorizationRequestMatcher =
  539. new PathPatternParserServerWebExchangeMatcher(
  540. "/login/oauth2/authorization/{registrationId}");
  541. return new DefaultServerOAuth2AuthorizationRequestResolver(
  542. this.clientRegistrationRepository(), authorizationRequestMatcher);
  543. }
  544. ...
  545. }
  546. ----
  547. .Kotlin
  548. [source,kotlin,role="secondary"]
  549. ----
  550. @EnableWebFluxSecurity
  551. class OAuth2LoginSecurityConfig {
  552. @Bean
  553. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  554. http {
  555. exceptionHandling {
  556. authenticationEntryPoint = RedirectServerAuthenticationEntryPoint("/login/oauth2")
  557. }
  558. oauth2Login {
  559. authorizationRequestResolver = authorizationRequestResolver()
  560. }
  561. }
  562. return http.build()
  563. }
  564. private fun authorizationRequestResolver(): ServerOAuth2AuthorizationRequestResolver {
  565. val authorizationRequestMatcher: ServerWebExchangeMatcher = PathPatternParserServerWebExchangeMatcher(
  566. "/login/oauth2/authorization/{registrationId}"
  567. )
  568. return DefaultServerOAuth2AuthorizationRequestResolver(
  569. clientRegistrationRepository(), authorizationRequestMatcher
  570. )
  571. }
  572. ...
  573. }
  574. ----
  575. ====
  576. [IMPORTANT]
  577. You need to provide a `@Controller` with a `@RequestMapping("/login/oauth2")` that is capable of rendering the custom login page.
  578. [TIP]
  579. ====
  580. As noted earlier, configuring `oauth2Login().authorizationRequestResolver()` is optional.
  581. However, if you choose to customize it, ensure the link to each OAuth Client matches the pattern provided through the `ServerWebExchangeMatcher`.
  582. The following line shows an example:
  583. [source,html]
  584. ----
  585. <a href="/login/oauth2/authorization/google">Google</a>
  586. ----
  587. ====
  588. [[webflux-oauth2-login-advanced-redirection-endpoint]]
  589. === Redirection Endpoint
  590. 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.
  591. [TIP]
  592. OAuth 2.0 Login leverages the Authorization Code Grant.
  593. Therefore, the authorization credential is the authorization code.
  594. The default Authorization Response redirection endpoint is `/login/oauth2/code/{registrationId}`.
  595. If you would like to customize the Authorization Response redirection endpoint, configure it as shown in the following example:
  596. .Redirection Endpoint Configuration
  597. ====
  598. .Java
  599. [source,java,role="primary"]
  600. ----
  601. @EnableWebFluxSecurity
  602. public class OAuth2LoginSecurityConfig {
  603. @Bean
  604. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  605. http
  606. .oauth2Login(oauth2 -> oauth2
  607. .authenticationMatcher(new PathPatternParserServerWebExchangeMatcher("/login/oauth2/callback/{registrationId}"))
  608. );
  609. return http.build();
  610. }
  611. }
  612. ----
  613. .Kotlin
  614. [source,kotlin,role="secondary"]
  615. ----
  616. @EnableWebFluxSecurity
  617. class OAuth2LoginSecurityConfig {
  618. @Bean
  619. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  620. http {
  621. oauth2Login {
  622. authenticationMatcher = PathPatternParserServerWebExchangeMatcher("/login/oauth2/callback/{registrationId}")
  623. }
  624. }
  625. return http.build()
  626. }
  627. }
  628. ----
  629. ====
  630. [IMPORTANT]
  631. ====
  632. You also need to ensure the `ClientRegistration.redirectUri` matches the custom Authorization Response redirection endpoint.
  633. The following listing shows an example:
  634. .Java
  635. [source,java,role="primary",attrs="-attributes"]
  636. ----
  637. return CommonOAuth2Provider.GOOGLE.getBuilder("google")
  638. .clientId("google-client-id")
  639. .clientSecret("google-client-secret")
  640. .redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
  641. .build();
  642. ----
  643. .Kotlin
  644. [source,kotlin,role="secondary",attrs="-attributes"]
  645. ----
  646. return CommonOAuth2Provider.GOOGLE.getBuilder("google")
  647. .clientId("google-client-id")
  648. .clientSecret("google-client-secret")
  649. .redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
  650. .build()
  651. ----
  652. ====
  653. [[webflux-oauth2-login-advanced-userinfo-endpoint]]
  654. === UserInfo Endpoint
  655. The UserInfo Endpoint includes a number of configuration options, as described in the following sub-sections:
  656. * <<webflux-oauth2-login-advanced-map-authorities, Mapping User Authorities>>
  657. * <<webflux-oauth2-login-advanced-oauth2-user-service, OAuth 2.0 UserService>>
  658. * <<webflux-oauth2-login-advanced-oidc-user-service, OpenID Connect 1.0 UserService>>
  659. [[webflux-oauth2-login-advanced-map-authorities]]
  660. ==== Mapping User Authorities
  661. 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.
  662. [TIP]
  663. `OAuth2AuthenticationToken.getAuthorities()` is used for authorizing requests, such as in `hasRole('USER')` or `hasRole('ADMIN')`.
  664. There are a couple of options to choose from when mapping user authorities:
  665. * <<webflux-oauth2-login-advanced-map-authorities-grantedauthoritiesmapper, Using a GrantedAuthoritiesMapper>>
  666. * <<webflux-oauth2-login-advanced-map-authorities-reactiveoauth2userservice, Delegation-based strategy with ReactiveOAuth2UserService>>
  667. [[webflux-oauth2-login-advanced-map-authorities-grantedauthoritiesmapper]]
  668. ===== Using a GrantedAuthoritiesMapper
  669. Register a `GrantedAuthoritiesMapper` `@Bean` to have it automatically applied to the configuration, as shown in the following example:
  670. .Granted Authorities Mapper Configuration
  671. ====
  672. .Java
  673. [source,java,role="primary"]
  674. ----
  675. @EnableWebFluxSecurity
  676. public class OAuth2LoginSecurityConfig {
  677. @Bean
  678. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  679. http
  680. ...
  681. .oauth2Login(withDefaults());
  682. return http.build();
  683. }
  684. @Bean
  685. public GrantedAuthoritiesMapper userAuthoritiesMapper() {
  686. return (authorities) -> {
  687. Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
  688. authorities.forEach(authority -> {
  689. if (OidcUserAuthority.class.isInstance(authority)) {
  690. OidcUserAuthority oidcUserAuthority = (OidcUserAuthority)authority;
  691. OidcIdToken idToken = oidcUserAuthority.getIdToken();
  692. OidcUserInfo userInfo = oidcUserAuthority.getUserInfo();
  693. // Map the claims found in idToken and/or userInfo
  694. // to one or more GrantedAuthority's and add it to mappedAuthorities
  695. } else if (OAuth2UserAuthority.class.isInstance(authority)) {
  696. OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority)authority;
  697. Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();
  698. // Map the attributes found in userAttributes
  699. // to one or more GrantedAuthority's and add it to mappedAuthorities
  700. }
  701. });
  702. return mappedAuthorities;
  703. };
  704. }
  705. }
  706. ----
  707. .Kotlin
  708. [source,kotlin,role="secondary"]
  709. ----
  710. @EnableWebFluxSecurity
  711. class OAuth2LoginSecurityConfig {
  712. @Bean
  713. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  714. http {
  715. oauth2Login { }
  716. }
  717. return http.build()
  718. }
  719. @Bean
  720. fun userAuthoritiesMapper(): GrantedAuthoritiesMapper = GrantedAuthoritiesMapper { authorities: Collection<GrantedAuthority> ->
  721. val mappedAuthorities = emptySet<GrantedAuthority>()
  722. authorities.forEach { authority ->
  723. if (authority is OidcUserAuthority) {
  724. val idToken = authority.idToken
  725. val userInfo = authority.userInfo
  726. // Map the claims found in idToken and/or userInfo
  727. // to one or more GrantedAuthority's and add it to mappedAuthorities
  728. } else if (authority is OAuth2UserAuthority) {
  729. val userAttributes = authority.attributes
  730. // Map the attributes found in userAttributes
  731. // to one or more GrantedAuthority's and add it to mappedAuthorities
  732. }
  733. }
  734. mappedAuthorities
  735. }
  736. }
  737. ----
  738. ====
  739. [[webflux-oauth2-login-advanced-map-authorities-reactiveoauth2userservice]]
  740. ===== Delegation-based strategy with ReactiveOAuth2UserService
  741. 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).
  742. 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.
  743. The following example shows how to implement and configure a delegation-based strategy using an OpenID Connect 1.0 UserService:
  744. .ReactiveOAuth2UserService Configuration
  745. ====
  746. .Java
  747. [source,java,role="primary"]
  748. ----
  749. @EnableWebFluxSecurity
  750. public class OAuth2LoginSecurityConfig {
  751. @Bean
  752. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  753. http
  754. ...
  755. .oauth2Login(withDefaults());
  756. return http.build();
  757. }
  758. @Bean
  759. public ReactiveOAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
  760. final OidcReactiveOAuth2UserService delegate = new OidcReactiveOAuth2UserService();
  761. return (userRequest) -> {
  762. // Delegate to the default implementation for loading a user
  763. return delegate.loadUser(userRequest)
  764. .flatMap((oidcUser) -> {
  765. OAuth2AccessToken accessToken = userRequest.getAccessToken();
  766. Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
  767. // TODO
  768. // 1) Fetch the authority information from the protected resource using accessToken
  769. // 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities
  770. // 3) Create a copy of oidcUser but use the mappedAuthorities instead
  771. oidcUser = new DefaultOidcUser(mappedAuthorities, oidcUser.getIdToken(), oidcUser.getUserInfo());
  772. return Mono.just(oidcUser);
  773. });
  774. };
  775. }
  776. }
  777. ----
  778. .Kotlin
  779. [source,kotlin,role="secondary"]
  780. ----
  781. @EnableWebFluxSecurity
  782. class OAuth2LoginSecurityConfig {
  783. @Bean
  784. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  785. http {
  786. oauth2Login { }
  787. }
  788. return http.build()
  789. }
  790. @Bean
  791. fun oidcUserService(): ReactiveOAuth2UserService<OidcUserRequest, OidcUser> {
  792. val delegate = OidcReactiveOAuth2UserService()
  793. return ReactiveOAuth2UserService { userRequest ->
  794. // Delegate to the default implementation for loading a user
  795. delegate.loadUser(userRequest)
  796. .flatMap { oidcUser ->
  797. val accessToken = userRequest.accessToken
  798. val mappedAuthorities = mutableSetOf<GrantedAuthority>()
  799. // TODO
  800. // 1) Fetch the authority information from the protected resource using accessToken
  801. // 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities
  802. // 3) Create a copy of oidcUser but use the mappedAuthorities instead
  803. val mappedOidcUser = DefaultOidcUser(mappedAuthorities, oidcUser.idToken, oidcUser.userInfo)
  804. Mono.just(mappedOidcUser)
  805. }
  806. }
  807. }
  808. }
  809. ----
  810. ====
  811. [[webflux-oauth2-login-advanced-oauth2-user-service]]
  812. ==== OAuth 2.0 UserService
  813. `DefaultReactiveOAuth2UserService` is an implementation of a `ReactiveOAuth2UserService` that supports standard OAuth 2.0 Provider's.
  814. [NOTE]
  815. `ReactiveOAuth2UserService` 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`.
  816. `DefaultReactiveOAuth2UserService` uses a `WebClient` when requesting the user attributes at the UserInfo Endpoint.
  817. 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 `DefaultReactiveOAuth2UserService.setWebClient()` with a custom configured `WebClient`.
  818. Whether you customize `DefaultReactiveOAuth2UserService` or provide your own implementation of `ReactiveOAuth2UserService`, you'll need to configure it as shown in the following example:
  819. ====
  820. .Java
  821. [source,java,role="primary"]
  822. ----
  823. @EnableWebFluxSecurity
  824. public class OAuth2LoginSecurityConfig {
  825. @Bean
  826. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  827. http
  828. ...
  829. .oauth2Login(withDefaults());
  830. return http.build();
  831. }
  832. @Bean
  833. public ReactiveOAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService() {
  834. ...
  835. }
  836. }
  837. ----
  838. .Kotlin
  839. [source,kotlin,role="secondary"]
  840. ----
  841. @EnableWebFluxSecurity
  842. class OAuth2LoginSecurityConfig {
  843. @Bean
  844. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  845. http {
  846. oauth2Login { }
  847. }
  848. return http.build()
  849. }
  850. @Bean
  851. fun oauth2UserService(): ReactiveOAuth2UserService<OAuth2UserRequest, OAuth2User> {
  852. // ...
  853. }
  854. }
  855. ----
  856. ====
  857. [[webflux-oauth2-login-advanced-oidc-user-service]]
  858. ==== OpenID Connect 1.0 UserService
  859. `OidcReactiveOAuth2UserService` is an implementation of a `ReactiveOAuth2UserService` that supports OpenID Connect 1.0 Provider's.
  860. The `OidcReactiveOAuth2UserService` leverages the `DefaultReactiveOAuth2UserService` when requesting the user attributes at the UserInfo Endpoint.
  861. 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 `OidcReactiveOAuth2UserService.setOauth2UserService()` with a custom configured `ReactiveOAuth2UserService`.
  862. Whether you customize `OidcReactiveOAuth2UserService` or provide your own implementation of `ReactiveOAuth2UserService` for OpenID Connect 1.0 Provider's, you'll need to configure it as shown in the following example:
  863. ====
  864. .Java
  865. [source,java,role="primary"]
  866. ----
  867. @EnableWebFluxSecurity
  868. public class OAuth2LoginSecurityConfig {
  869. @Bean
  870. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  871. http
  872. ...
  873. .oauth2Login(withDefaults());
  874. return http.build();
  875. }
  876. @Bean
  877. public ReactiveOAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
  878. ...
  879. }
  880. }
  881. ----
  882. .Kotlin
  883. [source,kotlin,role="secondary"]
  884. ----
  885. @EnableWebFluxSecurity
  886. class OAuth2LoginSecurityConfig {
  887. @Bean
  888. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  889. http {
  890. oauth2Login { }
  891. }
  892. return http.build()
  893. }
  894. @Bean
  895. fun oidcUserService(): ReactiveOAuth2UserService<OidcUserRequest, OidcUser> {
  896. // ...
  897. }
  898. }
  899. ----
  900. ====
  901. [[webflux-oauth2-login-advanced-idtoken-verify]]
  902. === ID Token Signature Verification
  903. 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.
  904. 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).
  905. The `ReactiveOidcIdTokenDecoderFactory` provides a `ReactiveJwtDecoder` used for `OidcIdToken` signature verification. The default algorithm is `RS256` but may be different when assigned during client registration.
  906. For these cases, a resolver may be configured to return the expected JWS algorithm assigned for a specific client.
  907. 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`
  908. The following code shows how to configure the `OidcIdTokenDecoderFactory` `@Bean` to default to `MacAlgorithm.HS256` for all `ClientRegistration`:
  909. ====
  910. .Java
  911. [source,java,role="primary"]
  912. ----
  913. @Bean
  914. public ReactiveJwtDecoderFactory<ClientRegistration> idTokenDecoderFactory() {
  915. ReactiveOidcIdTokenDecoderFactory idTokenDecoderFactory = new ReactiveOidcIdTokenDecoderFactory();
  916. idTokenDecoderFactory.setJwsAlgorithmResolver(clientRegistration -> MacAlgorithm.HS256);
  917. return idTokenDecoderFactory;
  918. }
  919. ----
  920. .Kotlin
  921. [source,kotlin,role="secondary"]
  922. ----
  923. @Bean
  924. fun idTokenDecoderFactory(): ReactiveJwtDecoderFactory<ClientRegistration> {
  925. val idTokenDecoderFactory = ReactiveOidcIdTokenDecoderFactory()
  926. idTokenDecoderFactory.setJwsAlgorithmResolver { MacAlgorithm.HS256 }
  927. return idTokenDecoderFactory
  928. }
  929. ----
  930. ====
  931. [NOTE]
  932. 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.
  933. [TIP]
  934. 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.
  935. [[webflux-oauth2-login-advanced-oidc-logout]]
  936. === OpenID Connect 1.0 Logout
  937. OpenID Connect Session Management 1.0 allows the ability to log out the End-User at the Provider using the Client.
  938. One of the strategies available is https://openid.net/specs/openid-connect-session-1_0.html#RPLogout[RP-Initiated Logout].
  939. 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].
  940. This can be achieved by configuring the `ClientRegistration` with the `issuer-uri`, as in the following example:
  941. [source,yaml]
  942. ----
  943. spring:
  944. security:
  945. oauth2:
  946. client:
  947. registration:
  948. okta:
  949. client-id: okta-client-id
  950. client-secret: okta-client-secret
  951. ...
  952. provider:
  953. okta:
  954. issuer-uri: https://dev-1234.oktapreview.com
  955. ----
  956. ...and the `OidcClientInitiatedServerLogoutSuccessHandler`, which implements RP-Initiated Logout, may be configured as follows:
  957. ====
  958. .Java
  959. [source,java,role="primary"]
  960. ----
  961. @EnableWebFluxSecurity
  962. public class OAuth2LoginSecurityConfig {
  963. @Autowired
  964. private ReactiveClientRegistrationRepository clientRegistrationRepository;
  965. @Bean
  966. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  967. http
  968. .authorizeExchange(authorize -> authorize
  969. .anyExchange().authenticated()
  970. )
  971. .oauth2Login(withDefaults())
  972. .logout(logout -> logout
  973. .logoutSuccessHandler(oidcLogoutSuccessHandler())
  974. );
  975. return http.build();
  976. }
  977. private ServerLogoutSuccessHandler oidcLogoutSuccessHandler() {
  978. OidcClientInitiatedServerLogoutSuccessHandler oidcLogoutSuccessHandler =
  979. new OidcClientInitiatedServerLogoutSuccessHandler(this.clientRegistrationRepository);
  980. // Sets the location that the End-User's User Agent will be redirected to
  981. // after the logout has been performed at the Provider
  982. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}");
  983. return oidcLogoutSuccessHandler;
  984. }
  985. }
  986. ----
  987. .Kotlin
  988. [source,kotlin,role="secondary"]
  989. ----
  990. @EnableWebFluxSecurity
  991. class OAuth2LoginSecurityConfig {
  992. @Autowired
  993. private lateinit var clientRegistrationRepository: ReactiveClientRegistrationRepository
  994. @Bean
  995. fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  996. http {
  997. authorizeExchange {
  998. authorize(anyExchange, authenticated)
  999. }
  1000. oauth2Login { }
  1001. logout {
  1002. logoutSuccessHandler = oidcLogoutSuccessHandler()
  1003. }
  1004. }
  1005. return http.build()
  1006. }
  1007. private fun oidcLogoutSuccessHandler(): ServerLogoutSuccessHandler {
  1008. val oidcLogoutSuccessHandler = OidcClientInitiatedServerLogoutSuccessHandler(clientRegistrationRepository)
  1009. // Sets the location that the End-User's User Agent will be redirected to
  1010. // after the logout has been performed at the Provider
  1011. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}")
  1012. return oidcLogoutSuccessHandler
  1013. }
  1014. }
  1015. ----
  1016. ====
  1017. NOTE: `OidcClientInitiatedServerLogoutSuccessHandler` supports the `{baseUrl}` placeholder.
  1018. If used, the application's base URL, like `https://app.example.org`, will replace it at request time.