mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-31 17:22:13 +00:00
Kotlin OAuth2 client WebFlux samples
Issue gh-8172
This commit is contained in:
parent
79dca94ce1
commit
31d5b5068c
@ -21,7 +21,10 @@ You will need to replace the `client-id` and `client-secret` with values registe
|
|||||||
|
|
||||||
The next step is to instruct Spring Security that you wish to act as an OAuth2 Client so that you can obtain an access token.
|
The next step is to instruct Spring Security that you wish to act as an OAuth2 Client so that you can obtain an access token.
|
||||||
|
|
||||||
[source,java]
|
.OAuth2 Client
|
||||||
|
====
|
||||||
|
.Java
|
||||||
|
[source,java,role="primary"]
|
||||||
----
|
----
|
||||||
@Bean
|
@Bean
|
||||||
SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
|
SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
|
||||||
@ -32,4 +35,18 @@ SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
|
|||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|
||||||
|
.Kotlin
|
||||||
|
[source,kotlin,role="secondary"]
|
||||||
|
----
|
||||||
|
@Bean
|
||||||
|
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
|
||||||
|
return http {
|
||||||
|
// ...
|
||||||
|
oauth2Client { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
====
|
||||||
|
|
||||||
You can now leverage Spring Security's <<webclient>> or <<webflux-roac,@RegisteredOAuth2AuthorizedClient>> support to obtain and use the access token.
|
You can now leverage Spring Security's <<webclient>> or <<webflux-roac,@RegisteredOAuth2AuthorizedClient>> support to obtain and use the access token.
|
||||||
|
@ -122,7 +122,10 @@ The `client-id` and `client-secret` are linked to the provider because `keycloak
|
|||||||
|
|
||||||
A minimal OAuth2 Login configuration is shown below:
|
A minimal OAuth2 Login configuration is shown below:
|
||||||
|
|
||||||
[source,java]
|
.Minimal OAuth2 Login
|
||||||
|
====
|
||||||
|
.Java
|
||||||
|
[source,java,role="primary"]
|
||||||
----
|
----
|
||||||
@Bean
|
@Bean
|
||||||
ReactiveClientRegistrationRepository clientRegistrations() {
|
ReactiveClientRegistrationRepository clientRegistrations() {
|
||||||
@ -143,9 +146,34 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
|||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
.Kotlin
|
||||||
|
[source,kotlin,role="secondary"]
|
||||||
|
----
|
||||||
|
@Bean
|
||||||
|
fun clientRegistrations(): ReactiveClientRegistrationRepository {
|
||||||
|
val clientRegistration: ClientRegistration = ClientRegistrations
|
||||||
|
.fromIssuerLocation("https://idp.example.com/auth/realms/demo")
|
||||||
|
.clientId("spring-security")
|
||||||
|
.clientSecret("6cea952f-10d0-4d00-ac79-cc865820dc2c")
|
||||||
|
.build()
|
||||||
|
return InMemoryReactiveClientRegistrationRepository(clientRegistration)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
|
||||||
|
return http {
|
||||||
|
oauth2Login { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
====
|
||||||
|
|
||||||
Additional configuration options can be seen below:
|
Additional configuration options can be seen below:
|
||||||
|
|
||||||
[source,java]
|
.Advanced OAuth2 Login
|
||||||
|
====
|
||||||
|
.Java
|
||||||
|
[source,java,role="primary"]
|
||||||
----
|
----
|
||||||
@Bean
|
@Bean
|
||||||
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||||
@ -161,9 +189,29 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
|||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
.Kotlin
|
||||||
|
[source,kotlin,role="secondary"]
|
||||||
|
----
|
||||||
|
@Bean
|
||||||
|
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
|
||||||
|
return http {
|
||||||
|
oauth2Login {
|
||||||
|
authenticationConverter = converter
|
||||||
|
authenticationManager = manager
|
||||||
|
authorizedClientRepository = authorizedClients
|
||||||
|
clientRegistrationRepository = clientRegistration
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
====
|
||||||
|
|
||||||
You may register a `GrantedAuthoritiesMapper` `@Bean` to have it automatically applied to the default configuration, as shown in the following example:
|
You may register a `GrantedAuthoritiesMapper` `@Bean` to have it automatically applied to the default configuration, as shown in the following example:
|
||||||
|
|
||||||
[source,java]
|
.GrantedAuthoritiesMapper Bean
|
||||||
|
====
|
||||||
|
.Java
|
||||||
|
[source,java,role="primary"]
|
||||||
----
|
----
|
||||||
@Bean
|
@Bean
|
||||||
public GrantedAuthoritiesMapper userAuthoritiesMapper() {
|
public GrantedAuthoritiesMapper userAuthoritiesMapper() {
|
||||||
@ -178,3 +226,20 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
|||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
.Kotlin
|
||||||
|
[source,kotlin,role="secondary"]
|
||||||
|
----
|
||||||
|
@Bean
|
||||||
|
fun userAuthoritiesMapper(): GrantedAuthoritiesMapper {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
|
||||||
|
return http {
|
||||||
|
oauth2Login { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
====
|
||||||
|
Loading…
x
Reference in New Issue
Block a user