Use Spring Security lambda DSL in samples

Fixes: gh-1580
This commit is contained in:
Eleftheria Stein
2020-02-19 12:36:51 +01:00
parent 1a07ba5114
commit f13eb8d73e
9 changed files with 47 additions and 36 deletions

View File

@@ -19,6 +19,7 @@ package docs.security;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@@ -41,14 +42,16 @@ public class RememberMeSecurityConfiguration extends WebSecurityConfigurerAdapte
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
// ... additional configuration ... // ... additional configuration ...
.rememberMe() .rememberMe((rememberMe) -> rememberMe
.rememberMeServices(rememberMeServices()); .rememberMeServices(rememberMeServices())
);
// end::http-rememberme[] // end::http-rememberme[]
http http
.formLogin().and() .formLogin(Customizer.withDefaults())
.authorizeRequests() .authorizeRequests((authorize) -> authorize
.anyRequest().authenticated(); .anyRequest().authenticated()
);
} }
// tag::rememberme-bean[] // tag::rememberme-bean[]

View File

@@ -40,9 +40,10 @@ public class SecurityConfiguration<S extends Session> extends WebSecurityConfigu
// @formatter:off // @formatter:off
http http
// other config goes here... // other config goes here...
.sessionManagement() .sessionManagement((sessionManagement) -> sessionManagement
.maximumSessions(2) .maximumSessions(2)
.sessionRegistry(sessionRegistry()); .sessionRegistry(sessionRegistry())
);
// @formatter:on // @formatter:on
} }

View File

@@ -35,13 +35,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.authorizeRequests() .authorizeRequests((authorize) -> authorize
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() )
.formLogin() .formLogin((formLogin) -> formLogin
.loginPage("/login") .loginPage("/login")
.permitAll(); .permitAll()
);
} }
// end::config[] // end::config[]
// @formatter:on // @formatter:on

View File

@@ -44,12 +44,13 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.authorizeRequests() .authorizeRequests((authorize) -> authorize
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() )
.formLogin() .formLogin((formLogin) -> formLogin
.permitAll(); .permitAll()
);
} }
// end::config[] // end::config[]
// @formatter:on // @formatter:on

View File

@@ -34,13 +34,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.authorizeRequests() .authorizeRequests((authorize) -> authorize
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() )
.formLogin() .formLogin((formLogin) -> formLogin
.loginPage("/login") .loginPage("/login")
.permitAll(); .permitAll()
);
} }
// @formatter:on // @formatter:on

View File

@@ -28,12 +28,13 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.authorizeRequests() .authorizeRequests((authorize) -> authorize
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() )
.formLogin() .formLogin((formLogin) -> formLogin
.permitAll(); .permitAll()
);
} }
// @formatter:on // @formatter:on

View File

@@ -35,12 +35,13 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.authorizeRequests() .authorizeRequests((authorize) -> authorize
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() )
.formLogin() .formLogin((formLogin) -> formLogin
.permitAll(); .permitAll()
);
} }
// end::config[] // end::config[]
// @formatter:on // @formatter:on

View File

@@ -53,12 +53,13 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.authorizeRequests() .authorizeRequests((authorize) -> authorize
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() )
.formLogin() .formLogin((formLogin) -> formLogin
.permitAll(); .permitAll()
);
} }
// @formatter:on // @formatter:on

View File

@@ -17,6 +17,7 @@
package sample; package sample;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -31,13 +32,13 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.authorizeRequests() .authorizeRequests((authorize) -> authorize
.anyRequest().authenticated() .anyRequest().authenticated()
.and() )
.requestCache() .requestCache((requestCache) -> requestCache
.requestCache(new NullRequestCache()) .requestCache(new NullRequestCache())
.and() )
.httpBasic(); .httpBasic(Customizer.withDefaults());
} }
// @formatter:on // @formatter:on