Skip to content

Commit 603eb1e

Browse files
committed
Add OAuth2 Kotlin samples to docs
Issue: gh-5558
1 parent b4cab71 commit 603eb1e

File tree

3 files changed

+622
-22
lines changed

3 files changed

+622
-22
lines changed

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

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ In addition, `HttpSecurity.oauth2Client().authorizationCodeGrant()` enables the
1919

2020
The following code shows the complete configuration options provided by the `HttpSecurity.oauth2Client()` DSL:
2121

22-
[source,java]
22+
.OAuth2 Client Configuration Options
23+
====
24+
.Java
25+
[source,java,role="primary"]
2326
----
2427
@EnableWebSecurity
2528
public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -41,6 +44,30 @@ public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
4144
}
4245
----
4346
47+
.Kotlin
48+
[source,kotlin,role="secondary"]
49+
----
50+
@EnableWebSecurity
51+
class OAuth2ClientSecurityConfig : WebSecurityConfigurerAdapter() {
52+
53+
override fun configure(http: HttpSecurity) {
54+
http {
55+
oauth2Client {
56+
clientRegistrationRepository = clientRegistrationRepository()
57+
authorizedClientRepository = authorizedClientRepository()
58+
authorizedClientService = authorizedClientService()
59+
authorizationCodeGrant {
60+
authorizationRequestRepository = authorizationRequestRepository()
61+
authorizationRequestResolver = authorizationRequestResolver()
62+
accessTokenResponseClient = accessTokenResponseClient()
63+
}
64+
}
65+
}
66+
}
67+
}
68+
----
69+
====
70+
4471
The `OAuth2AuthorizedClientManager` is responsible for managing the authorization (or re-authorization) of an OAuth 2.0 Client, in collaboration with one or more `OAuth2AuthorizedClientProvider`(s).
4572

4673
The following code shows an example of how to register an `OAuth2AuthorizedClientManager` `@Bean` and associate it with an `OAuth2AuthorizedClientProvider` composite that provides support for the `authorization_code`, `refresh_token`, `client_credentials` and `password` authorization grant types:
@@ -583,7 +610,10 @@ The default implementation of `AuthorizationRequestRepository` is `HttpSessionOA
583610

584611
If you have a custom implementation of `AuthorizationRequestRepository`, you may configure it as shown in the following example:
585612

586-
[source,java]
613+
.AuthorizationRequestRepository Configuration
614+
====
615+
.Java
616+
[source,java,role="primary"]
587617
----
588618
@EnableWebSecurity
589619
public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -601,6 +631,25 @@ public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
601631
}
602632
----
603633
634+
.Kotlin
635+
[source,kotlin,role="secondary"]
636+
----
637+
@EnableWebSecurity
638+
class OAuth2ClientSecurityConfig : WebSecurityConfigurerAdapter() {
639+
640+
override fun configure(http: HttpSecurity) {
641+
http {
642+
oauth2Client {
643+
authorizationCodeGrant {
644+
authorizationRequestRepository = authorizationRequestRepository()
645+
}
646+
}
647+
}
648+
}
649+
}
650+
----
651+
====
652+
604653

605654
===== Requesting an Access Token
606655

@@ -645,7 +694,10 @@ It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error
645694

646695
Whether you customize `DefaultAuthorizationCodeTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you'll need to configure it as shown in the following example:
647696

648-
[source,java]
697+
.Access Token Response Configuration
698+
====
699+
.Java
700+
[source,java,role="primary"]
649701
----
650702
@EnableWebSecurity
651703
public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -663,6 +715,25 @@ public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
663715
}
664716
----
665717
718+
.Kotlin
719+
[source,kotlin,role="secondary"]
720+
----
721+
@EnableWebSecurity
722+
class OAuth2ClientSecurityConfig : WebSecurityConfigurerAdapter() {
723+
724+
override fun configure(http: HttpSecurity) {
725+
http {
726+
oauth2Client {
727+
authorizationCodeGrant {
728+
accessTokenResponseClient = accessTokenResponseClient()
729+
}
730+
}
731+
}
732+
}
733+
}
734+
----
735+
====
736+
666737

667738
[[oauth2Client-refresh-token-grant]]
668739
==== Refresh Token

0 commit comments

Comments
 (0)