Skip to content

Commit 4886367

Browse files
author
eugenp
committed
maven work and formatting
1 parent 453e617 commit 4886367

File tree

13 files changed

+129
-176
lines changed

13 files changed

+129
-176
lines changed

pom.xml

+9-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<parent>
1111
<groupId>org.springframework.boot</groupId>
1212
<artifactId>spring-boot-starter-parent</artifactId>
13-
<version>1.3.3.RELEASE</version>
13+
<version>1.4.0.RELEASE</version>
1414
<relativePath></relativePath>
1515
</parent>
1616

@@ -68,38 +68,35 @@
6868

6969
<properties>
7070
<!-- Spring -->
71-
<org.springframework.version>4.2.5.RELEASE</org.springframework.version>
72-
<org.springframework.security.version>4.0.4.RELEASE</org.springframework.security.version>
73-
<oauth.version>2.0.9.RELEASE</oauth.version>
7471

7572
<!-- marshalling -->
7673

77-
<jackson.version>2.7.0</jackson.version>
74+
<jackson.version>2.8.1</jackson.version>
7875

7976
<!-- logging -->
8077
<org.slf4j.version>1.7.12</org.slf4j.version>
8178
<logback.version>1.1.3</logback.version>
8279

8380
<!-- util -->
8481
<guava.version>19.0</guava.version>
85-
<commons-lang3.version>3.3.2</commons-lang3.version>
82+
<commons-lang3.version>3.4</commons-lang3.version>
8683
<commons-io.version>2.4</commons-io.version>
8784

8885
<!-- testing -->
8986
<org.hamcrest.version>1.3</org.hamcrest.version>
90-
<junit.version>4.11</junit.version>
87+
<junit.version>4.12</junit.version>
9188
<mockito.version>1.10.19</mockito.version>
9289

93-
<httpcore.version>4.4</httpcore.version>
94-
<httpclient.version>4.4</httpclient.version>
90+
<httpcore.version>4.4.5</httpcore.version>
91+
<httpclient.version>4.5.2</httpclient.version>
9592

9693
<rest-assured.version>2.4.0</rest-assured.version>
9794

9895
<!-- Maven plugins -->
99-
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
96+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
10097
<maven-war-plugin.version>2.6</maven-war-plugin.version>
101-
<maven-surefire-plugin.version>2.19</maven-surefire-plugin.version>
102-
<cargo-maven2-plugin.version>1.4.16</cargo-maven2-plugin.version>
98+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
99+
<cargo-maven2-plugin.version>1.4.19</cargo-maven2-plugin.version>
103100

104101
</properties>
105102

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

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<dependency>
2222
<groupId>org.springframework.security.oauth</groupId>
2323
<artifactId>spring-security-oauth2</artifactId>
24-
<version>${oauth.version}</version>
2524
</dependency>
2625
<dependency>
2726
<groupId>org.springframework.security</groupId>

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ public class OAuth2ResourceServerConfig1 extends ResourceServerConfigurerAdapter
1616
@Override
1717
public void configure(final HttpSecurity http) throws Exception {
1818
// @formatter:off
19-
http
20-
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
21-
.and().authorizeRequests().anyRequest().authenticated();
22-
;
23-
// @formatter:on
19+
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED).and().authorizeRequests()
20+
.anyRequest().authenticated();
21+
;
22+
// @formatter:on
2423
}
2524

2625
@Bean

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ public class OAuth2ResourceServerConfig2 extends ResourceServerConfigurerAdapter
2222
@Override
2323
public void configure(final HttpSecurity http) throws Exception {
2424
// @formatter:off
25-
http
26-
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
27-
.and().authorizeRequests().anyRequest().authenticated();
28-
;
29-
// @formatter:on
25+
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED).and().authorizeRequests()
26+
.anyRequest().authenticated();
27+
;
28+
// @formatter:on
3029
}
3130

3231
@Override

spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/controller/BarController.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public Bar findById(@PathVariable final long id) {
2929
}
3030

3131
// API - write
32-
// @PreAuthorize("#oauth2.hasScope('bar') and #oauth2.hasScope('write') and hasRole('ROLE_ADMIN')")
32+
// @PreAuthorize("#oauth2.hasScope('bar') and #oauth2.hasScope('write') and
33+
// hasRole('ROLE_ADMIN')")
3334
@RequestMapping(method = RequestMethod.POST, value = "/bars")
3435
@ResponseStatus(HttpStatus.CREATED)
3536
@ResponseBody

spring-security-oauth-resource/src/main/java/org/baeldung/config/OAuth2ResourceServerConfig.java

+26-27
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,21 @@ public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter
3434
@Override
3535
public void configure(final HttpSecurity http) throws Exception {
3636
// @formatter:off
37-
http
38-
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
39-
.and().authorizeRequests().anyRequest().authenticated();
40-
// .requestMatchers().antMatchers("/foos/**","/bars/**")
41-
// .and()
42-
// .authorizeRequests()
43-
// .antMatchers(HttpMethod.GET,"/foos/**").access("#oauth2.hasScope('foo') and #oauth2.hasScope('read')")
44-
// .antMatchers(HttpMethod.POST,"/foos/**").access("#oauth2.hasScope('foo') and #oauth2.hasScope('write')")
45-
// .antMatchers(HttpMethod.GET,"/bars/**").access("#oauth2.hasScope('bar') and #oauth2.hasScope('read')")
46-
// .antMatchers(HttpMethod.POST,"/bars/**").access("#oauth2.hasScope('bar') and #oauth2.hasScope('write') and hasRole('ROLE_ADMIN')")
47-
;
48-
// @formatter:on
37+
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED).and().authorizeRequests()
38+
.anyRequest().authenticated();
39+
// .requestMatchers().antMatchers("/foos/**","/bars/**")
40+
// .and()
41+
// .authorizeRequests()
42+
// .antMatchers(HttpMethod.GET,"/foos/**").access("#oauth2.hasScope('foo')
43+
// and #oauth2.hasScope('read')")
44+
// .antMatchers(HttpMethod.POST,"/foos/**").access("#oauth2.hasScope('foo')
45+
// and #oauth2.hasScope('write')")
46+
// .antMatchers(HttpMethod.GET,"/bars/**").access("#oauth2.hasScope('bar')
47+
// and #oauth2.hasScope('read')")
48+
// .antMatchers(HttpMethod.POST,"/bars/**").access("#oauth2.hasScope('bar')
49+
// and #oauth2.hasScope('write') and hasRole('ROLE_ADMIN')")
50+
;
51+
// @formatter:on
4952
}
5053

5154
@Override
@@ -84,19 +87,15 @@ public DefaultTokenServices tokenServices() {
8487
// JDBC token store configuration
8588

8689
/*
87-
@Bean
88-
public DataSource dataSource() {
89-
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
90-
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
91-
dataSource.setUrl(env.getProperty("jdbc.url"));
92-
dataSource.setUsername(env.getProperty("jdbc.user"));
93-
dataSource.setPassword(env.getProperty("jdbc.pass"));
94-
return dataSource;
95-
}
96-
97-
@Bean
98-
public TokenStore tokenStore() {
99-
return new JdbcTokenStore(dataSource());
100-
}
101-
*/
90+
* @Bean public DataSource dataSource() { final DriverManagerDataSource
91+
* dataSource = new DriverManagerDataSource();
92+
* dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
93+
* dataSource.setUrl(env.getProperty("jdbc.url"));
94+
* dataSource.setUsername(env.getProperty("jdbc.user"));
95+
* dataSource.setPassword(env.getProperty("jdbc.pass")); return dataSource;
96+
* }
97+
*
98+
* @Bean public TokenStore tokenStore() { return new
99+
* JdbcTokenStore(dataSource()); }
100+
*/
102101
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
<dependency>
2323
<groupId>org.springframework.security.oauth</groupId>
2424
<artifactId>spring-security-oauth2</artifactId>
25-
<version>${oauth.version}</version>
2625
</dependency>
2726
<dependency>
2827
<groupId>org.springframework.security</groupId>

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@ public void configure(final AuthorizationServerSecurityConfigurer oauthServer) t
3030

3131
@Override
3232
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { // @formatter:off
33-
clients.inMemory()
34-
.withClient("fooClientIdPassword")
35-
.secret("secret")
36-
.authorizedGrantTypes("password", "authorization_code")
37-
.scopes("foo", "read", "write")
38-
.accessTokenValiditySeconds(3600) // 1 hour
39-
;
40-
} // @formatter:on
33+
clients.inMemory().withClient("fooClientIdPassword").secret("secret")
34+
.authorizedGrantTypes("password", "authorization_code").scopes("foo", "read", "write")
35+
.accessTokenValiditySeconds(3600) // 1 hour
36+
;
37+
} // @formatter:on
4138

4239
@Override
4340
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

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

+8-15
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,17 @@ public void configure(final AuthorizationServerSecurityConfigurer oauthServer) t
3232

3333
@Override
3434
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { // @formatter:off
35-
clients.inMemory()
36-
.withClient("fooClientIdPassword")
37-
.secret("secret")
38-
.authorizedGrantTypes("password", "authorization_code" )
39-
.scopes("foo", "read", "write")
40-
.accessTokenValiditySeconds(3600) // 1 hour
41-
;
42-
} // @formatter:on
35+
clients.inMemory().withClient("fooClientIdPassword").secret("secret")
36+
.authorizedGrantTypes("password", "authorization_code").scopes("foo", "read", "write")
37+
.accessTokenValiditySeconds(3600) // 1 hour
38+
;
39+
} // @formatter:on
4340

4441
@Override
4542
public void configure(final AuthorizationServerEndpointsConfigurer conf) { // @formatter:off
46-
conf.
47-
tokenStore(tokenStore())
48-
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
49-
.accessTokenConverter(accessTokenConverter())
50-
.authenticationManager(authenticationManager)
51-
;
52-
} // @formatter:on
43+
conf.tokenStore(tokenStore()).allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
44+
.accessTokenConverter(accessTokenConverter()).authenticationManager(authenticationManager);
45+
} // @formatter:on
5346

5447
// JWT
5548

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

+16-17
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,24 @@ public void configure(final AuthorizationServerSecurityConfigurer oauthServer) t
3232

3333
@Override
3434
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { // @formatter:off
35-
clients.inMemory()
36-
.withClient("fooClientIdPassword")
37-
.secret("secret")
38-
/*1*/.authorizedGrantTypes("password", "authorization_code", "refresh_token" )
39-
/*2*/.scopes("foo", "read", "write")
40-
.accessTokenValiditySeconds(30) // 30 seconds, so that it expires quickly
41-
/*3*/.refreshTokenValiditySeconds(2592000) // 30 days
42-
;
43-
} // @formatter:on
35+
clients.inMemory().withClient("fooClientIdPassword").secret("secret")
36+
/* 1 */.authorizedGrantTypes("password", "authorization_code", "refresh_token")
37+
/* 2 */.scopes("foo", "read", "write").accessTokenValiditySeconds(30) // 30
38+
// seconds,
39+
// so
40+
// that
41+
// it
42+
// expires
43+
// quickly
44+
/* 3 */.refreshTokenValiditySeconds(2592000) // 30 days
45+
;
46+
} // @formatter:on
4447

4548
@Override
4649
public void configure(final AuthorizationServerEndpointsConfigurer conf) { // @formatter:off
47-
conf.
48-
tokenStore(tokenStore())
49-
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
50-
.accessTokenConverter(accessTokenConverter())
51-
.authenticationManager(authenticationManager)
52-
;
53-
} // @formatter:on
50+
conf.tokenStore(tokenStore()).allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
51+
.accessTokenConverter(accessTokenConverter()).authenticationManager(authenticationManager);
52+
} // @formatter:on
5453

5554
// JWT
5655

@@ -59,7 +58,7 @@ public void configure(final AuthorizationServerEndpointsConfigurer conf) { // @f
5958
public DefaultTokenServices tokenServices() {
6059
final DefaultTokenServices tokenServices = new DefaultTokenServices();
6160
tokenServices.setTokenStore(tokenStore());
62-
/*4*/tokenServices.setSupportRefreshToken(true);
61+
/* 4 */tokenServices.setSupportRefreshToken(true);
6362
return tokenServices;
6463
}
6564

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

+6-11
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
1313

1414
@Autowired
1515
public void globalUserDetails(final AuthenticationManagerBuilder auth) throws Exception {// @formatter:off
16-
auth.inMemoryAuthentication().
17-
withUser("john").password("123").roles("USER").
18-
and().
19-
withUser("tom").password("111").roles("ADMIN");
20-
}// @formatter:on
16+
auth.inMemoryAuthentication().withUser("john").password("123").roles("USER").and().withUser("tom")
17+
.password("111").roles("ADMIN");
18+
}// @formatter:on
2119

2220
@Override
2321
@Bean
@@ -28,12 +26,9 @@ public AuthenticationManager authenticationManagerBean() throws Exception {
2826
@Override
2927
protected void configure(final HttpSecurity http) throws Exception {
3028
// @formatter:off
31-
http.authorizeRequests()
32-
.antMatchers("/login").permitAll()
33-
.anyRequest().authenticated()
34-
.and().formLogin().permitAll()
35-
;
36-
// @formatter:on
29+
http.authorizeRequests().antMatchers("/login").permitAll().anyRequest().authenticated().and().formLogin()
30+
.permitAll();
31+
// @formatter:on
3732
}
3833

3934
}

0 commit comments

Comments
 (0)