Skip to content

Commit 7c0a225

Browse files
eleftheriaskostya05983
authored andcommitted
Use http security nested builder in samples
Issue: spring-projectsgh-5557
1 parent d6a2851 commit 7c0a225

File tree

14 files changed

+224
-144
lines changed

14 files changed

+224
-144
lines changed

samples/boot/helloworld/src/main/java/org/springframework/security/samples/config/SecurityConfig.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,11 +32,16 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
3232
@Override
3333
protected void configure(HttpSecurity http) throws Exception {
3434
http
35-
.authorizeRequests()
36-
.antMatchers("/css/**", "/index").permitAll()
37-
.antMatchers("/user/**").hasRole("USER")
38-
.and()
39-
.formLogin().loginPage("/login").failureUrl("/login-error");
35+
.authorizeRequests(authorizeRequests ->
36+
authorizeRequests
37+
.antMatchers("/css/**", "/index").permitAll()
38+
.antMatchers("/user/**").hasRole("USER")
39+
)
40+
.formLogin(formLogin ->
41+
formLogin
42+
.loginPage("/login")
43+
.failureUrl("/login-error")
44+
);
4045
}
4146
// @formatter:on
4247

samples/boot/oauth2login/src/integration-test/java/org/springframework/security/samples/OAuth2LoginApplicationTests.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -358,15 +358,21 @@ public static class SecurityTestConfig extends WebSecurityConfigurerAdapter {
358358
@Override
359359
protected void configure(HttpSecurity http) throws Exception {
360360
http
361-
.authorizeRequests()
362-
.anyRequest().authenticated()
363-
.and()
364-
.oauth2Login()
365-
.tokenEndpoint()
366-
.accessTokenResponseClient(this.mockAccessTokenResponseClient())
367-
.and()
368-
.userInfoEndpoint()
369-
.userService(this.mockUserService());
361+
.authorizeRequests(authorizeRequests ->
362+
authorizeRequests
363+
.anyRequest().authenticated()
364+
)
365+
.oauth2Login(oauth2Login ->
366+
oauth2Login
367+
.tokenEndpoint(tokenEndpoint ->
368+
tokenEndpoint
369+
.accessTokenResponseClient(this.mockAccessTokenResponseClient())
370+
)
371+
.userInfoEndpoint(userInfoEndpoint ->
372+
userInfoEndpoint
373+
.userService(this.mockUserService())
374+
)
375+
);
370376
}
371377
// @formatter:on
372378

samples/boot/oauth2resourceserver-jwe/src/main/java/sample/OAuth2ResourceServerSecurityConfiguration.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,8 @@
4646
import org.springframework.security.oauth2.jwt.JwtDecoder;
4747
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
4848

49+
import static org.springframework.security.config.Customizer.withDefaults;
50+
4951
/**
5052
* @author Josh Cummings
5153
*/
@@ -66,12 +68,15 @@ public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfig
6668
protected void configure(HttpSecurity http) throws Exception {
6769
// @formatter:off
6870
http
69-
.authorizeRequests()
70-
.antMatchers("/message/**").hasAuthority("SCOPE_message:read")
71-
.anyRequest().authenticated()
72-
.and()
73-
.oauth2ResourceServer()
74-
.jwt();
71+
.authorizeRequests(authorizeRequests ->
72+
authorizeRequests
73+
.antMatchers("/message/**").hasAuthority("SCOPE_message:read")
74+
.anyRequest().authenticated()
75+
)
76+
.oauth2ResourceServer(oauth2ResourceServer ->
77+
oauth2ResourceServer
78+
.jwt(withDefaults())
79+
);
7580
// @formatter:on
7681
}
7782

samples/boot/oauth2resourceserver-multitenancy/src/main/java/sample/OAuth2ResourceServerSecurityConfiguration.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@ public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfig
5151
protected void configure(HttpSecurity http) throws Exception {
5252
// @formatter:off
5353
http
54-
.authorizeRequests()
55-
.antMatchers("/**/message/**").hasAuthority("SCOPE_message:read")
56-
.anyRequest().authenticated()
57-
.and()
58-
.oauth2ResourceServer()
59-
.authenticationManagerResolver(multitenantAuthenticationManager());
54+
.authorizeRequests(authorizeRequests ->
55+
authorizeRequests
56+
.antMatchers("/**/message/**").hasAuthority("SCOPE_message:read")
57+
.anyRequest().authenticated()
58+
)
59+
.oauth2ResourceServer(oauth2ResourceServer ->
60+
oauth2ResourceServer
61+
.authenticationManagerResolver(multitenantAuthenticationManager())
62+
);
6063
// @formatter:on
6164
}
6265

samples/boot/oauth2resourceserver-opaque/src/main/java/sample/OAuth2ResourceServerSecurityConfiguration.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfig
3434
protected void configure(HttpSecurity http) throws Exception {
3535
// @formatter:off
3636
http
37-
.authorizeRequests()
38-
.mvcMatchers("/message/**").hasAuthority("SCOPE_message:read")
39-
.anyRequest().authenticated()
40-
.and()
41-
.oauth2ResourceServer()
42-
.opaqueToken()
43-
.introspectionUri(this.introspectionUri)
44-
.introspectionClientCredentials(this.clientId, this.clientSecret);
37+
.authorizeRequests(authorizeRequests ->
38+
authorizeRequests
39+
.mvcMatchers("/message/**").hasAuthority("SCOPE_message:read")
40+
.anyRequest().authenticated()
41+
)
42+
.oauth2ResourceServer(oauth2ResourceServer ->
43+
oauth2ResourceServer
44+
.opaqueToken(opaqueToken ->
45+
opaqueToken
46+
.introspectionUri(this.introspectionUri)
47+
.introspectionClientCredentials(this.clientId, this.clientSecret)
48+
)
49+
);
4550
// @formatter:on
4651
}
4752
}

samples/boot/oauth2resourceserver-static/src/main/java/sample/OAuth2ResourceServerSecurityConfiguration.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfig
3838
protected void configure(HttpSecurity http) throws Exception {
3939
// @formatter:off
4040
http
41-
.authorizeRequests()
42-
.antMatchers("/message/**").hasAuthority("SCOPE_message:read")
43-
.anyRequest().authenticated()
44-
.and()
45-
.oauth2ResourceServer()
46-
.jwt()
47-
.decoder(jwtDecoder());
41+
.authorizeRequests(authorizeRequests ->
42+
authorizeRequests
43+
.antMatchers("/message/**").hasAuthority("SCOPE_message:read")
44+
.anyRequest().authenticated()
45+
)
46+
.oauth2ResourceServer(oauth2ResourceServer ->
47+
oauth2ResourceServer
48+
.jwt(jwt ->
49+
jwt.decoder(jwtDecoder())
50+
)
51+
);
4852
// @formatter:on
4953
}
5054

samples/boot/oauth2resourceserver/src/main/java/sample/OAuth2ResourceServerSecurityConfiguration.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
1919
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
2020
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2121

22+
import static org.springframework.security.config.Customizer.withDefaults;
23+
2224
/**
2325
* @author Josh Cummings
2426
*/
@@ -29,12 +31,15 @@ public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfig
2931
protected void configure(HttpSecurity http) throws Exception {
3032
// @formatter:off
3133
http
32-
.authorizeRequests()
33-
.antMatchers("/message/**").hasAuthority("SCOPE_message:read")
34-
.anyRequest().authenticated()
35-
.and()
36-
.oauth2ResourceServer()
37-
.jwt();
34+
.authorizeRequests(authorizeRequests ->
35+
authorizeRequests
36+
.antMatchers("/message/**").hasAuthority("SCOPE_message:read")
37+
.anyRequest().authenticated()
38+
)
39+
.oauth2ResourceServer(oauth2ResourceServer ->
40+
oauth2ResourceServer
41+
.jwt(withDefaults())
42+
);
3843
// @formatter:on
3944
}
4045
}

samples/boot/oauth2webclient/src/main/java/sample/config/SecurityConfig.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@
2424
import org.springframework.security.core.userdetails.UserDetailsService;
2525
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
2626

27+
import static org.springframework.security.config.Customizer.withDefaults;
28+
2729
/**
2830
* @author Joe Grandja
2931
*/
@@ -33,15 +35,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
3335
@Override
3436
protected void configure(HttpSecurity http) throws Exception {
3537
http
36-
.authorizeRequests()
37-
.mvcMatchers("/", "/public/**").permitAll()
38-
.anyRequest().authenticated()
39-
.and()
40-
.formLogin()
41-
.and()
42-
.oauth2Login()
43-
.and()
44-
.oauth2Client();
38+
.authorizeRequests(authorizeRequests ->
39+
authorizeRequests
40+
.mvcMatchers("/", "/public/**").permitAll()
41+
.anyRequest().authenticated()
42+
)
43+
.formLogin(withDefaults())
44+
.oauth2Login(withDefaults())
45+
.oauth2Client(withDefaults());
4546
}
4647

4748
@Bean

samples/javaconfig/concurrency/src/main/java/org/springframework/security/samples/config/SecurityConfig.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@
2222
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
2323
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2424

25+
import static org.springframework.security.config.Customizer.withDefaults;
26+
2527
@EnableWebSecurity
2628
@EnableGlobalMethodSecurity(prePostEnabled = true)
2729
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@@ -40,14 +42,19 @@ public void configureGlobal(
4042
protected void configure(
4143
HttpSecurity http) throws Exception {
4244
http
43-
.authorizeRequests()
44-
.anyRequest().authenticated()
45-
.and()
46-
.formLogin()
47-
.and()
48-
.sessionManagement()
49-
.maximumSessions(1)
50-
.expiredUrl("/login?expired");
45+
.authorizeRequests(authorizeRequests ->
46+
authorizeRequests
47+
.anyRequest().authenticated()
48+
)
49+
.formLogin(withDefaults())
50+
.sessionManagement(sessionManagement ->
51+
sessionManagement
52+
.sessionConcurrency(sessionConcurrency ->
53+
sessionConcurrency
54+
.maximumSessions(1)
55+
.expiredUrl("/login?expired")
56+
)
57+
);
5158
}
5259
// @formatter:on
5360
}

samples/javaconfig/form/src/main/java/org/springframework/security/samples/config/SecurityConfig.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,16 +29,20 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
2929
@Override
3030
protected void configure(HttpSecurity http) throws Exception {
3131
http
32-
.authorizeRequests()
33-
.antMatchers("/resources/**").permitAll()
34-
.anyRequest().authenticated()
35-
.and()
36-
.formLogin()
37-
.loginPage("/login")
38-
.permitAll()
39-
.and()
40-
.logout()
41-
.permitAll();
32+
.authorizeRequests(authorizeRequests ->
33+
authorizeRequests
34+
.antMatchers("/resources/**").permitAll()
35+
.anyRequest().authenticated()
36+
)
37+
.formLogin(formLogin ->
38+
formLogin
39+
.loginPage("/login")
40+
.permitAll()
41+
)
42+
.logout(logout ->
43+
logout
44+
.permitAll()
45+
);
4246
}
4347
// @formatter:on
4448

0 commit comments

Comments
 (0)