Skip to content

Commit 1ba7fee

Browse files
authored
Merge pull request eugenp#5604 from Doha2012/master
webflux oauth
2 parents f2d3c04 + fcd0198 commit 1ba7fee

8 files changed

Lines changed: 239 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@
468468
<module>spring-5-reactive-client</module>
469469
<module>spring-5-mvc</module>
470470
<module>spring-5-security</module>
471+
<module>spring-5-reactive-oauth</module>
471472
<module>spring-activiti</module>
472473
<module>spring-akka</module>
473474
<module>spring-amqp</module>

‎spring-5-reactive-oauth/pom.xml‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung.reactive.oauth</groupId>
7+
<artifactId>spring-5-reactive-oauth</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-5-reactive-oauth</name>
12+
<description>WebFluc and Spring Security OAuth </description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.1.0.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-security</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-webflux</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.security</groupId>
39+
<artifactId>spring-security-oauth2-client</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>io.projectreactor</groupId>
49+
<artifactId>reactor-test</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.security</groupId>
54+
<artifactId>spring-security-test</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<plugins>
61+
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-surefire-plugin</artifactId>
65+
<version>2.20.1</version>
66+
<configuration>
67+
<testFailureIgnore>true</testFailureIgnore>
68+
<excludes>
69+
<exclude>**/*IntegrationTest.java</exclude>
70+
</excludes>
71+
</configuration>
72+
</plugin>
73+
74+
</plugins>
75+
</build>
76+
77+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.reactive.oauth;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
5+
import org.springframework.security.config.web.server.ServerHttpSecurity;
6+
import org.springframework.security.web.server.SecurityWebFilterChain;
7+
8+
@EnableWebFluxSecurity
9+
public class SecurityConfig {
10+
11+
@Bean
12+
public SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
13+
return http.authorizeExchange()
14+
.pathMatchers("/about").permitAll()
15+
.anyExchange().authenticated()
16+
.and().oauth2Login()
17+
.and().build();
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.reactive.oauth;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
7+
import org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction;
8+
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
9+
import org.springframework.web.reactive.function.client.WebClient;
10+
11+
@SpringBootApplication
12+
public class Spring5ReactiveOauthApplication {
13+
14+
public static void main(String[] args) {
15+
SpringApplication.run(Spring5ReactiveOauthApplication.class, args);
16+
}
17+
18+
@Bean
19+
public WebClient webClient(ReactiveClientRegistrationRepository clientRegistrationRepo, ServerOAuth2AuthorizedClientRepository authorizedClientRepo) {
20+
ServerOAuth2AuthorizedClientExchangeFilterFunction filter = new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrationRepo, authorizedClientRepo);
21+
return WebClient.builder()
22+
.filter(filter)
23+
.build();
24+
}
25+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.reactive.oauth.web;
2+
3+
import static org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
7+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
8+
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
9+
import org.springframework.security.oauth2.core.user.OAuth2User;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RestController;
13+
import org.springframework.web.reactive.function.client.WebClient;
14+
15+
import reactor.core.publisher.Mono;
16+
17+
import com.baeldung.reactive.oauth.web.dto.Foo;
18+
19+
@RestController
20+
public class MainController {
21+
22+
@Autowired
23+
private WebClient webClient;
24+
25+
@GetMapping("/")
26+
public Mono<String> index(@AuthenticationPrincipal Mono<OAuth2User> oauth2User) {
27+
return oauth2User
28+
.map(OAuth2User::getName)
29+
.map(name -> String.format("Hi, %s", name));
30+
}
31+
32+
@GetMapping("/foos/{id}")
33+
public Mono<Foo> getFooResource(@RegisteredOAuth2AuthorizedClient("custom") OAuth2AuthorizedClient client, @PathVariable final long id){
34+
return webClient
35+
.get()
36+
.uri("http://localhost:8088/spring-security-oauth-resource/foos/{id}", id)
37+
.attributes(oauth2AuthorizedClient(client))
38+
.retrieve()
39+
.bodyToMono(Foo.class);
40+
}
41+
42+
@GetMapping("/about")
43+
public String getAboutPage() {
44+
return "WebFlux OAuth example";
45+
}
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.reactive.oauth.web.dto;
2+
3+
public class Foo {
4+
private long id;
5+
private String name;
6+
7+
public Foo() {
8+
super();
9+
}
10+
11+
public Foo(final long id, final String name) {
12+
super();
13+
14+
this.id = id;
15+
this.name = name;
16+
}
17+
18+
//
19+
20+
public long getId() {
21+
return id;
22+
}
23+
24+
public void setId(final long id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(final String name) {
33+
this.name = name;
34+
}
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
spring:
2+
security:
3+
oauth2:
4+
client:
5+
registration:
6+
google:
7+
client-id: YOUR_APP_CLIENT_ID
8+
client-secret: YOUR_APP_CLIENT_SECRET
9+
custom:
10+
client-id: fooClientIdPassword
11+
client-secret: secret
12+
scopes: read,foo
13+
authorization-grant-type: authorization_code
14+
redirect-uri-template: http://localhost:8080/login/oauth2/code/custom
15+
provider:
16+
custom:
17+
authorization-uri: http://localhost:8081/spring-security-oauth-server/oauth/authorize
18+
token-uri: http://localhost:8081/spring-security-oauth-server/oauth/token
19+
user-info-uri: http://localhost:8088/spring-security-oauth-resource/users/extra
20+
user-name-attribute: user_name
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.reactive.oauth;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class Spring5ReactiveOauthIntegrationTest {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)