Skip to content

Commit d3bed13

Browse files
author
eugenp
committed
security work for new scenario
1 parent 03010d8 commit d3bed13

5 files changed

Lines changed: 102 additions & 44 deletions

File tree

‎spring-security-oauth/spring-security-oauth-resource-demo/pom.xml‎

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
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-
<artifactId>spring-security-oauth-resource-demo</artifactId>
5-
<name>spring-security-oauth-resource-demo</name>
6-
<packaging>war</packaging>
7-
8-
<parent>
9-
<groupId>org.baeldung</groupId>
10-
<artifactId>spring-security-oauth</artifactId>
11-
<version>1.0.0-SNAPSHOT</version>
12-
</parent>
13-
14-
<dependencies>
15-
16-
<dependency>
17-
<groupId>org.springframework.boot</groupId>
18-
<artifactId>spring-boot-starter-web</artifactId>
19-
</dependency>
20-
21-
<!-- oauth -->
22-
<dependency>
23-
<groupId>org.springframework.security.oauth</groupId>
24-
<artifactId>spring-security-oauth2</artifactId>
25-
<version>${oauth.version}</version>
26-
</dependency>
27-
28-
<dependency>
29-
<groupId>org.apache.commons</groupId>
30-
<artifactId>commons-lang3</artifactId>
31-
<version>${commons-lang3.version}</version>
32-
</dependency>
33-
34-
</dependencies>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<artifactId>spring-security-oauth-resource-demo</artifactId>
4+
<name>spring-security-oauth-resource-demo</name>
5+
<packaging>war</packaging>
6+
7+
<parent>
8+
<groupId>org.baeldung</groupId>
9+
<artifactId>spring-security-oauth</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<dependencies>
14+
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
20+
<!-- oauth -->
21+
<dependency>
22+
<groupId>org.springframework.security.oauth</groupId>
23+
<artifactId>spring-security-oauth2</artifactId>
24+
<version>${oauth.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.security</groupId>
28+
<artifactId>spring-security-jwt</artifactId>
29+
</dependency>
30+
31+
<!-- utils -->
32+
<dependency>
33+
<groupId>org.apache.commons</groupId>
34+
<artifactId>commons-lang3</artifactId>
35+
<version>${commons-lang3.version}</version>
36+
</dependency>
37+
38+
</dependencies>
3539

3640
<build>
3741
<finalName>spring-security-oauth-resource-demo</finalName>

‎spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/OAuth2ResourceServerConfig2.java‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Primary;
56
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
67
import org.springframework.security.config.http.SessionCreationPolicy;
78
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
89
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
10+
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
11+
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
912
import org.springframework.security.oauth2.provider.token.TokenStore;
10-
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
13+
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
14+
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
1115

1216
@Configuration
1317
@EnableResourceServer
@@ -25,9 +29,31 @@ public void configure(final HttpSecurity http) throws Exception {
2529
// @formatter:on
2630
}
2731

32+
@Override
33+
public void configure(final ResourceServerSecurityConfigurer config) {
34+
config.tokenServices(tokenServices());
35+
}
36+
37+
// JWT
38+
39+
@Bean
40+
@Primary
41+
public DefaultTokenServices tokenServices() {
42+
final DefaultTokenServices tokenServices = new DefaultTokenServices();
43+
tokenServices.setTokenStore(tokenStore());
44+
return tokenServices;
45+
}
46+
2847
@Bean
2948
public TokenStore tokenStore() {
30-
return new InMemoryTokenStore();
49+
return new JwtTokenStore(accessTokenConverter());
50+
}
51+
52+
@Bean
53+
public JwtAccessTokenConverter accessTokenConverter() {
54+
final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
55+
converter.setSigningKey("123");
56+
return converter;
3157
}
3258

3359
}

‎spring-security-oauth/spring-security-oauth-server-demo/pom.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<artifactId>spring-security-oauth2</artifactId>
2525
<version>${oauth.version}</version>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.security</groupId>
29+
<artifactId>spring-security-jwt</artifactId>
30+
</dependency>
2731

2832
</dependencies>
2933

‎spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/OAuth2AuthorizationServerConfig1.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ public void configure(final ClientDetailsServiceConfigurer clients) throws Excep
3131
clients.inMemory()
3232
.withClient("fooClientIdPassword")
3333
.secret("secret")
34-
.authorizedGrantTypes("password", "authorization_code", "refresh_token")
34+
.authorizedGrantTypes("password", "authorization_code")
3535
.scopes("foo", "read", "write")
3636
.accessTokenValiditySeconds(3600) // 1 hour
37-
.refreshTokenValiditySeconds(2592000) // 30 days
3837
;
3938
} // @formatter:on
4039

‎spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/OAuth2AuthorizationServerConfig2.java‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
import org.springframework.beans.factory.annotation.Qualifier;
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Primary;
8+
import org.springframework.http.HttpMethod;
79
import org.springframework.security.authentication.AuthenticationManager;
810
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
911
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
1012
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
1113
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
1214
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
15+
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
1316
import org.springframework.security.oauth2.provider.token.TokenStore;
14-
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
17+
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
18+
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
1519

1620
@Configuration
1721
@EnableAuthorizationServer
@@ -33,21 +37,42 @@ public void configure(final ClientDetailsServiceConfigurer clients) throws Excep
3337
clients.inMemory()
3438
.withClient("fooClientIdPassword")
3539
.secret("secret")
36-
.authorizedGrantTypes("password", "authorization_code", "refresh_token")
40+
.authorizedGrantTypes("password", "authorization_code" )
3741
.scopes("foo", "read", "write")
3842
.accessTokenValiditySeconds(3600) // 1 hour
39-
.refreshTokenValiditySeconds(2592000) // 30 days
4043
;
4144
} // @formatter:on
4245

4346
@Override
44-
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
45-
endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager);
47+
public void configure(final AuthorizationServerEndpointsConfigurer conf) { // @formatter:off
48+
conf.
49+
tokenStore(tokenStore())
50+
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
51+
.accessTokenConverter(accessTokenConverter())
52+
.authenticationManager(authenticationManager)
53+
;
54+
} // @formatter:on
55+
56+
// JWT
57+
58+
@Bean
59+
@Primary
60+
public DefaultTokenServices tokenServices() {
61+
final DefaultTokenServices tokenServices = new DefaultTokenServices();
62+
tokenServices.setTokenStore(tokenStore());
63+
return tokenServices;
4664
}
4765

4866
@Bean
4967
public TokenStore tokenStore() {
50-
return new InMemoryTokenStore();
68+
return new JwtTokenStore(accessTokenConverter());
69+
}
70+
71+
@Bean
72+
public JwtAccessTokenConverter accessTokenConverter() {
73+
final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
74+
converter.setSigningKey("123");
75+
return converter;
5176
}
5277

5378
}

0 commit comments

Comments
 (0)