Use Spring Security lambda DSL in samples
Fixes: gh-1580
This commit is contained in:
@@ -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[]
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user