Skip to content

Commit 7213541

Browse files
committed
[BAEL-9245] Copied over the existing oauth server codebase
The solution will be similar to this previous article, so it is good to use it as a base for the redis specific approach
1 parent a0c0521 commit 7213541

File tree

17 files changed

+474
-2
lines changed

17 files changed

+474
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<description>Spring Security OAuth Authorization Server Implemented With Redis</description>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>oauth-authorization-server-with-redis</artifactId>
8+
<version>0.1.0-SNAPSHOT</version>
9+
<packaging>pom</packaging>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>3.5.0</version>
15+
</parent>
16+
17+
<modules>
18+
<module>redis-authorization-server</module>
19+
<module>redis-client-server</module>
20+
<module>redis-resource-server</module>
21+
</modules>
22+
23+
<properties>
24+
<java.version>17</java.version>
25+
</properties>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-dependency-plugin</artifactId>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
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+
<artifactId>redis-authorization-server</artifactId>
6+
<name>redis-authorization-server</name>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>oauth-authorization-server-with-redis</artifactId>
12+
<version>0.1.0-SNAPSHOT</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-security</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-oauth2-authorization-server</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-test</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>io.rest-assured</groupId>
36+
<artifactId>rest-assured</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class OAuth2AuthorizationServerApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(OAuth2AuthorizationServerApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.config;
2+
3+
import static org.springframework.security.config.Customizer.withDefaults;
4+
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.core.annotation.Order;
8+
import org.springframework.security.config.Customizer;
9+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
10+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
11+
import org.springframework.security.core.userdetails.User;
12+
import org.springframework.security.core.userdetails.UserDetails;
13+
import org.springframework.security.core.userdetails.UserDetailsService;
14+
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
15+
import org.springframework.security.crypto.password.PasswordEncoder;
16+
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer;
17+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
18+
import org.springframework.security.web.SecurityFilterChain;
19+
20+
@Configuration
21+
@EnableWebSecurity
22+
public class SecurityConfig {
23+
24+
@Bean
25+
@Order(1)
26+
SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
27+
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer = OAuth2AuthorizationServerConfigurer.authorizationServer()
28+
.oidc(Customizer.withDefaults());
29+
30+
http.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
31+
.with(authorizationServerConfigurer, Customizer.withDefaults())
32+
.authorizeHttpRequests((authorize) -> authorize.anyRequest()
33+
.authenticated());
34+
35+
return http.formLogin(withDefaults())
36+
.build();
37+
}
38+
39+
@Bean
40+
@Order(2)
41+
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
42+
http.authorizeHttpRequests(authorizeRequests -> authorizeRequests.anyRequest()
43+
.authenticated())
44+
.formLogin(withDefaults());
45+
46+
return http.build();
47+
}
48+
49+
@Bean
50+
UserDetailsService users() {
51+
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
52+
UserDetails user = User.builder()
53+
.username("admin")
54+
.password("password")
55+
.passwordEncoder(encoder::encode)
56+
.roles("USER")
57+
.build();
58+
59+
return new InMemoryUserDetailsManager(user);
60+
}
61+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
server:
2+
port: 9000
3+
4+
logging:
5+
level:
6+
root: INFO
7+
org.springframework.web: INFO
8+
org.springframework.security: INFO
9+
org.springframework.security.oauth2: INFO
10+
11+
spring:
12+
security:
13+
oauth2:
14+
authorizationserver:
15+
issuer: http://auth-server:9000
16+
client:
17+
articles-client:
18+
registration:
19+
client-id: articles-client
20+
client-secret: "{noop}secret"
21+
client-name: Articles Client
22+
client-authentication-methods:
23+
- client_secret_basic
24+
authorization-grant-types:
25+
- authorization_code
26+
- refresh_token
27+
redirect-uris:
28+
- http://127.0.0.1:8080/login/oauth2/code/articles-client-oidc
29+
- http://127.0.0.1:8080/authorized
30+
scopes:
31+
- openid
32+
- articles.read
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
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+
<artifactId>redis-client-server</artifactId>
6+
<name>redis-client-server</name>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>oauth-authorization-server-with-redis</artifactId>
12+
<version>0.1.0-SNAPSHOT</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-security</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-oauth2-client</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-webflux</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>io.projectreactor.netty</groupId>
34+
<artifactId>reactor-netty</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>io.rest-assured</groupId>
44+
<artifactId>rest-assured</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class OAuth2ClientApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(OAuth2ClientApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
6+
import org.springframework.security.web.SecurityFilterChain;
7+
8+
import static org.springframework.security.config.Customizer.withDefaults;
9+
10+
@EnableWebSecurity
11+
public class SecurityConfig {
12+
13+
@Bean
14+
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
15+
http
16+
.authorizeHttpRequests(authorizeRequests ->
17+
authorizeRequests.anyRequest().authenticated()
18+
)
19+
.oauth2Login(oauth2Login ->
20+
oauth2Login.loginPage("/oauth2/authorization/articles-client-oidc"))
21+
.oauth2Client(withDefaults());
22+
return http.build();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
6+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
7+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
8+
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
9+
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager;
10+
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
11+
import org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction;
12+
import org.springframework.web.reactive.function.client.WebClient;
13+
14+
@Configuration
15+
public class WebClientConfig {
16+
17+
@Bean
18+
WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
19+
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
20+
return WebClient.builder()
21+
.apply(oauth2Client.oauth2Configuration())
22+
.build();
23+
}
24+
25+
@Bean
26+
OAuth2AuthorizedClientManager authorizedClientManager(ClientRegistrationRepository clientRegistrationRepository,
27+
OAuth2AuthorizedClientRepository authorizedClientRepository) {
28+
OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
29+
.authorizationCode()
30+
.refreshToken()
31+
.build();
32+
DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(clientRegistrationRepository,
33+
authorizedClientRepository);
34+
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
35+
36+
return authorizedClientManager;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.web;
2+
3+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
4+
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
import org.springframework.web.reactive.function.client.WebClient;
8+
9+
import static org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;
10+
11+
@RestController
12+
public class ArticlesController {
13+
14+
private final WebClient webClient;
15+
16+
public ArticlesController(WebClient webClient) {
17+
this.webClient = webClient;
18+
}
19+
20+
@GetMapping(value = "/articles")
21+
public String[] getArticles(@RegisteredOAuth2AuthorizedClient("articles-client-authorization-code") OAuth2AuthorizedClient authorizedClient) {
22+
return this.webClient.get()
23+
.uri("http://127.0.0.1:8090/articles")
24+
.attributes(oauth2AuthorizedClient(authorizedClient))
25+
.retrieve()
26+
.bodyToMono(String[].class)
27+
.block();
28+
}
29+
}

0 commit comments

Comments
 (0)