Skip to content

Commit a9fe2cb

Browse files
committed
Add servlet OAuth2 login Kotlin samples
Issue gh-8172
1 parent 44399a5 commit a9fe2cb

File tree

1 file changed

+152
-6
lines changed

1 file changed

+152
-6
lines changed

docs/manual/src/docs/asciidoc/_includes/servlet/oauth2/oauth2-login.adoc

Lines changed: 152 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ If you need to override the auto-configuration based on your specific requiremen
250250

251251
The following example shows how to register a `ClientRegistrationRepository` `@Bean`:
252252

253-
[source,java,attrs="-attributes"]
253+
====
254+
.Java
255+
[source,java,role="primary",attrs="-attributes"]
254256
----
255257
@Configuration
256258
public class OAuth2LoginConfig {
@@ -279,6 +281,36 @@ public class OAuth2LoginConfig {
279281
}
280282
----
281283
284+
.Kotlin
285+
[source,kotlin,role="secondary",attrs="-attributes"]
286+
----
287+
@Configuration
288+
class OAuth2LoginConfig {
289+
@Bean
290+
fun clientRegistrationRepository(): ClientRegistrationRepository {
291+
return InMemoryClientRegistrationRepository(googleClientRegistration())
292+
}
293+
294+
private fun googleClientRegistration(): ClientRegistration {
295+
return ClientRegistration.withRegistrationId("google")
296+
.clientId("google-client-id")
297+
.clientSecret("google-client-secret")
298+
.clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
299+
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
300+
.redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
301+
.scope("openid", "profile", "email", "address", "phone")
302+
.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
303+
.tokenUri("https://www.googleapis.com/oauth2/v4/token")
304+
.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
305+
.userNameAttributeName(IdTokenClaimNames.SUB)
306+
.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
307+
.clientName("Google")
308+
.build()
309+
}
310+
}
311+
----
312+
====
313+
282314

283315
[[oauth2login-provide-websecurityconfigureradapter]]
284316
==== Provide a WebSecurityConfigurerAdapter
@@ -856,14 +888,25 @@ You also need to ensure the `ClientRegistration.redirectUri` matches the custom
856888
857889
The following listing shows an example:
858890
859-
[source,java,attrs="-attributes"]
891+
.Java
892+
[source,java,role="primary",attrs="-attributes"]
860893
----
861894
return CommonOAuth2Provider.GOOGLE.getBuilder("google")
862895
.clientId("google-client-id")
863896
.clientSecret("google-client-secret")
864897
.redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
865898
.build();
866899
----
900+
901+
.Kotlin
902+
[source,kotlin,role="secondary",attrs="-attributes"]
903+
----
904+
return CommonOAuth2Provider.GOOGLE.getBuilder("google")
905+
.clientId("google-client-id")
906+
.clientSecret("google-client-secret")
907+
.redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
908+
.build()
909+
----
867910
====
868911

869912

@@ -1166,7 +1209,9 @@ It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error
11661209

11671210
Whether you customize `DefaultOAuth2UserService` or provide your own implementation of `OAuth2UserService`, you'll need to configure it as shown in the following example:
11681211

1169-
[source,java]
1212+
====
1213+
.Java
1214+
[source,java,role="primary"]
11701215
----
11711216
@EnableWebSecurity
11721217
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -1188,6 +1233,30 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
11881233
}
11891234
----
11901235
1236+
.Kotlin
1237+
[source,kotlin,role="secondary"]
1238+
----
1239+
@EnableWebSecurity
1240+
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
1241+
1242+
override fun configure(http: HttpSecurity) {
1243+
http {
1244+
oauth2Login {
1245+
userInfoEndpoint {
1246+
userService = oauth2UserService()
1247+
// ...
1248+
}
1249+
}
1250+
}
1251+
}
1252+
1253+
private fun oauth2UserService(): OAuth2UserService<OAuth2UserRequest, OAuth2User> {
1254+
// ...
1255+
}
1256+
}
1257+
----
1258+
====
1259+
11911260

11921261
[[oauth2login-advanced-oidc-user-service]]
11931262
===== OpenID Connect 1.0 UserService
@@ -1200,7 +1269,9 @@ If you need to customize the pre-processing of the UserInfo Request and/or the p
12001269

12011270
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:
12021271

1203-
[source,java]
1272+
====
1273+
.Java
1274+
[source,java,role="primary"]
12041275
----
12051276
@EnableWebSecurity
12061277
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -1222,6 +1293,30 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
12221293
}
12231294
----
12241295
1296+
.Kotlin
1297+
[source,kotlin,role="secondary"]
1298+
----
1299+
@EnableWebSecurity
1300+
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
1301+
1302+
override fun configure(http: HttpSecurity) {
1303+
http {
1304+
oauth2Login {
1305+
userInfoEndpoint {
1306+
oidcUserService = oidcUserService()
1307+
// ...
1308+
}
1309+
}
1310+
}
1311+
}
1312+
1313+
private fun oidcUserService(): OAuth2UserService<OidcUserRequest, OidcUser> {
1314+
// ...
1315+
}
1316+
}
1317+
----
1318+
====
1319+
12251320

12261321
[[oauth2login-advanced-idtoken-verify]]
12271322
==== ID Token Signature Verification
@@ -1237,7 +1332,9 @@ The JWS algorithm resolver is a `Function` that accepts a `ClientRegistration` a
12371332

12381333
The following code shows how to configure the `OidcIdTokenDecoderFactory` `@Bean` to default to `MacAlgorithm.HS256` for all `ClientRegistration`:
12391334

1240-
[source,java]
1335+
====
1336+
.Java
1337+
[source,java,role="primary"]
12411338
----
12421339
@Bean
12431340
public JwtDecoderFactory<ClientRegistration> idTokenDecoderFactory() {
@@ -1247,6 +1344,18 @@ public JwtDecoderFactory<ClientRegistration> idTokenDecoderFactory() {
12471344
}
12481345
----
12491346
1347+
.Kotlin
1348+
[source,kotlin,role="secondary"]
1349+
----
1350+
@Bean
1351+
fun idTokenDecoderFactory(): JwtDecoderFactory<ClientRegistration?> {
1352+
val idTokenDecoderFactory = OidcIdTokenDecoderFactory()
1353+
idTokenDecoderFactory.setJwsAlgorithmResolver { MacAlgorithm.HS256 }
1354+
return idTokenDecoderFactory
1355+
}
1356+
----
1357+
====
1358+
12501359
[NOTE]
12511360
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.
12521361

@@ -1281,7 +1390,9 @@ spring:
12811390

12821391
...and the `OidcClientInitiatedLogoutSuccessHandler`, which implements RP-Initiated Logout, may be configured as follows:
12831392

1284-
[source,java]
1393+
====
1394+
.Java
1395+
[source,java,role="primary"]
12851396
----
12861397
@EnableWebSecurity
12871398
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -1316,3 +1427,38 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
13161427
NOTE: `OidcClientInitiatedLogoutSuccessHandler` supports the `{baseUrl}` placeholder.
13171428
If used, the application's base URL, like `https://app.example.org`, will replace it at request time.
13181429
----
1430+
1431+
.Kotlin
1432+
[source,kotlin,role="secondary"]
1433+
----
1434+
@EnableWebSecurity
1435+
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
1436+
@Autowired
1437+
private lateinit var clientRegistrationRepository: ClientRegistrationRepository
1438+
1439+
override fun configure(http: HttpSecurity) {
1440+
http {
1441+
authorizeRequests {
1442+
authorize(anyRequest, authenticated)
1443+
}
1444+
oauth2Login { }
1445+
logout {
1446+
logoutSuccessHandler = oidcLogoutSuccessHandler()
1447+
}
1448+
}
1449+
}
1450+
1451+
private fun oidcLogoutSuccessHandler(): LogoutSuccessHandler {
1452+
val oidcLogoutSuccessHandler = OidcClientInitiatedLogoutSuccessHandler(clientRegistrationRepository)
1453+
1454+
// Sets the location that the End-User's User Agent will be redirected to
1455+
// after the logout has been performed at the Provider
1456+
oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}")
1457+
return oidcLogoutSuccessHandler
1458+
}
1459+
}
1460+
1461+
NOTE: `OidcClientInitiatedLogoutSuccessHandler` supports the `{baseUrl}` placeholder.
1462+
If used, the application's base URL, like `https://app.example.org`, will replace it at request time.
1463+
----
1464+
====

0 commit comments

Comments
 (0)