Use HttpSecurity Lambda DSL in Config Tests

Issue gh-13067
This commit is contained in:
Josh Cummings 2025-06-20 10:05:54 -06:00
parent 13e738e733
commit 1435e0f3d3
No known key found for this signature in database
GPG Key ID: 869B37A20E876129
72 changed files with 1185 additions and 1392 deletions

View File

@ -51,9 +51,8 @@ public class SecurityConfig {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers(new AntPathRequestMatcher("/*")).permitAll() .requestMatchers(new AntPathRequestMatcher("/*")).permitAll())
.and()
.authenticationProvider(authenticationProvider()); .authenticationProvider(authenticationProvider());
// @formatter:on // @formatter:on
return http.build(); return http.build();

View File

@ -87,8 +87,8 @@ public class Sec2758Tests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().access("hasAnyRole('CUSTOM')"); .anyRequest().access("hasAnyRole('CUSTOM')"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -86,9 +86,9 @@ public class AbstractRequestMatcherRegistryAnyMatcherTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated()
.requestMatchers(new AntPathRequestMatcher("/demo/**")).permitAll(); .requestMatchers(new AntPathRequestMatcher("/demo/**")).permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -103,9 +103,9 @@ public class AbstractRequestMatcherRegistryAnyMatcherTests {
SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception { SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated()
.requestMatchers(new MvcRequestMatcher(introspector, "/demo/**")).permitAll(); .requestMatchers(new MvcRequestMatcher(introspector, "/demo/**")).permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -120,9 +120,9 @@ public class AbstractRequestMatcherRegistryAnyMatcherTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated()
.requestMatchers(new RegexRequestMatcher(".*", null)).permitAll(); .requestMatchers(new RegexRequestMatcher(".*", null)).permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -137,9 +137,9 @@ public class AbstractRequestMatcherRegistryAnyMatcherTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated()
.anyRequest().permitAll(); .anyRequest().permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -154,9 +154,9 @@ public class AbstractRequestMatcherRegistryAnyMatcherTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated()
.requestMatchers(new AntPathRequestMatcher("/**")).permitAll(); .requestMatchers(new AntPathRequestMatcher("/**")).permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -48,6 +48,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -148,14 +149,12 @@ public class HttpConfigurationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.securityMatchers() .securityMatchers((security) -> security
.requestMatchers(new AntPathRequestMatcher("/api/**")) .requestMatchers(new AntPathRequestMatcher("/api/**"))
.requestMatchers(new AntPathRequestMatcher("/oauth/**")) .requestMatchers(new AntPathRequestMatcher("/oauth/**")))
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().hasRole("USER"))
.anyRequest().hasRole("USER") .httpBasic(withDefaults());
.and()
.httpBasic();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -71,6 +71,7 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
@ -293,9 +294,9 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().permitAll() .anyRequest().permitAll()
.accessDecisionManager(ACCESS_DECISION_MANAGER); .accessDecisionManager(ACCESS_DECISION_MANAGER));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -311,12 +312,11 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/admin").hasRole("ADMIN") .requestMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .exceptionHandling((handling) -> handling
.exceptionHandling() .accessDeniedPage("/AccessDeniedPage"));
.accessDeniedPage("/AccessDeniedPage");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -338,10 +338,9 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin(withDefaults());
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -356,11 +355,10 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().permitAll() .anyRequest().permitAll())
.and() .sessionManagement((management) -> management
.sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.ALWAYS));
.sessionCreationPolicy(SessionCreationPolicy.ALWAYS);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -375,11 +373,10 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().permitAll() .anyRequest().permitAll())
.and() .sessionManagement((management) -> management
.sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS));
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -395,14 +392,12 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/unsecure").permitAll() .requestMatchers("/unsecure").permitAll()
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .sessionManagement((management) -> management
.sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED))
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) .formLogin(withDefaults());
.and()
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -417,11 +412,10 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().anonymous() .anyRequest().anonymous())
.and() .sessionManagement((management) -> management
.sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.NEVER));
.sessionCreationPolicy(SessionCreationPolicy.NEVER);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -436,13 +430,11 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .exceptionHandling((handling) -> handling
.exceptionHandling() .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/entry-point")))
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/entry-point")) .formLogin(withDefaults());
.and()
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -472,11 +464,10 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .httpBasic((basic) -> basic
.httpBasic() .realmName("RealmConfig"));
.realmName("RealmConfig");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -562,13 +553,11 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .securityContext((context) -> context
.securityContext() .securityContextRepository(new NullSecurityContextRepository()))
.securityContextRepository(new NullSecurityContextRepository()) .formLogin(withDefaults());
.and()
.formLogin();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -588,11 +577,10 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().permitAll() .anyRequest().permitAll())
.and() .servletApi((api) -> api
.servletApi() .disable());
.disable();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -607,8 +595,8 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().permitAll(); .anyRequest().permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -641,10 +629,10 @@ public class NamespaceHttpTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/users**", "/sessions/**").hasRole("USER") .requestMatchers("/users**", "/sessions/**").hasRole("USER")
.requestMatchers("/signup").permitAll() .requestMatchers("/signup").permitAll()
.anyRequest().hasRole("USER"); .anyRequest().hasRole("USER"));
this.httpSecurity = http; this.httpSecurity = http;
return http.build(); return http.build();
// @formatter:on // @formatter:on

View File

@ -50,6 +50,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector; import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.config.Customizer.withDefaults;
/** /**
* @author Rob Winch * @author Rob Winch
@ -137,9 +138,9 @@ public class WebSecurityTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic().and() .httpBasic(withDefaults())
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().denyAll(); .anyRequest().denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -176,9 +177,9 @@ public class WebSecurityTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic().and() .httpBasic(withDefaults())
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().denyAll(); .anyRequest().denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -58,6 +58,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
@ -272,10 +273,9 @@ public class OAuth2ClientConfigurationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2Login(withDefaults());
.oauth2Login();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -311,10 +311,9 @@ public class OAuth2ClientConfigurationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2Login(withDefaults());
.oauth2Login();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -330,10 +329,9 @@ public class OAuth2ClientConfigurationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2Login(withDefaults());
.oauth2Login();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -369,10 +367,9 @@ public class OAuth2ClientConfigurationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2Login(withDefaults());
.oauth2Login();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -113,7 +113,7 @@ public class SecurityReactorContextConfigurationResourceServerTests {
@Bean @Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.securityContext().requireExplicitSave(false); http.securityContext((context) -> context.requireExplicitSave(false));
return http.build(); return http.build();
} }

View File

@ -535,9 +535,9 @@ public class WebSecurityConfigurationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated()
.expressionHandler(EXPRESSION_HANDLER); .expressionHandler(EXPRESSION_HANDLER));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -563,8 +563,8 @@ public class WebSecurityConfigurationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated(); .anyRequest().authenticated());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -616,8 +616,8 @@ public class WebSecurityConfigurationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated(); .anyRequest().authenticated());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -649,8 +649,8 @@ public class WebSecurityConfigurationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().access("request.method == 'GET' ? @b.grant() : @b.deny()"); .anyRequest().access("request.method == 'GET' ? @b.grant() : @b.deny()"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -110,11 +110,10 @@ public class AnonymousConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.anonymous() .anonymous((anonymous) -> anonymous
.key("key") .key("key")
.principal("principal") .principal("principal"))
.and() .anonymous(withDefaults());
.anonymous();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -762,7 +762,7 @@ public class AuthorizeHttpRequestsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeHttpRequests(); .authorizeHttpRequests(withDefaults());
// @formatter:on // @formatter:on
return http.build(); return http.build();
@ -793,8 +793,8 @@ public class AuthorizeHttpRequestsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeHttpRequests() .authorizeHttpRequests((requests) -> requests
.anyRequest(); .anyRequest());
// @formatter:on // @formatter:on
return http.build(); return http.build();
@ -849,8 +849,8 @@ public class AuthorizeHttpRequestsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeHttpRequests() .authorizeHttpRequests((requests) -> requests
.anyRequest().access(authorizationManager); .anyRequest().access(authorizationManager));
// @formatter:on // @formatter:on
return http.build(); return http.build();
@ -899,8 +899,7 @@ public class AuthorizeHttpRequestsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
return http return http
.httpBasic() .httpBasic(withDefaults())
.and()
.authorizeHttpRequests((requests) -> requests .authorizeHttpRequests((requests) -> requests
.anyRequest().hasAnyAuthority("ROLE_USER") .anyRequest().hasAnyAuthority("ROLE_USER")
) )
@ -918,8 +917,7 @@ public class AuthorizeHttpRequestsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
return http return http
.httpBasic() .httpBasic(withDefaults())
.and()
.authorizeHttpRequests((requests) -> requests .authorizeHttpRequests((requests) -> requests
.anyRequest().hasAuthority("ROLE_USER") .anyRequest().hasAuthority("ROLE_USER")
) )
@ -937,8 +935,7 @@ public class AuthorizeHttpRequestsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
return http return http
.httpBasic() .httpBasic(withDefaults())
.and()
.authorizeHttpRequests((requests) -> requests .authorizeHttpRequests((requests) -> requests
.anyRequest().hasAnyAuthority("ROLE_USER", "ROLE_ADMIN") .anyRequest().hasAnyAuthority("ROLE_USER", "ROLE_ADMIN")
) )
@ -1014,8 +1011,7 @@ public class AuthorizeHttpRequestsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
return http return http
.httpBasic() .httpBasic(withDefaults())
.and()
.authorizeHttpRequests((requests) -> requests .authorizeHttpRequests((requests) -> requests
.anyRequest().denyAll() .anyRequest().denyAll()
) )
@ -1050,8 +1046,7 @@ public class AuthorizeHttpRequestsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
return http return http
.httpBasic() .httpBasic(withDefaults())
.and()
.authorizeHttpRequests((requests) -> requests .authorizeHttpRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated()
) )
@ -1090,8 +1085,7 @@ public class AuthorizeHttpRequestsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
return http return http
.httpBasic() .httpBasic(withDefaults())
.and()
.authorizeHttpRequests((requests) -> requests .authorizeHttpRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated()
) )

View File

@ -198,8 +198,8 @@ public class AuthorizeRequestsTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers(new AntPathRequestMatcher("/**", HttpMethod.POST.name())).denyAll(); .requestMatchers(new AntPathRequestMatcher("/**", HttpMethod.POST.name())).denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -242,9 +242,9 @@ public class AuthorizeRequestsTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers(new AntPathRequestMatcher("/user/{user}", null, false)).access("#user == 'user'") .requestMatchers(new AntPathRequestMatcher("/user/{user}", null, false)).access("#user == 'user'")
.anyRequest().denyAll(); .anyRequest().denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -264,9 +264,9 @@ public class AuthorizeRequestsTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers(new AntPathRequestMatcher("/user/{userName}", null, false)).access("#userName == 'user'") .requestMatchers(new AntPathRequestMatcher("/user/{userName}", null, false)).access("#userName == 'user'")
.anyRequest().denyAll(); .anyRequest().denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -286,8 +286,8 @@ public class AuthorizeRequestsTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("ADMIN"); .anyRequest().hasRole("ADMIN"));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -315,9 +315,9 @@ public class AuthorizeRequestsTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic().and() .httpBasic(withDefaults())
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/path").denyAll(); .requestMatchers("/path").denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -385,9 +385,9 @@ public class AuthorizeRequestsTests {
.servletPath("/spring"); .servletPath("/spring");
// @formatter:off // @formatter:off
http http
.httpBasic().and() .httpBasic(withDefaults())
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers(mvcMatcherBuilder.pattern("/path")).denyAll(); .requestMatchers(mvcMatcherBuilder.pattern("/path")).denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -455,9 +455,9 @@ public class AuthorizeRequestsTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic().and() .httpBasic(withDefaults())
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/user/{userName}").access("#userName == 'user'"); .requestMatchers("/user/{userName}").access("#userName == 'user'"));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -523,9 +523,9 @@ public class AuthorizeRequestsTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic().and() .httpBasic(withDefaults())
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/user").denyAll(); .requestMatchers("/user").denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -46,6 +46,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
@ -138,8 +139,8 @@ public class ChannelSecurityConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.requiresChannel() .requiresChannel((channel) -> channel
.anyRequest().requiresSecure(); .anyRequest().requiresSecure());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -168,10 +169,9 @@ public class ChannelSecurityConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.requiresChannel() .requiresChannel((channel) -> channel
.anyRequest().requiresSecure() .anyRequest().requiresSecure())
.and() .requiresChannel(withDefaults());
.requiresChannel();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -204,13 +204,12 @@ public class ChannelSecurityConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.portMapper() .portMapper((mapper) -> mapper
.portMapper(new PortMapperImpl()) .portMapper(new PortMapperImpl()))
.and() .requiresChannel((channel) -> channel
.requiresChannel()
.redirectStrategy(new TestUrlRedirectStrategy()) .redirectStrategy(new TestUrlRedirectStrategy())
.anyRequest() .anyRequest()
.requiresSecure(); .requiresSecure());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -239,10 +238,9 @@ public class ChannelSecurityConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.portMapper() .portMapper((mapper) -> mapper
.portMapper(new PortMapperImpl()) .portMapper(new PortMapperImpl()))
.and() .requiresChannel((channel) -> channel
.requiresChannel()
.requestMatchers("/test-1") .requestMatchers("/test-1")
.requiresSecure() .requiresSecure()
.requestMatchers("/test-2") .requestMatchers("/test-2")
@ -250,7 +248,7 @@ public class ChannelSecurityConfigurerTests {
.requestMatchers("/test-3") .requestMatchers("/test-3")
.requiresSecure() .requiresSecure()
.anyRequest() .anyRequest()
.requiresInsecure(); .requiresInsecure());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -204,10 +204,9 @@ public class CorsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .cors(withDefaults());
.cors();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -223,10 +222,9 @@ public class CorsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .cors(withDefaults());
.cors();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -283,10 +281,9 @@ public class CorsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .cors(withDefaults());
.cors();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -340,10 +337,9 @@ public class CorsConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .cors(withDefaults());
.cors();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -108,9 +108,9 @@ public class CsrfConfigurerIgnoringRequestMatchersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.requireCsrfProtectionMatcher(new AntPathRequestMatcher("/path")) .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/path"))
.ignoringRequestMatchers(this.requestMatcher); .ignoringRequestMatchers(this.requestMatcher));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -149,9 +149,9 @@ public class CsrfConfigurerIgnoringRequestMatchersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.ignoringRequestMatchers(new AntPathRequestMatcher("/no-csrf")) .ignoringRequestMatchers(new AntPathRequestMatcher("/no-csrf"))
.ignoringRequestMatchers(this.requestMatcher); .ignoringRequestMatchers(this.requestMatcher));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -189,8 +189,8 @@ public class CsrfConfigurerIgnoringRequestMatchersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.ignoringRequestMatchers("/no-csrf"); .ignoringRequestMatchers("/no-csrf"));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -676,8 +676,8 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.disable(); .disable());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -707,13 +707,11 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin(withDefaults())
.formLogin() .csrf((csrf) -> csrf
.and() .disable());
.csrf()
.disable();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -735,13 +733,11 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin(withDefaults())
.formLogin() .csrf((csrf) -> csrf
.and() .csrfTokenRepository(REPO));
.csrf()
.csrfTokenRepository(REPO);
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -761,10 +757,9 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf(withDefaults())
.and() .sessionManagement((management) -> management
.sessionManagement() .invalidSessionUrl("/error/sessionError"));
.invalidSessionUrl("/error/sessionError");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -781,8 +776,8 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.requireCsrfProtectionMatcher(MATCHER); .requireCsrfProtectionMatcher(MATCHER));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -816,10 +811,9 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .csrf((csrf) -> csrf
.csrf() .csrfTokenRepository(REPO));
.csrfTokenRepository(REPO);
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -859,8 +853,8 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.exceptionHandling() .exceptionHandling((handling) -> handling
.accessDeniedHandler(DENIED_HANDLER); .accessDeniedHandler(DENIED_HANDLER));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -879,8 +873,8 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.exceptionHandling() .exceptionHandling((handling) -> handling
.defaultAccessDeniedHandlerFor(DENIED_HANDLER, MATCHER); .defaultAccessDeniedHandlerFor(DENIED_HANDLER, MATCHER));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -895,7 +889,7 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin(); .formLogin(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -910,10 +904,9 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .logout((logout) -> logout
.logout() .logoutRequestMatcher(new AntPathRequestMatcher("/logout")));
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -928,8 +921,8 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.requireCsrfProtectionMatcher(null); .requireCsrfProtectionMatcher(null));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -944,12 +937,10 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().permitAll() .anyRequest().permitAll())
.and() .formLogin(withDefaults())
.formLogin() .httpBasic(withDefaults());
.and()
.httpBasic();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -969,8 +960,8 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.sessionAuthenticationStrategy(null); .sessionAuthenticationStrategy(null));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -987,10 +978,9 @@ public class CsrfConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .csrf((csrf) -> csrf
.csrf() .sessionAuthenticationStrategy(STRATEGY));
.sessionAuthenticationStrategy(STRATEGY);
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -63,6 +63,7 @@ import org.springframework.security.web.util.matcher.AnyRequestMatcher;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.config.Customizer.withDefaults;
/** /**
* @author Rob Winch * @author Rob Winch
@ -170,7 +171,7 @@ public class DefaultFiltersTests {
@Bean @Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
TestHttpSecurities.disableDefaults(http); TestHttpSecurities.disableDefaults(http);
http.formLogin(); http.formLogin(withDefaults());
return http.build(); return http.build();
} }
@ -190,8 +191,8 @@ public class DefaultFiltersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER"); .anyRequest().hasRole("USER"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -387,10 +387,9 @@ public class DefaultLoginPageConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin(withDefaults());
.formLogin();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -410,13 +409,11 @@ public class DefaultLoginPageConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .logout((logout) -> logout
.logout() .logoutSuccessHandler(new SimpleUrlLogoutSuccessHandler()))
.logoutSuccessHandler(new SimpleUrlLogoutSuccessHandler()) .formLogin(withDefaults());
.and()
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -431,13 +428,11 @@ public class DefaultLoginPageConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .logout((logout) -> logout
.logout() .logoutSuccessUrl("/login?logout"))
.logoutSuccessUrl("/login?logout") .formLogin(withDefaults());
.and()
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -452,12 +447,10 @@ public class DefaultLoginPageConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin(withDefaults())
.formLogin() .rememberMe(withDefaults());
.and()
.rememberMe();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -477,13 +470,11 @@ public class DefaultLoginPageConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.exceptionHandling() .exceptionHandling((handling) -> handling
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login")) .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login")))
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().hasRole("USER"))
.anyRequest().hasRole("USER") .formLogin(withDefaults());
.and()
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -500,9 +491,8 @@ public class DefaultLoginPageConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.exceptionHandling() .exceptionHandling(withDefaults())
.and() .formLogin(withDefaults());
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -87,16 +87,15 @@ public class ExceptionHandlingConfigurerAccessDeniedHandlerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().denyAll() .anyRequest().denyAll())
.and() .exceptionHandling((handling) -> handling
.exceptionHandling()
.defaultAccessDeniedHandlerFor( .defaultAccessDeniedHandlerFor(
this.teapotDeniedHandler, this.teapotDeniedHandler,
new AntPathRequestMatcher("/hello/**")) new AntPathRequestMatcher("/hello/**"))
.defaultAccessDeniedHandlerFor( .defaultAccessDeniedHandlerFor(
new AccessDeniedHandlerImpl(), new AccessDeniedHandlerImpl(),
AnyRequestMatcher.INSTANCE); AnyRequestMatcher.INSTANCE));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -146,13 +145,12 @@ public class ExceptionHandlingConfigurerAccessDeniedHandlerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().denyAll() .anyRequest().denyAll())
.and() .exceptionHandling((handling) -> handling
.exceptionHandling()
.defaultAccessDeniedHandlerFor( .defaultAccessDeniedHandlerFor(
this.teapotDeniedHandler, this.teapotDeniedHandler,
new AntPathRequestMatcher("/hello/**")); new AntPathRequestMatcher("/hello/**")));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -52,6 +52,7 @@ import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.config.annotation.SecurityContextChangedListenerArgumentMatchers.setAuthentication; import static org.springframework.security.config.annotation.SecurityContextChangedListenerArgumentMatchers.setAuthentication;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
@ -241,7 +242,7 @@ public class ExceptionHandlingConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.exceptionHandling(); .exceptionHandling(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -291,12 +292,10 @@ public class ExceptionHandlingConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .httpBasic(withDefaults())
.httpBasic() .formLogin(withDefaults());
.and()
.formLogin();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -330,12 +329,10 @@ public class ExceptionHandlingConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .httpBasic(withDefaults())
.httpBasic() .formLogin(withDefaults());
.and()
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -352,12 +349,11 @@ public class ExceptionHandlingConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .exceptionHandling((handling) -> handling
.exceptionHandling() .authenticationEntryPoint(AEP))
.authenticationEntryPoint(AEP).and() .exceptionHandling(withDefaults());
.exceptionHandling();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -70,6 +70,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
@ -566,8 +567,8 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("ROLE_USER"); .anyRequest().hasRole("ROLE_USER"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -584,8 +585,8 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER"); .anyRequest().hasRole("USER"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -605,7 +606,7 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests(); .authorizeRequests(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -621,9 +622,9 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/a").authenticated() .requestMatchers("/a").authenticated()
.anyRequest(); .anyRequest());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -638,10 +639,9 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().hasAnyAuthority("ROLE_USER"));
.anyRequest().hasAnyAuthority("ROLE_USER");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -656,10 +656,9 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().hasAuthority("ROLE_USER"));
.anyRequest().hasAuthority("ROLE_USER");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -674,10 +673,9 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().hasAnyAuthority("ROLE_USER", "ROLE_ADMIN"));
.anyRequest().hasAnyAuthority("ROLE_USER", "ROLE_ADMIN");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -692,8 +690,8 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasAnyRole("USER"); .anyRequest().hasAnyRole("USER"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -708,8 +706,8 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasAnyRole("USER"); .anyRequest().hasAnyRole("USER"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -729,8 +727,8 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasAnyRole("USER"); .anyRequest().hasAnyRole("USER"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -750,8 +748,8 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasAnyRole("USER", "ADMIN"); .anyRequest().hasAnyRole("USER", "ADMIN"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -766,8 +764,8 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasAnyRole("USER", "ADMIN"); .anyRequest().hasAnyRole("USER", "ADMIN"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -787,8 +785,8 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasAnyRole("USER", "ADMIN"); .anyRequest().hasAnyRole("USER", "ADMIN"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -808,10 +806,9 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().hasIpAddress("192.168.1.0"));
.anyRequest().hasIpAddress("192.168.1.0");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -826,10 +823,9 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().anonymous());
.anyRequest().anonymous();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -844,12 +840,10 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.rememberMe() .rememberMe(withDefaults())
.and() .httpBasic(withDefaults())
.httpBasic() .authorizeRequests((requests) -> requests
.and() .anyRequest().rememberMe());
.authorizeRequests()
.anyRequest().rememberMe();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -869,10 +863,9 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().denyAll());
.anyRequest().denyAll();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -887,10 +880,9 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().not().denyAll());
.anyRequest().not().denyAll();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -905,12 +897,10 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.rememberMe() .rememberMe(withDefaults())
.and() .httpBasic(withDefaults())
.httpBasic() .authorizeRequests((requests) -> requests
.and() .anyRequest().fullyAuthenticated());
.authorizeRequests()
.anyRequest().fullyAuthenticated();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -930,12 +920,10 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.rememberMe() .rememberMe(withDefaults())
.and() .httpBasic(withDefaults())
.httpBasic() .authorizeRequests((requests) -> requests
.and() .anyRequest().access("hasRole('ROLE_USER') or request.method == 'GET'"));
.authorizeRequests()
.anyRequest().access("hasRole('ROLE_USER') or request.method == 'GET'");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -955,12 +943,10 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().authenticated())
.anyRequest().authenticated() .authorizeRequests(withDefaults());
.and()
.authorizeRequests();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -979,14 +965,13 @@ public class ExpressionUrlAuthorizationConfigurerTests {
AffirmativeBased adm = new AffirmativeBased(Collections.singletonList(expressionVoter)); AffirmativeBased adm = new AffirmativeBased(Collections.singletonList(expressionVoter));
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.expressionHandler(handler) .expressionHandler(handler)
.accessDecisionManager(adm) .accessDecisionManager(adm)
.filterSecurityInterceptorOncePerRequest(true) .filterSecurityInterceptorOncePerRequest(true)
.requestMatchers("/a", "/b").hasRole("ADMIN") .requestMatchers("/a", "/b").hasRole("ADMIN")
.anyRequest().permitAll() .anyRequest().permitAll())
.and() .formLogin(withDefaults());
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1001,7 +986,7 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().permitAll() .anyRequest().permitAll()
.withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() { .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
@Override @Override
@ -1010,7 +995,7 @@ public class ExpressionUrlAuthorizationConfigurerTests {
fsi.setPublishAuthorizationSuccess(true); fsi.setPublishAuthorizationSuccess(true);
return fsi; return fsi;
} }
}); }));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1046,11 +1031,11 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/admin").hasRole("ADMIN") .requestMatchers("/admin").hasRole("ADMIN")
.requestMatchers("/user").hasRole("USER") .requestMatchers("/user").hasRole("USER")
.requestMatchers("/allow").access("@permission.check(authentication,'user')") .requestMatchers("/allow").access("@permission.check(authentication,'user')")
.anyRequest().access("@permission.check(authentication,'admin')"); .anyRequest().access("@permission.check(authentication,'admin')"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1079,12 +1064,12 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.expressionHandler(expressionHandler()) .expressionHandler(expressionHandler())
.requestMatchers("/admin").hasRole("ADMIN") .requestMatchers("/admin").hasRole("ADMIN")
.requestMatchers("/user").hasRole("USER") .requestMatchers("/user").hasRole("USER")
.requestMatchers("/allow").access("check('user')") .requestMatchers("/allow").access("check('user')")
.anyRequest().access("check('admin')"); .anyRequest().access("check('admin')"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1133,8 +1118,8 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated(); .anyRequest().authenticated());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -1160,12 +1145,12 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/allow").access("hasPermission('ID', 'TYPE', 'PERMISSION')") .requestMatchers("/allow").access("hasPermission('ID', 'TYPE', 'PERMISSION')")
.requestMatchers("/allowObject").access("hasPermission('TESTOBJ', 'PERMISSION')") .requestMatchers("/allowObject").access("hasPermission('TESTOBJ', 'PERMISSION')")
.requestMatchers("/deny").access("hasPermission('ID', 'TYPE', 'NO PERMISSION')") .requestMatchers("/deny").access("hasPermission('ID', 'TYPE', 'NO PERMISSION')")
.requestMatchers("/denyObject").access("hasPermission('TESTOBJ', 'NO PERMISSION')") .requestMatchers("/denyObject").access("hasPermission('TESTOBJ', 'NO PERMISSION')")
.anyRequest().permitAll(); .anyRequest().permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1198,10 +1183,10 @@ public class ExpressionUrlAuthorizationConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/allow").access("hasRole('MEMBER')") .requestMatchers("/allow").access("hasRole('MEMBER')")
.requestMatchers("/deny").access("hasRole('ADMIN')") .requestMatchers("/deny").access("hasRole('ADMIN')")
.anyRequest().permitAll(); .anyRequest().permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -396,9 +396,9 @@ public class FormLoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin().and() .formLogin(withDefaults())
.requestCache() .requestCache((cache) -> cache
.requestCache(this.requestCache); .requestCache(this.requestCache));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -430,11 +430,10 @@ public class FormLoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin((login) -> login
.formLogin() .loginPage("/login"));
.loginPage("/login");
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -478,11 +477,10 @@ public class FormLoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin((login) -> login
.formLogin() .permitAll());
.permitAll();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -497,15 +495,13 @@ public class FormLoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin((login) -> login
.formLogin()
.loginPage("/authenticate") .loginPage("/authenticate")
.permitAll() .permitAll())
.and() .logout((logout) -> logout
.logout() .permitAll());
.permitAll();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -544,21 +540,19 @@ public class FormLoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin((login) -> login
.formLogin()
.loginProcessingUrl("/loginCheck") .loginProcessingUrl("/loginCheck")
.loginPage("/login") .loginPage("/login")
.defaultSuccessUrl("/", true) .defaultSuccessUrl("/", true)
.passwordParameter("password") .passwordParameter("password")
.usernameParameter("username") .usernameParameter("username")
.permitAll() .permitAll())
.and() .logout((logout) -> logout
.logout()
.logoutSuccessUrl("/login") .logoutSuccessUrl("/login")
.logoutUrl("/logout") .logoutUrl("/logout")
.deleteCookies("JSESSIONID"); .deleteCookies("JSESSIONID"));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -616,14 +610,12 @@ public class FormLoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin((login) -> login
.formLogin() .permitAll())
.permitAll() .portMapper((mapper) -> mapper
.and() .portMapper(PORT_MAPPER));
.portMapper()
.portMapper(PORT_MAPPER);
// @formatter:on // @formatter:on
LoginUrlAuthenticationEntryPoint authenticationEntryPoint = (LoginUrlAuthenticationEntryPoint) http LoginUrlAuthenticationEntryPoint authenticationEntryPoint = (LoginUrlAuthenticationEntryPoint) http
.getConfigurer(FormLoginConfigurer.class) .getConfigurer(FormLoginConfigurer.class)
@ -644,12 +636,11 @@ public class FormLoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin((login) -> login
.formLogin()
.failureHandler(FAILURE_HANDLER) .failureHandler(FAILURE_HANDLER)
.permitAll(); .permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -664,10 +655,9 @@ public class FormLoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin((login) -> login
.usernameParameter("custom-username") .usernameParameter("custom-username"))
.and() .formLogin(withDefaults());
.formLogin();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -687,15 +677,14 @@ public class FormLoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.disable() .disable())
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin((login) -> login
.formLogin()
.failureForwardUrl("/failure_forward_url") .failureForwardUrl("/failure_forward_url")
.successForwardUrl("/success_forward_url") .successForwardUrl("/success_forward_url")
.permitAll(); .permitAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -717,9 +706,8 @@ public class FormLoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.exceptionHandling() .exceptionHandling(withDefaults())
.and() .formLogin(withDefaults());
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -71,14 +71,14 @@ public class HeadersConfigurerEagerHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.addObjectPostProcessor(new ObjectPostProcessor<HeaderWriterFilter>() { .addObjectPostProcessor(new ObjectPostProcessor<HeaderWriterFilter>() {
@Override @Override
public HeaderWriterFilter postProcess(HeaderWriterFilter filter) { public HeaderWriterFilter postProcess(HeaderWriterFilter filter) {
filter.setShouldWriteHeadersEagerly(true); filter.setShouldWriteHeadersEagerly(true);
return filter; return filter;
} }
}); }));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -28,6 +28,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
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.test.SpringTestContext; import org.springframework.security.config.test.SpringTestContext;
@ -582,7 +583,7 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers(); .headers(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -612,9 +613,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.contentTypeOptions(); .contentTypeOptions(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -648,9 +649,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.frameOptions(); .frameOptions(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -665,9 +666,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpStrictTransportSecurity(); .httpStrictTransportSecurity(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -682,9 +683,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.cacheControl(); .cacheControl(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -718,9 +719,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.xssProtection(); .xssProtection(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -735,10 +736,10 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.xssProtection() .xssProtection((xss) -> xss
.headerValue(XXssProtectionHeaderWriter.HeaderValue.ENABLED_MODE_BLOCK); .headerValue(XXssProtectionHeaderWriter.HeaderValue.ENABLED_MODE_BLOCK)));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -791,8 +792,8 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.frameOptions().sameOrigin(); .frameOptions((frameOptions) -> frameOptions.sameOrigin()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -825,9 +826,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpPublicKeyPinning(); .httpPublicKeyPinning(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -842,10 +843,10 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpPublicKeyPinning() .httpPublicKeyPinning((hpkp) -> hpkp
.addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM="); .addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=")));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -863,10 +864,9 @@ public class HeadersConfigurerTests {
pins.put("E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=", "sha256"); pins.put("E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=", "sha256");
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpPublicKeyPinning() .httpPublicKeyPinning((hpkp) -> hpkp.withPins(pins)));
.withPins(pins);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -881,11 +881,11 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpPublicKeyPinning() .httpPublicKeyPinning((hpkp) -> hpkp
.addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=") .addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=")
.maxAgeInSeconds(604800); .maxAgeInSeconds(604800)));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -900,11 +900,11 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpPublicKeyPinning() .httpPublicKeyPinning((hpkp) -> hpkp
.addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=") .addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=")
.reportOnly(false); .reportOnly(false)));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -919,11 +919,11 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpPublicKeyPinning() .httpPublicKeyPinning((hpkp) -> hpkp
.addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=") .addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=")
.includeSubDomains(true); .includeSubDomains(true)));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -938,11 +938,11 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpPublicKeyPinning() .httpPublicKeyPinning((hpkp) -> hpkp
.addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=") .addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=")
.reportUri(new URI("https://example.net/pkp-report")); .reportUri(URI.create("https://example.net/pkp-report"))));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -957,11 +957,11 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpPublicKeyPinning() .httpPublicKeyPinning((hpkp) -> hpkp
.addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=") .addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=")
.reportUri("https://example.net/pkp-report"); .reportUri("https://example.net/pkp-report")));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -999,9 +999,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.contentSecurityPolicy("default-src 'self'"); .contentSecurityPolicy((csp) -> csp.policyDirectives("default-src 'self'")));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1016,10 +1016,11 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.contentSecurityPolicy("default-src 'self'; script-src trustedscripts.example.com") .contentSecurityPolicy((csp) -> csp
.reportOnly(); .policyDirectives("default-src 'self'; script-src trustedscripts.example.com")
.reportOnly()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1057,9 +1058,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.contentSecurityPolicy(""); .contentSecurityPolicy((csp) -> csp.policyDirectives("")));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1114,9 +1115,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.referrerPolicy(); .referrerPolicy(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1134,7 +1135,7 @@ public class HeadersConfigurerTests {
.headers((headers) -> .headers((headers) ->
headers headers
.defaultsDisabled() .defaultsDisabled()
.referrerPolicy() .referrerPolicy(Customizer.withDefaults())
); );
return http.build(); return http.build();
// @formatter:on // @formatter:on
@ -1150,9 +1151,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.referrerPolicy(ReferrerPolicy.SAME_ORIGIN); .referrerPolicy((referrer) -> referrer.policy(ReferrerPolicy.SAME_ORIGIN)));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1188,9 +1189,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.featurePolicy("geolocation 'self'"); .featurePolicy("geolocation 'self'"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1205,9 +1206,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.featurePolicy(""); .featurePolicy(""));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1222,9 +1223,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.permissionsPolicy((permissionsPolicy) -> permissionsPolicy.policy("geolocation=(self)")); .permissionsPolicy((permissionsPolicy) -> permissionsPolicy.policy("geolocation=(self)")));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1239,10 +1240,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.permissionsPolicy() .permissionsPolicy((permissions) -> permissions.policy("geolocation=(self)")));
.policy("geolocation=(self)");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1257,9 +1257,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.permissionsPolicy((permissionsPolicy) -> permissionsPolicy.policy(null)); .permissionsPolicy((permissionsPolicy) -> permissionsPolicy.policy(null)));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1274,10 +1274,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.permissionsPolicy() .permissionsPolicy((permissions) -> permissions.policy("")));
.policy("");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1292,10 +1291,9 @@ public class HeadersConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpStrictTransportSecurity() .httpStrictTransportSecurity((hsts) -> hsts.preload(true)));
.preload(true);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1353,16 +1351,14 @@ public class HeadersConfigurerTests {
@Bean @Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http.headers() http.headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.crossOriginOpenerPolicy() .crossOriginOpenerPolicy((opener) -> opener
.policy(CrossOriginOpenerPolicyHeaderWriter.CrossOriginOpenerPolicy.SAME_ORIGIN) .policy(CrossOriginOpenerPolicyHeaderWriter.CrossOriginOpenerPolicy.SAME_ORIGIN))
.and() .crossOriginEmbedderPolicy((embedder) -> embedder
.crossOriginEmbedderPolicy() .policy(CrossOriginEmbedderPolicyHeaderWriter.CrossOriginEmbedderPolicy.REQUIRE_CORP))
.policy(CrossOriginEmbedderPolicyHeaderWriter.CrossOriginEmbedderPolicy.REQUIRE_CORP) .crossOriginResourcePolicy((resource) -> resource
.and() .policy(CrossOriginResourcePolicyHeaderWriter.CrossOriginResourcePolicy.SAME_ORIGIN)));
.crossOriginResourcePolicy()
.policy(CrossOriginResourcePolicyHeaderWriter.CrossOriginResourcePolicy.SAME_ORIGIN);
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -221,7 +221,7 @@ public class HttpBasicConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic(); .httpBasic(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -274,10 +274,9 @@ public class HttpBasicConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .httpBasic(withDefaults());
.httpBasic();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -299,11 +298,10 @@ public class HttpBasicConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .httpBasic((basic) -> basic
.httpBasic() .authenticationEntryPoint(ENTRY_POINT));
.authenticationEntryPoint(ENTRY_POINT);
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -325,13 +323,11 @@ public class HttpBasicConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .httpBasic((basic) -> basic
.httpBasic() .authenticationEntryPoint(ENTRY_POINT))
.authenticationEntryPoint(ENTRY_POINT) .httpBasic(withDefaults());
.and()
.httpBasic();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -351,9 +347,8 @@ public class HttpBasicConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .rememberMe(withDefaults());
.rememberMe();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -397,8 +392,8 @@ public class HttpBasicConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic((basic) -> basic
.securityContextRepository(SECURITY_CONTEXT_REPOSITORY); .securityContextRepository(SECURITY_CONTEXT_REPOSITORY));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -100,9 +100,9 @@ public class HttpSecurityLogoutTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf().disable() .csrf((csrf) -> csrf.disable())
.logout() .logout((logout) -> logout
.clearAuthentication(false); .clearAuthentication(false));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -222,14 +222,12 @@ public class HttpSecurityRequestMatchersTests {
MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector); MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector);
// @formatter:off // @formatter:off
http http
.securityMatchers() .securityMatchers((security) -> security
.requestMatchers(mvcMatcherBuilder.pattern("/test-1")) .requestMatchers(mvcMatcherBuilder.pattern("/test-1"))
.requestMatchers(mvcMatcherBuilder.pattern("/test-2")) .requestMatchers(mvcMatcherBuilder.pattern("/test-2"))
.requestMatchers(mvcMatcherBuilder.pattern("/test-3")) .requestMatchers(mvcMatcherBuilder.pattern("/test-3")))
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().denyAll())
.anyRequest().denyAll()
.and()
.httpBasic(withDefaults()); .httpBasic(withDefaults());
// @formatter:on // @formatter:on
return http.build(); return http.build();
@ -240,11 +238,10 @@ public class HttpSecurityRequestMatchersTests {
MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector); MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector);
// @formatter:off // @formatter:off
http http
.securityMatchers() .securityMatchers((security) -> security
.requestMatchers(mvcMatcherBuilder.pattern("/test-1")) .requestMatchers(mvcMatcherBuilder.pattern("/test-1")))
.and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().permitAll());
.anyRequest().permitAll();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -271,9 +268,9 @@ public class HttpSecurityRequestMatchersTests {
// @formatter:off // @formatter:off
http http
.securityMatcher(new MvcRequestMatcher(introspector, "/path")) .securityMatcher(new MvcRequestMatcher(introspector, "/path"))
.httpBasic().and() .httpBasic(withDefaults())
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().denyAll(); .anyRequest().denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -304,12 +301,11 @@ public class HttpSecurityRequestMatchersTests {
SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception { SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
// @formatter:off // @formatter:off
http http
.securityMatchers() .securityMatchers((security) -> security
.requestMatchers(new MvcRequestMatcher(introspector, "/path")) .requestMatchers(new MvcRequestMatcher(introspector, "/path")))
.and() .httpBasic(withDefaults())
.httpBasic().and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().denyAll());
.anyRequest().denyAll();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -376,13 +372,12 @@ public class HttpSecurityRequestMatchersTests {
mvcMatcherBuilder.servletPath("/spring"); mvcMatcherBuilder.servletPath("/spring");
// @formatter:off // @formatter:off
http http
.securityMatchers() .securityMatchers((security) -> security
.requestMatchers(mvcMatcherBuilder.pattern("/path")) .requestMatchers(mvcMatcherBuilder.pattern("/path"))
.requestMatchers("/never-match") .requestMatchers("/never-match"))
.and() .httpBasic(withDefaults())
.httpBasic().and() .authorizeRequests((requests) -> requests
.authorizeRequests() .anyRequest().denyAll());
.anyRequest().denyAll();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -45,6 +45,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.config.Customizer.withDefaults;
/** /**
* @author Marcus Da Coregio * @author Marcus Da Coregio
@ -121,9 +122,9 @@ public class HttpSecuritySecurityMatchersNoMvcTests {
// @formatter:off // @formatter:off
http http
.securityMatcher("/path") .securityMatcher("/path")
.httpBasic().and() .httpBasic(withDefaults())
.authorizeHttpRequests() .authorizeHttpRequests((requests) -> requests
.anyRequest().denyAll(); .anyRequest().denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -222,14 +222,12 @@ public class HttpSecuritySecurityMatchersTests {
SecurityFilterChain first(HttpSecurity http) throws Exception { SecurityFilterChain first(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.securityMatchers() .securityMatchers((security) -> security
.requestMatchers("/test-1") .requestMatchers("/test-1")
.requestMatchers("/test-2") .requestMatchers("/test-2")
.requestMatchers("/test-3") .requestMatchers("/test-3"))
.and() .authorizeHttpRequests((requests) -> requests
.authorizeHttpRequests() .anyRequest().denyAll())
.anyRequest().denyAll()
.and()
.httpBasic(withDefaults()); .httpBasic(withDefaults());
// @formatter:on // @formatter:on
return http.build(); return http.build();
@ -239,11 +237,10 @@ public class HttpSecuritySecurityMatchersTests {
SecurityFilterChain second(HttpSecurity http) throws Exception { SecurityFilterChain second(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.securityMatchers() .securityMatchers((security) -> security
.requestMatchers("/test-1") .requestMatchers("/test-1"))
.and() .authorizeHttpRequests((requests) -> requests
.authorizeHttpRequests() .anyRequest().permitAll());
.anyRequest().permitAll();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -271,9 +268,9 @@ public class HttpSecuritySecurityMatchersTests {
// @formatter:off // @formatter:off
http http
.securityMatcher("/path") .securityMatcher("/path")
.httpBasic().and() .httpBasic(withDefaults())
.authorizeHttpRequests() .authorizeHttpRequests((requests) -> requests
.anyRequest().denyAll(); .anyRequest().denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -301,9 +298,9 @@ public class HttpSecuritySecurityMatchersTests {
// @formatter:off // @formatter:off
http http
.securityMatcher("/path") .securityMatcher("/path")
.httpBasic().and() .httpBasic(withDefaults())
.authorizeHttpRequests() .authorizeHttpRequests((requests) -> requests
.anyRequest().denyAll(); .anyRequest().denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -364,13 +361,13 @@ public class HttpSecuritySecurityMatchersTests {
.servletPath("/spring"); .servletPath("/spring");
// @formatter:off // @formatter:off
http http
.securityMatchers() .securityMatchers((security) -> security
.requestMatchers(mvcMatcherBuilder.pattern("/path")) .requestMatchers(mvcMatcherBuilder.pattern("/path"))
.requestMatchers(mvcMatcherBuilder.pattern("/never-match")) .requestMatchers(mvcMatcherBuilder.pattern("/never-match"))
.and() )
.httpBasic().and() .httpBasic(withDefaults())
.authorizeHttpRequests() .authorizeHttpRequests((requests) -> requests
.anyRequest().denyAll(); .anyRequest().denyAll());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -44,6 +44,7 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated; import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@ -162,7 +163,7 @@ public class JeeConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.jee(); .jee(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -191,10 +192,9 @@ public class JeeConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.jee() .jee((jee) -> jee
.mappableRoles("USER") .mappableRoles("USER"))
.and() .jee(withDefaults());
.jee();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -97,8 +97,8 @@ public class LogoutConfigurerClearSiteDataTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.logout() .logout((logout) -> logout
.addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(SOURCE))); .addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(SOURCE))));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -56,6 +56,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
@ -414,8 +415,8 @@ public class LogoutConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.logout() .logout((logout) -> logout
.defaultLogoutSuccessHandlerFor(null, mock(RequestMatcher.class)); .defaultLogoutSuccessHandlerFor(null, mock(RequestMatcher.class)));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -447,8 +448,8 @@ public class LogoutConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.logout() .logout((logout) -> logout
.defaultLogoutSuccessHandlerFor(mock(LogoutSuccessHandler.class), null); .defaultLogoutSuccessHandlerFor(mock(LogoutSuccessHandler.class), null));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -482,7 +483,7 @@ public class LogoutConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.logout(); .logout(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -511,10 +512,9 @@ public class LogoutConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.logout() .logout((logout) -> logout
.logoutUrl("/custom/logout") .logoutUrl("/custom/logout"))
.and() .logout(withDefaults());
.logout();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -534,9 +534,9 @@ public class LogoutConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.disable() .disable())
.logout(); .logout(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -551,10 +551,10 @@ public class LogoutConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.disable() .disable())
.logout() .logout((logout) -> logout
.logoutUrl("/custom/logout"); .logoutUrl("/custom/logout"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -569,8 +569,8 @@ public class LogoutConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf() .csrf((csrf) -> csrf
.disable() .disable())
.logout((logout) -> logout.logoutUrl("/custom/logout")); .logout((logout) -> logout.logoutUrl("/custom/logout"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
@ -586,8 +586,8 @@ public class LogoutConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.logout() .logout((logout) -> logout
.addLogoutHandler(null); .addLogoutHandler(null));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -619,8 +619,8 @@ public class LogoutConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.rememberMe() .rememberMe((me) -> me
.rememberMeServices(REMEMBER_ME); .rememberMeServices(REMEMBER_ME));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -641,8 +641,8 @@ public class LogoutConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.logout() .logout((logout) -> logout
.disable(); .disable());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -99,9 +99,9 @@ public class NamespaceHttpAnonymousTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/type").anonymous() .requestMatchers("/type").anonymous()
.anyRequest().denyAll(); .anyRequest().denyAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -116,10 +116,9 @@ public class NamespaceHttpAnonymousTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().permitAll() .anyRequest().permitAll())
.and() .anonymous((anonymous) -> anonymous.disable());
.anonymous().disable();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -140,12 +139,11 @@ public class NamespaceHttpAnonymousTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/type").hasRole("ANON") .requestMatchers("/type").hasRole("ANON")
.anyRequest().denyAll() .anyRequest().denyAll())
.and() .anonymous((anonymous) -> anonymous
.anonymous() .authorities("ROLE_ANON"));
.authorities("ROLE_ANON");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -161,11 +159,10 @@ public class NamespaceHttpAnonymousTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/key").anonymous() .requestMatchers("/key").anonymous()
.anyRequest().denyAll() .anyRequest().denyAll())
.and() .anonymous((anonymous) -> anonymous.key("AnonymousKeyConfig"));
.anonymous().key("AnonymousKeyConfig");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -181,11 +178,10 @@ public class NamespaceHttpAnonymousTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/principal").anonymous() .requestMatchers("/principal").anonymous()
.anyRequest().denyAll() .anyRequest().denyAll())
.and() .anonymous((anonymous) -> anonymous.principal("AnonymousUsernameConfig"));
.anonymous().principal("AnonymousUsernameConfig");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -183,10 +183,9 @@ public class NamespaceHttpBasicTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .httpBasic(withDefaults());
.httpBasic();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -220,10 +219,9 @@ public class NamespaceHttpBasicTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .httpBasic((basic) -> basic.realmName("Custom Realm"));
.httpBasic().realmName("Custom Realm");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -260,8 +258,8 @@ public class NamespaceHttpBasicTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic((basic) -> basic
.authenticationDetailsSource(this.authenticationDetailsSource); .authenticationDetailsSource(this.authenticationDetailsSource));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -307,11 +305,10 @@ public class NamespaceHttpBasicTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .httpBasic((basic) -> basic
.httpBasic() .authenticationEntryPoint(this.authenticationEntryPoint));
.authenticationEntryPoint(this.authenticationEntryPoint);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -48,6 +48,7 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.config.Customizer.withDefaults;
/** /**
* Tests to verify that all the functionality of &lt;custom-filter&gt; attributes is * Tests to verify that all the functionality of &lt;custom-filter&gt; attributes is
@ -110,7 +111,7 @@ public class NamespaceHttpCustomFilterTests {
// @formatter:off // @formatter:off
http http
.addFilterBefore(new CustomFilter(), UsernamePasswordAuthenticationFilter.class) .addFilterBefore(new CustomFilter(), UsernamePasswordAuthenticationFilter.class)
.formLogin(); .formLogin(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -126,7 +127,7 @@ public class NamespaceHttpCustomFilterTests {
// @formatter:off // @formatter:off
http http
.addFilterAfter(new CustomFilter(), UsernamePasswordAuthenticationFilter.class) .addFilterAfter(new CustomFilter(), UsernamePasswordAuthenticationFilter.class)
.formLogin(); .formLogin(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -181,9 +182,8 @@ public class NamespaceHttpCustomFilterTests {
// @formatter:off // @formatter:off
TestHttpSecurities.disableDefaults(http); TestHttpSecurities.disableDefaults(http);
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and()
.addFilterBefore(new CustomFilter(), UsernamePasswordAuthenticationFilter.class); .addFilterBefore(new CustomFilter(), UsernamePasswordAuthenticationFilter.class);
return http.build(); return http.build();
// @formatter:on // @formatter:on

View File

@ -99,9 +99,9 @@ public class NamespaceHttpExpressionHandlerTests {
handler.setExpressionParser(expressionParser()); handler.setExpressionParser(expressionParser());
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.expressionHandler(handler) .expressionHandler(handler)
.anyRequest().access("hasRole('USER')"); .anyRequest().access("hasRole('USER')"));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -42,6 +42,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@ -124,10 +125,9 @@ public class NamespaceHttpFormLoginTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin(withDefaults());
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -143,16 +143,15 @@ public class NamespaceHttpFormLoginTests {
boolean alwaysUseDefaultSuccess = true; boolean alwaysUseDefaultSuccess = true;
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin((login) -> login
.formLogin()
.usernameParameter("username") // form-login@username-parameter .usernameParameter("username") // form-login@username-parameter
.passwordParameter("password") // form-login@password-parameter .passwordParameter("password") // form-login@password-parameter
.loginPage("/authentication/login") // form-login@login-page .loginPage("/authentication/login") // form-login@login-page
.failureUrl("/authentication/login?failed") // form-login@authentication-failure-url .failureUrl("/authentication/login?failed") // form-login@authentication-failure-url
.loginProcessingUrl("/authentication/login/process") // form-login@login-processing-url .loginProcessingUrl("/authentication/login/process") // form-login@login-processing-url
.defaultSuccessUrl("/default", alwaysUseDefaultSuccess); .defaultSuccessUrl("/default", alwaysUseDefaultSuccess));
return http.build(); // form-login@default-target-url / form-login@always-use-default-target return http.build(); // form-login@default-target-url / form-login@always-use-default-target
// @formatter:on // @formatter:on
} }
@ -169,15 +168,13 @@ public class NamespaceHttpFormLoginTests {
successHandler.setDefaultTargetUrl("/custom/targetUrl"); successHandler.setDefaultTargetUrl("/custom/targetUrl");
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin((login) -> login
.formLogin()
.loginPage("/login") .loginPage("/login")
.failureHandler(new SimpleUrlAuthenticationFailureHandler("/custom/failure")) // form-login@authentication-failure-handler-ref .failureHandler(new SimpleUrlAuthenticationFailureHandler("/custom/failure")) // form-login@authentication-failure-handler-ref
.successHandler(successHandler) // form-login@authentication-success-handler-ref .successHandler(successHandler) // form-login@authentication-success-handler-ref
.authenticationDetailsSource(authenticationDetailsSource()) // form-login@authentication-details-source-ref .authenticationDetailsSource(authenticationDetailsSource()));
.and();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -27,6 +27,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
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.test.SpringTestContext; import org.springframework.security.config.test.SpringTestContext;
@ -41,6 +42,7 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.ResultMatcher;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
@ -161,7 +163,7 @@ public class NamespaceHttpHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers(); .headers(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -176,9 +178,9 @@ public class NamespaceHttpHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.cacheControl(); .cacheControl(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -193,9 +195,9 @@ public class NamespaceHttpHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.httpStrictTransportSecurity(); .httpStrictTransportSecurity(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -210,13 +212,13 @@ public class NamespaceHttpHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
// hsts@request-matcher-ref, hsts@max-age-seconds, hsts@include-subdomains // hsts@request-matcher-ref, hsts@max-age-seconds, hsts@include-subdomains
.defaultsDisabled() .defaultsDisabled()
.httpStrictTransportSecurity() .httpStrictTransportSecurity((hsts) -> hsts
.requestMatcher(AnyRequestMatcher.INSTANCE) .requestMatcher(AnyRequestMatcher.INSTANCE)
.maxAgeInSeconds(15768000) .maxAgeInSeconds(15768000)
.includeSubDomains(false); .includeSubDomains(false)));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -231,11 +233,10 @@ public class NamespaceHttpHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
// frame-options@policy=SAMEORIGIN // frame-options@policy=SAMEORIGIN
.defaultsDisabled() .defaultsDisabled()
.frameOptions() .frameOptions((frameOptions) -> frameOptions.sameOrigin()));
.sameOrigin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -250,11 +251,11 @@ public class NamespaceHttpHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
// frame-options@ref // frame-options@ref
.defaultsDisabled() .defaultsDisabled()
.addHeaderWriter(new XFrameOptionsHeaderWriter( .addHeaderWriter(new XFrameOptionsHeaderWriter(
new StaticAllowFromStrategy(URI.create("https://example.com")))); new StaticAllowFromStrategy(URI.create("https://example.com")))));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -269,10 +270,10 @@ public class NamespaceHttpHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
// xss-protection // xss-protection
.defaultsDisabled() .defaultsDisabled()
.xssProtection(); .xssProtection(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -287,11 +288,11 @@ public class NamespaceHttpHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
// xss-protection@enabled and xss-protection@block // xss-protection@enabled and xss-protection@block
.defaultsDisabled() .defaultsDisabled()
.xssProtection() .xssProtection((xss) -> xss
.headerValue(XXssProtectionHeaderWriter.HeaderValue.ENABLED_MODE_BLOCK); .headerValue(XXssProtectionHeaderWriter.HeaderValue.ENABLED_MODE_BLOCK)));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -306,10 +307,10 @@ public class NamespaceHttpHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
// content-type-options // content-type-options
.defaultsDisabled() .defaultsDisabled()
.contentTypeOptions(); .contentTypeOptions(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -324,9 +325,9 @@ public class NamespaceHttpHeadersTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.headers() .headers((headers) -> headers
.defaultsDisabled() .defaultsDisabled()
.addHeaderWriter(new StaticHeadersWriter("customHeaderName", "customHeaderValue")); .addHeaderWriter(new StaticHeadersWriter("customHeaderName", "customHeaderValue")));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -118,7 +118,7 @@ public class NamespaceHttpInterceptUrlTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests().requestMatchers( .authorizeRequests((requests) -> requests.requestMatchers(
// the line below is similar to intercept-url@pattern: // the line below is similar to intercept-url@pattern:
// <intercept-url pattern="/users**" access="hasRole('ROLE_ADMIN')"/> // <intercept-url pattern="/users**" access="hasRole('ROLE_ADMIN')"/>
//" access="hasRole('ROLE_ADMIN')"/> //" access="hasRole('ROLE_ADMIN')"/>
@ -128,14 +128,13 @@ public class NamespaceHttpInterceptUrlTests {
//" access="hasRole('ROLE_ADMIN')" method="POST"/> //" access="hasRole('ROLE_ADMIN')" method="POST"/>
HttpMethod.POST, "/admin/post", "/admin/another-post/**").hasRole("ADMIN") HttpMethod.POST, "/admin/post", "/admin/another-post/**").hasRole("ADMIN")
.requestMatchers("/signup").permitAll() .requestMatchers("/signup").permitAll()
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .requiresChannel((channel) -> channel.requestMatchers("/login", "/secured/**")
.requiresChannel().requestMatchers("/login", "/secured/**")
// NOTE: channel security is configured separately of authorization (i.e. intercept-url@access // NOTE: channel security is configured separately of authorization (i.e. intercept-url@access
// the line below is similar to intercept-url@requires-channel="https": // the line below is similar to intercept-url@requires-channel="https":
// <intercept-url pattern="/login" requires-channel="https"/> // <intercept-url pattern="/login" requires-channel="https"/>
//" requires-channel="https"/> //" requires-channel="https"/>
.requiresSecure().anyRequest().requiresInsecure(); .requiresSecure().anyRequest().requiresInsecure());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -104,11 +104,10 @@ public class NamespaceHttpJeeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("user") .anyRequest().hasRole("user"))
.and() .jee((jee) -> jee
.jee() .mappableRoles("user", "admin"));
.mappableRoles("user", "admin");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -126,12 +125,11 @@ public class NamespaceHttpJeeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("user") .anyRequest().hasRole("user"))
.and() .jee((jee) -> jee
.jee()
.mappableAuthorities("ROLE_user", "ROLE_admin") .mappableAuthorities("ROLE_user", "ROLE_admin")
.authenticatedUserDetailsService(this.authenticationUserDetailsService); .authenticatedUserDetailsService(this.authenticationUserDetailsService));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -196,11 +196,11 @@ public class NamespaceHttpLogoutTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.logout() .logout((logout) -> logout
.deleteCookies("remove") // logout@delete-cookies .deleteCookies("remove") // logout@delete-cookies
.invalidateHttpSession(false) // logout@invalidate-session=false (default is true) .invalidateHttpSession(false) // logout@invalidate-session=false (default is true)
.logoutUrl("/custom-logout") // logout@logout-url (default is /logout) .logoutUrl("/custom-logout") // logout@logout-url (default is /logout)
.logoutSuccessUrl("/logout-success"); .logoutSuccessUrl("/logout-success"));
return http.build(); // logout@success-url (default is /login?logout) return http.build(); // logout@success-url (default is /login?logout)
// @formatter:on // @formatter:on
} }
@ -237,8 +237,8 @@ public class NamespaceHttpLogoutTests {
logoutSuccessHandler.setDefaultTargetUrl("/SuccessHandlerRefHttpLogoutConfig"); logoutSuccessHandler.setDefaultTargetUrl("/SuccessHandlerRefHttpLogoutConfig");
// @formatter:off // @formatter:off
http http
.logout() .logout((logout) -> logout
.logoutSuccessHandler(logoutSuccessHandler); .logoutSuccessHandler(logoutSuccessHandler));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -70,15 +70,13 @@ public class NamespaceHttpPortMappingsTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .portMapper((mapper) -> mapper
.portMapper() .http(9080).mapsTo(9443))
.http(9080).mapsTo(9443) .requiresChannel((channel) -> channel
.and()
.requiresChannel()
.requestMatchers("/login", "/secured/**").requiresSecure() .requestMatchers("/login", "/secured/**").requiresSecure()
.anyRequest().requiresInsecure(); .anyRequest().requiresInsecure());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -88,11 +88,10 @@ public class NamespaceHttpRequestCacheTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .requestCache((cache) -> cache
.requestCache() .requestCache(requestCache()));
.requestCache(requestCache());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -117,8 +116,8 @@ public class NamespaceHttpRequestCacheTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated(); .anyRequest().authenticated());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -112,11 +112,10 @@ public class NamespaceHttpServerAccessDeniedHandlerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().denyAll() .anyRequest().denyAll())
.and() .exceptionHandling((handling) -> handling
.exceptionHandling() .accessDeniedPage("/AccessDeniedPageConfig"));
.accessDeniedPage("/AccessDeniedPageConfig");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -152,11 +151,10 @@ public class NamespaceHttpServerAccessDeniedHandlerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().denyAll() .anyRequest().denyAll())
.and() .exceptionHandling((handling) -> handling
.exceptionHandling() .accessDeniedHandler(accessDeniedHandler()));
.accessDeniedHandler(accessDeniedHandler());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -54,6 +54,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.x509; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.x509;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
@ -153,10 +154,9 @@ public class NamespaceHttpX509Tests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .x509(withDefaults());
.x509();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -182,11 +182,10 @@ public class NamespaceHttpX509Tests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .x509((x509) -> x509
.x509() .authenticationDetailsSource(authenticationDetailsSource()));
.authenticationDetailsSource(authenticationDetailsSource());
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -217,11 +216,10 @@ public class NamespaceHttpX509Tests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .x509((x509) -> x509
.x509() .subjectPrincipalRegex("CN=(.*?)@example.com(?:,|$)"));
.subjectPrincipalRegex("CN=(.*?)@example.com(?:,|$)");
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -247,11 +245,10 @@ public class NamespaceHttpX509Tests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .x509((x509) -> x509
.x509() .x509PrincipalExtractor(this::extractCommonName));
.x509PrincipalExtractor(this::extractCommonName);
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -282,11 +279,10 @@ public class NamespaceHttpX509Tests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .x509((x509) -> x509
.x509() .userDetailsService((username) -> USER));
.userDetailsService((username) -> USER);
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -312,11 +308,10 @@ public class NamespaceHttpX509Tests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .x509((x509) -> x509
.x509() .authenticationUserDetailsService((authentication) -> USER));
.authenticationUserDetailsService((authentication) -> USER);
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -57,6 +57,7 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@ -287,12 +288,10 @@ public class NamespaceRememberMeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin(withDefaults())
.formLogin() .rememberMe(withDefaults());
.and()
.rememberMe();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -313,10 +312,9 @@ public class NamespaceRememberMeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .rememberMe((me) -> me
.rememberMe() .rememberMeServices(REMEMBER_ME_SERVICES));
.rememberMeServices(REMEMBER_ME_SERVICES);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -333,10 +331,9 @@ public class NamespaceRememberMeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .rememberMe((me) -> me
.rememberMe() .authenticationSuccessHandler(SUCCESS_HANDLER));
.authenticationSuccessHandler(SUCCESS_HANDLER);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -354,10 +351,9 @@ public class NamespaceRememberMeTests {
http http
.securityMatcher(new AntPathRequestMatcher("/without-key/**")) .securityMatcher(new AntPathRequestMatcher("/without-key/**"))
.authorizeHttpRequests((requests) -> requests.anyRequest().authenticated()) .authorizeHttpRequests((requests) -> requests.anyRequest().authenticated())
.formLogin() .formLogin((login) -> login
.loginProcessingUrl("/without-key/login") .loginProcessingUrl("/without-key/login"))
.and() .rememberMe(withDefaults());
.rememberMe();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -367,13 +363,11 @@ public class NamespaceRememberMeTests {
SecurityFilterChain keyFilterChain(HttpSecurity http) throws Exception { SecurityFilterChain keyFilterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin(withDefaults())
.formLogin() .rememberMe((me) -> me
.and() .key("KeyConfig"));
.rememberMe()
.key("KeyConfig");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -392,10 +386,9 @@ public class NamespaceRememberMeTests {
// tokenRepository.setDataSource(dataSource); // tokenRepository.setDataSource(dataSource);
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .rememberMe((me) -> me
.rememberMe() .tokenRepository(TOKEN_REPOSITORY));
.tokenRepository(TOKEN_REPOSITORY);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -410,13 +403,11 @@ public class NamespaceRememberMeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin(withDefaults())
.formLogin() .rememberMe((me) -> me
.and() .tokenValiditySeconds(314));
.rememberMe()
.tokenValiditySeconds(314);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -431,10 +422,9 @@ public class NamespaceRememberMeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .rememberMe((me) -> me
.rememberMe() .useSecureCookie(true));
.useSecureCookie(true);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -449,10 +439,9 @@ public class NamespaceRememberMeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .rememberMe((me) -> me
.rememberMe() .rememberMeParameter("rememberMe"));
.rememberMeParameter("rememberMe");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -467,10 +456,9 @@ public class NamespaceRememberMeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .rememberMe((me) -> me
.rememberMe() .rememberMeCookieName("rememberMe"));
.rememberMeCookieName("rememberMe");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -487,9 +475,8 @@ public class NamespaceRememberMeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .rememberMe(withDefaults());
.rememberMe();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -511,10 +498,9 @@ public class NamespaceRememberMeTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .rememberMe((me) -> me
.rememberMe() .userDetailsService(USERDETAILS_SERVICE));
.userDetailsService(USERDETAILS_SERVICE);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -63,6 +63,7 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
@ -287,18 +288,16 @@ public class NamespaceSessionManagementTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .httpBasic(withDefaults())
.httpBasic() .sessionManagement((management) -> management
.and()
.sessionManagement()
.invalidSessionUrl("/invalid-session") // session-management@invalid-session-url .invalidSessionUrl("/invalid-session") // session-management@invalid-session-url
.sessionAuthenticationErrorUrl("/session-auth-error") // session-management@session-authentication-error-url .sessionAuthenticationErrorUrl("/session-auth-error") // session-management@session-authentication-error-url
.maximumSessions(1) // session-management/concurrency-control@max-sessions .maximumSessions(1) // session-management/concurrency-control@max-sessions
.maxSessionsPreventsLogin(true) // session-management/concurrency-control@error-if-maximum-exceeded .maxSessionsPreventsLogin(true) // session-management/concurrency-control@error-if-maximum-exceeded
.expiredUrl("/expired-session") // session-management/concurrency-control@expired-url .expiredUrl("/expired-session") // session-management/concurrency-control@expired-url
.sessionRegistry(sessionRegistry()); .sessionRegistry(sessionRegistry()));
return http.build(); // session-management/concurrency-control@session-registry-ref return http.build(); // session-management/concurrency-control@session-registry-ref
// @formatter:on // @formatter:on
} }
@ -320,8 +319,8 @@ public class NamespaceSessionManagementTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement() .sessionManagement((management) -> management
.invalidSessionStrategy(invalidSessionStrategy()); .invalidSessionStrategy(invalidSessionStrategy()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -343,10 +342,9 @@ public class NamespaceSessionManagementTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement() .sessionManagement((management) -> management
.sessionAuthenticationStrategy(sessionAuthenticationStrategy()) // session-management@session-authentication-strategy-ref .sessionAuthenticationStrategy(sessionAuthenticationStrategy()))
.and() .httpBasic(withDefaults());
.httpBasic();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -366,10 +364,9 @@ public class NamespaceSessionManagementTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement() .sessionManagement((management) -> management
.sessionAuthenticationStrategy(new NullAuthenticatedSessionStrategy()) .sessionAuthenticationStrategy(new NullAuthenticatedSessionStrategy()))
.and() .httpBasic(withDefaults());
.httpBasic();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -384,10 +381,9 @@ public class NamespaceSessionManagementTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement() .sessionManagement((management) -> management
.requireExplicitAuthenticationStrategy(false) .requireExplicitAuthenticationStrategy(false))
.and() .httpBasic(withDefaults());
.httpBasic();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -405,7 +401,7 @@ public class NamespaceSessionManagementTests {
.sessionManagement((sessions) -> sessions .sessionManagement((sessions) -> sessions
.requireExplicitAuthenticationStrategy(false) .requireExplicitAuthenticationStrategy(false)
) )
.httpBasic(); .httpBasic(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -429,7 +425,7 @@ public class NamespaceSessionManagementTests {
.sessionFixation().newSession() .sessionFixation().newSession()
.requireExplicitAuthenticationStrategy(false) .requireExplicitAuthenticationStrategy(false)
) )
.httpBasic(); .httpBasic(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -100,12 +100,11 @@ public class PermitAllSupportTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin((login) -> login
.formLogin()
.loginPage("/xyz").permitAll() .loginPage("/xyz").permitAll()
.loginProcessingUrl("/abc?def").permitAll(); .loginProcessingUrl("/abc?def").permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -120,12 +119,11 @@ public class PermitAllSupportTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeHttpRequests() .authorizeHttpRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin((login) -> login
.formLogin()
.loginPage("/xyz").permitAll() .loginPage("/xyz").permitAll()
.loginProcessingUrl("/abc?def").permitAll(); .loginProcessingUrl("/abc?def").permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -140,15 +138,13 @@ public class PermitAllSupportTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .authorizeHttpRequests((requests) -> requests
.authorizeHttpRequests() .anyRequest().authenticated())
.anyRequest().authenticated() .formLogin((login) -> login
.and()
.formLogin()
.loginPage("/xyz").permitAll() .loginPage("/xyz").permitAll()
.loginProcessingUrl("/abc?def").permitAll(); .loginProcessingUrl("/abc?def").permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -163,8 +159,8 @@ public class PermitAllSupportTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin((login) -> login
.permitAll(); .permitAll());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -32,6 +32,7 @@ import org.springframework.security.web.PortMapperImpl;
import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.SecurityFilterChain;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
@ -73,13 +74,11 @@ public class PortMapperConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.requiresChannel() .requiresChannel((channel) -> channel
.anyRequest().requiresSecure() .anyRequest().requiresSecure())
.and() .portMapper((mapper) -> mapper
.portMapper() .http(543).mapsTo(123))
.http(543).mapsTo(123) .portMapper(withDefaults());
.and()
.portMapper();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -365,12 +365,10 @@ public class RememberMeConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin(withDefaults())
.formLogin() .rememberMe(withDefaults());
.and()
.rememberMe();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -398,8 +396,8 @@ public class RememberMeConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.rememberMe() .rememberMe((me) -> me
.userDetailsService(new AuthenticationManagerBuilder(this.objectPostProcessor).getDefaultUserDetailsService()); .userDetailsService(new AuthenticationManagerBuilder(this.objectPostProcessor).getDefaultUserDetailsService()));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -435,12 +433,10 @@ public class RememberMeConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .rememberMe((me) -> me
.rememberMe() .userDetailsService(userDetailsService))
.userDetailsService(userDetailsService) .rememberMe(withDefaults());
.and()
.rememberMe();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -489,12 +485,10 @@ public class RememberMeConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin(withDefaults())
.formLogin() .rememberMe(withDefaults());
.and()
.rememberMe();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -539,13 +533,11 @@ public class RememberMeConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin(withDefaults())
.formLogin() .rememberMe((me) -> me
.and() .rememberMeCookieDomain("spring.io"));
.rememberMe()
.rememberMeCookieDomain("spring.io");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -595,15 +587,13 @@ public class RememberMeConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin(withDefaults())
.formLogin() .rememberMe((me) -> me
.and()
.rememberMe()
.rememberMeCookieName("SPRING_COOKIE_DOMAIN") .rememberMeCookieName("SPRING_COOKIE_DOMAIN")
.rememberMeCookieDomain("spring.io") .rememberMeCookieDomain("spring.io")
.rememberMeServices(REMEMBER_ME); .rememberMeServices(REMEMBER_ME));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -627,13 +617,11 @@ public class RememberMeConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().hasRole("USER") .anyRequest().hasRole("USER"))
.and() .formLogin(withDefaults())
.formLogin() .rememberMe((me) -> me
.and() .rememberMeServices(new TokenBasedRememberMeServices("key", userDetailsService())));
.rememberMe()
.rememberMeServices(new TokenBasedRememberMeServices("key", userDetailsService()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -328,7 +328,7 @@ public class RequestCacheConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.requestCache(); .requestCache(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -359,10 +359,9 @@ public class RequestCacheConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.requestCache() .requestCache((cache) -> cache
.requestCache(requestCache) .requestCache(requestCache))
.and() .requestCache(withDefaults());
.requestCache();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -377,10 +376,9 @@ public class RequestCacheConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin(withDefaults());
.formLogin();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -78,14 +78,12 @@ public class RequestMatcherConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.securityMatchers() .securityMatchers((security) -> security
.requestMatchers(new AntPathRequestMatcher("/api/**")) .requestMatchers(new AntPathRequestMatcher("/api/**")))
.and() .securityMatchers((security) -> security
.securityMatchers() .requestMatchers(new AntPathRequestMatcher("/oauth/**")))
.requestMatchers(new AntPathRequestMatcher("/oauth/**")) .authorizeRequests((requests) -> requests
.and() .anyRequest().denyAll());
.authorizeRequests()
.anyRequest().denyAll();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -152,7 +152,7 @@ public class SecurityContextConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.securityContext(); .securityContext(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -183,10 +183,9 @@ public class SecurityContextConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.securityContext() .securityContext((context) -> context
.securityContextRepository(SCR) .securityContextRepository(SCR))
.and() .securityContext(withDefaults());
.securityContext();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -203,14 +202,11 @@ public class SecurityContextConfigurerTests {
// @formatter:off // @formatter:off
http http
.addFilter(new WebAsyncManagerIntegrationFilter()) .addFilter(new WebAsyncManagerIntegrationFilter())
.anonymous() .anonymous(withDefaults())
.and() .securityContext(withDefaults())
.securityContext() .authorizeRequests((requests) -> requests
.and() .anyRequest().permitAll())
.authorizeRequests() .httpBasic(withDefaults());
.anyRequest().permitAll()
.and()
.httpBasic();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -224,7 +224,7 @@ public class ServletApiConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.servletApi(); .servletApi(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -286,13 +286,11 @@ public class ServletApiConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .exceptionHandling((handling) -> handling
.exceptionHandling() .authenticationEntryPoint(ENTRYPOINT))
.authenticationEntryPoint(ENTRYPOINT) .formLogin(withDefaults());
.and()
.formLogin();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -312,10 +310,9 @@ public class ServletApiConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.servletApi() .servletApi((api) -> api
.rolePrefix("PERMISSION_") .rolePrefix("PERMISSION_"))
.and() .servletApi(withDefaults());
.servletApi();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -392,8 +389,8 @@ public class ServletApiConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.servletApi().and() .servletApi(withDefaults())
.logout(); .logout(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -408,7 +405,7 @@ public class ServletApiConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf().disable(); .csrf((csrf) -> csrf.disable());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -45,6 +45,7 @@ import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.security.web.csrf.XorCsrfTokenRequestAttributeHandler; import org.springframework.security.web.csrf.XorCsrfTokenRequestAttributeHandler;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.config.Customizer.withDefaults;
/** /**
* @author Rob Winch * @author Rob Winch
@ -122,9 +123,8 @@ public class SessionManagementConfigurerServlet31Tests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .sessionManagement(withDefaults());
.sessionManagement();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -39,6 +39,7 @@ import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
/** /**
@ -72,10 +73,9 @@ public class SessionManagementConfigurerSessionAuthenticationStrategyTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .sessionManagement((management) -> management
.sessionManagement() .sessionAuthenticationStrategy(customSessionAuthenticationStrategy));
.sessionAuthenticationStrategy(customSessionAuthenticationStrategy);
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -93,7 +93,7 @@ public class SessionManagementConfigurerSessionCreationPolicyTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); .sessionManagement((management) -> management.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
// @formatter:on // @formatter:on
http.setSharedObject(SessionCreationPolicy.class, SessionCreationPolicy.ALWAYS); http.setSharedObject(SessionCreationPolicy.class, SessionCreationPolicy.ALWAYS);
return http.build(); return http.build();

View File

@ -551,11 +551,10 @@ public class SessionManagementConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.requestCache() .requestCache((cache) -> cache
.requestCache(REQUEST_CACHE) .requestCache(REQUEST_CACHE))
.and() .sessionManagement((management) -> management
.sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS));
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -572,11 +571,10 @@ public class SessionManagementConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.securityContext() .securityContext((context) -> context
.securityContextRepository(SECURITY_CONTEXT_REPO) .securityContextRepository(SECURITY_CONTEXT_REPO))
.and() .sessionManagement((management) -> management
.sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS));
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -591,10 +589,9 @@ public class SessionManagementConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement() .sessionManagement((management) -> management
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) .sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.and() .sessionManagement(withDefaults());
.sessionManagement();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -609,11 +606,10 @@ public class SessionManagementConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic() .httpBasic(withDefaults())
.and() .sessionManagement((management) -> management
.sessionManagement()
.sessionFixation().none() .sessionFixation().none()
.maximumSessions(1); .maximumSessions(1));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -658,11 +654,10 @@ public class SessionManagementConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin(withDefaults())
.and() .sessionManagement((management) -> management
.sessionManagement()
.maximumSessions(1) .maximumSessions(1)
.maxSessionsPreventsLogin(true); .maxSessionsPreventsLogin(true));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }
@ -766,8 +761,8 @@ public class SessionManagementConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement() .sessionManagement((management) -> management
.maximumSessions(1); .maximumSessions(1));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -818,8 +813,8 @@ public class SessionManagementConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement() .sessionManagement((management) -> management
.maximumSessions(1); .maximumSessions(1));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -843,8 +838,8 @@ public class SessionManagementConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement() .sessionManagement((management) -> management
.maximumSessions(1); .maximumSessions(1));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -72,7 +72,7 @@ public class SessionManagementConfigurerTransientAuthenticationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.csrf().disable() .csrf((csrf) -> csrf.disable())
.authenticationProvider(new TransientAuthenticationProvider()); .authenticationProvider(new TransientAuthenticationProvider());
// @formatter:on // @formatter:on
return http.build(); return http.build();
@ -88,7 +88,7 @@ public class SessionManagementConfigurerTransientAuthenticationTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS); .sessionManagement((management) -> management.sessionCreationPolicy(SessionCreationPolicy.ALWAYS));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -49,6 +49,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector; import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.config.Customizer.withDefaults;
/** /**
* @author Rob Winch * @author Rob Winch
@ -135,7 +136,7 @@ public class UrlAuthorizationConfigurerTests {
HandlerMappingIntrospector introspector) throws Exception { HandlerMappingIntrospector introspector) throws Exception {
// @formatter:off // @formatter:off
http http
.httpBasic().and() .httpBasic(withDefaults())
.apply(new UrlAuthorizationConfigurer(context)).getRegistry() .apply(new UrlAuthorizationConfigurer(context)).getRegistry()
.requestMatchers(new MvcRequestMatcher(introspector, "/path")).hasRole("ADMIN"); .requestMatchers(new MvcRequestMatcher(introspector, "/path")).hasRole("ADMIN");
// @formatter:on // @formatter:on
@ -171,7 +172,7 @@ public class UrlAuthorizationConfigurerTests {
mvcRequestMatcher.setServletPath("/spring"); mvcRequestMatcher.setServletPath("/spring");
// @formatter:off // @formatter:off
http http
.httpBasic().and() .httpBasic(withDefaults())
.apply(new UrlAuthorizationConfigurer(context)).getRegistry() .apply(new UrlAuthorizationConfigurer(context)).getRegistry()
.requestMatchers(mvcRequestMatcher).hasRole("ADMIN"); .requestMatchers(mvcRequestMatcher).hasRole("ADMIN");
// @formatter:on // @formatter:on

View File

@ -141,13 +141,13 @@ public class UrlAuthorizationsTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/role-user-authority").hasAnyAuthority("ROLE_USER") .requestMatchers("/role-user-authority").hasAnyAuthority("ROLE_USER")
.requestMatchers("/role-admin-authority").hasAnyAuthority("ROLE_ADMIN") .requestMatchers("/role-admin-authority").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/role-user-admin-authority").hasAnyAuthority("ROLE_USER", "ROLE_ADMIN") .requestMatchers("/role-user-admin-authority").hasAnyAuthority("ROLE_USER", "ROLE_ADMIN")
.requestMatchers("/role-user").hasAnyRole("USER") .requestMatchers("/role-user").hasAnyRole("USER")
.requestMatchers("/role-admin").hasAnyRole("ADMIN") .requestMatchers("/role-admin").hasAnyRole("ADMIN")
.requestMatchers("/role-user-admin").hasAnyRole("USER", "ADMIN"); .requestMatchers("/role-user-admin").hasAnyRole("USER", "ADMIN"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -199,7 +199,7 @@ public class X509ConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.x509(); .x509(withDefaults());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -228,10 +228,9 @@ public class X509ConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.x509() .x509((x509) -> x509
.subjectPrincipalRegex("CN=(.*?)@example.com(?:,|$)") .subjectPrincipalRegex("CN=(.*?)@example.com(?:,|$)"))
.and() .x509(withDefaults());
.x509();
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -354,17 +354,15 @@ public class OAuth2ClientConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .requestCache((cache) -> cache
.requestCache() .requestCache(requestCache))
.requestCache(requestCache) .oauth2Client((client) -> client
.and() .authorizationCodeGrant((code) -> code
.oauth2Client()
.authorizationCodeGrant()
.authorizationRequestResolver(authorizationRequestResolver) .authorizationRequestResolver(authorizationRequestResolver)
.authorizationRedirectStrategy(authorizationRedirectStrategy) .authorizationRedirectStrategy(authorizationRedirectStrategy)
.accessTokenResponseClient(accessTokenResponseClient); .accessTokenResponseClient(accessTokenResponseClient)));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -44,6 +44,7 @@ import org.springframework.mock.web.MockFilterChain;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent; import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
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;
@ -118,6 +119,7 @@ import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyNoInteractions;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.config.annotation.SecurityContextChangedListenerArgumentMatchers.setAuthentication; import static org.springframework.security.config.annotation.SecurityContextChangedListenerArgumentMatchers.setAuthentication;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
@ -780,9 +782,9 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository( .clientRegistrationRepository(
new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)); new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)));
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -805,10 +807,9 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository(this.clientRegistrationRepository) .clientRegistrationRepository(this.clientRegistrationRepository))
.and() .formLogin(withDefaults());
.formLogin();
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -850,11 +851,11 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository( .clientRegistrationRepository(
new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)) new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION))
.userInfoEndpoint() .userInfoEndpoint((info) -> info
.userAuthoritiesMapper(createGrantedAuthoritiesMapper()); .userAuthoritiesMapper(createGrantedAuthoritiesMapper())));
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -869,7 +870,7 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login(); .oauth2Login(withDefaults());
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -894,15 +895,13 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .securityContext((context) -> context
.securityContext() .securityContextRepository(securityContextRepository()))
.securityContextRepository(securityContextRepository()) .oauth2Login((login) -> login
.and() .tokenEndpoint((token) -> token
.oauth2Login() .accessTokenResponseClient(createOauth2AccessTokenResponseClient())));
.tokenEndpoint()
.accessTokenResponseClient(createOauth2AccessTokenResponseClient());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -947,10 +946,10 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository( .clientRegistrationRepository(
new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)) new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION))
.loginProcessingUrl("/login/oauth2/*"); .loginProcessingUrl("/login/oauth2/*"));
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -970,10 +969,10 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository(this.clientRegistrationRepository) .clientRegistrationRepository(this.clientRegistrationRepository)
.authorizationEndpoint() .authorizationEndpoint((authorize) -> authorize
.authorizationRequestResolver(this.resolver); .authorizationRequestResolver(this.resolver)));
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -991,9 +990,9 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository(this.clientRegistrationRepository) .clientRegistrationRepository(this.clientRegistrationRepository)
.authorizationEndpoint(); .authorizationEndpoint(Customizer.withDefaults()));
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -1106,10 +1105,10 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository( .clientRegistrationRepository(
new InMemoryClientRegistrationRepository( new InMemoryClientRegistrationRepository(
GOOGLE_CLIENT_REGISTRATION, GITHUB_CLIENT_REGISTRATION)); GOOGLE_CLIENT_REGISTRATION, GITHUB_CLIENT_REGISTRATION)));
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -1124,10 +1123,10 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository( .clientRegistrationRepository(
new InMemoryClientRegistrationRepository( new InMemoryClientRegistrationRepository(
GOOGLE_CLIENT_REGISTRATION, CLIENT_CREDENTIALS_REGISTRATION)); GOOGLE_CLIENT_REGISTRATION, CLIENT_CREDENTIALS_REGISTRATION)));
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -1142,10 +1141,10 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository( .clientRegistrationRepository(
new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)) new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION))
.loginPage("/custom-login"); .loginPage("/custom-login"));
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -1180,8 +1179,8 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.logout() .logout((logout) -> logout
.logoutSuccessHandler(oidcLogoutSuccessHandler()); .logoutSuccessHandler(oidcLogoutSuccessHandler()));
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -1209,11 +1208,10 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository( .clientRegistrationRepository(
new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)) new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)))
.and() .httpBasic(withDefaults());
.httpBasic();
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -1254,14 +1252,13 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2Login() .oauth2Login((login) -> login
.clientRegistrationRepository( .clientRegistrationRepository(
new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)) new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)))
.and() .exceptionHandling((handling) -> handling
.exceptionHandling()
.defaultAuthenticationEntryPointFor( .defaultAuthenticationEntryPointFor(
new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED), new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED),
new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest")); new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest")));
// @formatter:on // @formatter:on
return super.configureFilterChain(http); return super.configureFilterChain(http);
} }
@ -1312,19 +1309,16 @@ public class OAuth2LoginConfigurerTests {
SecurityFilterChain configureFilterChain(HttpSecurity http) throws Exception { SecurityFilterChain configureFilterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .securityContext((context) -> context
.securityContext() .securityContextRepository(securityContextRepository()))
.securityContextRepository(securityContextRepository()) .oauth2Login((login) -> login
.and() .tokenEndpoint((token) -> token
.oauth2Login() .accessTokenResponseClient(createOauth2AccessTokenResponseClient()))
.tokenEndpoint() .userInfoEndpoint((info) -> info
.accessTokenResponseClient(createOauth2AccessTokenResponseClient())
.and()
.userInfoEndpoint()
.userService(createOauth2UserService()) .userService(createOauth2UserService())
.oidcUserService(createOidcUserService()); .oidcUserService(createOidcUserService())));
// @formatter:on // @formatter:on
return http.build(); return http.build();
} }

View File

@ -82,10 +82,10 @@ import org.springframework.security.authentication.AuthenticationManagerResolver
import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.AuthenticationServiceException; import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.TestingAuthenticationToken; import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.ObjectPostProcessor; import org.springframework.security.config.ObjectPostProcessor;
import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
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.configurers.AbstractHttpConfigurer; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
@ -163,7 +163,6 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults; import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
@ -815,15 +814,16 @@ public class OAuth2ResourceServerConfigurerTests {
@Test @Test
public void getJwtDecoderWhenConfiguredWithDecoderAndJwkSetUriThenLastOneWins() { public void getJwtDecoderWhenConfiguredWithDecoderAndJwkSetUriThenLastOneWins() {
ApplicationContext context = mock(ApplicationContext.class); ApplicationContext context = mock(ApplicationContext.class);
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt();
JwtDecoder decoder = mock(JwtDecoder.class); JwtDecoder decoder = mock(JwtDecoder.class);
jwtConfigurer.jwkSetUri(JWK_SET_URI); new OAuth2ResourceServerConfigurer<HttpSecurity>(context).jwt((jwt) -> {
jwtConfigurer.decoder(decoder); jwt.jwkSetUri(JWK_SET_URI);
assertThat(jwtConfigurer.getJwtDecoder()).isEqualTo(decoder); jwt.decoder(decoder);
jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt(); assertThat(jwt.getJwtDecoder()).isEqualTo(decoder);
jwtConfigurer.decoder(decoder); });
jwtConfigurer.jwkSetUri(JWK_SET_URI); new OAuth2ResourceServerConfigurer<HttpSecurity>(context).jwt((jwt) -> {
assertThat(jwtConfigurer.getJwtDecoder()).isInstanceOf(NimbusJwtDecoder.class); jwt.decoder(decoder).jwkSetUri(JWK_SET_URI);
assertThat(jwt.getJwtDecoder()).isInstanceOf(NimbusJwtDecoder.class);
});
} }
@Test @Test
@ -832,9 +832,10 @@ public class OAuth2ResourceServerConfigurerTests {
JwtDecoder decoder = mock(JwtDecoder.class); JwtDecoder decoder = mock(JwtDecoder.class);
ApplicationContext context = mock(ApplicationContext.class); ApplicationContext context = mock(ApplicationContext.class);
given(context.getBean(JwtDecoder.class)).willReturn(decoderBean); given(context.getBean(JwtDecoder.class)).willReturn(decoderBean);
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt(); new OAuth2ResourceServerConfigurer<HttpSecurity>(context).jwt((jwt) -> {
jwtConfigurer.decoder(decoder); jwt.decoder(decoder);
assertThat(jwtConfigurer.getJwtDecoder()).isEqualTo(decoder); assertThat(jwt.getJwtDecoder()).isEqualTo(decoder);
});
} }
@Test @Test
@ -842,10 +843,11 @@ public class OAuth2ResourceServerConfigurerTests {
JwtDecoder decoder = mock(JwtDecoder.class); JwtDecoder decoder = mock(JwtDecoder.class);
ApplicationContext context = mock(ApplicationContext.class); ApplicationContext context = mock(ApplicationContext.class);
given(context.getBean(JwtDecoder.class)).willReturn(decoder); given(context.getBean(JwtDecoder.class)).willReturn(decoder);
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt(); new OAuth2ResourceServerConfigurer<HttpSecurity>(context).jwt((jwt) -> {
jwtConfigurer.jwkSetUri(JWK_SET_URI); jwt.jwkSetUri(JWK_SET_URI);
assertThat(jwtConfigurer.getJwtDecoder()).isNotEqualTo(decoder); assertThat(jwt.getJwtDecoder()).isNotEqualTo(decoder);
assertThat(jwtConfigurer.getJwtDecoder()).isInstanceOf(NimbusJwtDecoder.class); assertThat(jwt.getJwtDecoder()).isInstanceOf(NimbusJwtDecoder.class);
});
} }
@Test @Test
@ -856,9 +858,10 @@ public class OAuth2ResourceServerConfigurerTests {
context.registerBean("decoderOne", JwtDecoder.class, () -> decoderBean); context.registerBean("decoderOne", JwtDecoder.class, () -> decoderBean);
context.registerBean("decoderTwo", JwtDecoder.class, () -> decoderBean); context.registerBean("decoderTwo", JwtDecoder.class, () -> decoderBean);
this.spring.context(context).autowire(); this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt(); new OAuth2ResourceServerConfigurer<HttpSecurity>(context).jwt((jwt) -> {
jwtConfigurer.decoder(decoder); jwt.decoder(decoder);
assertThat(jwtConfigurer.getJwtDecoder()).isEqualTo(decoder); assertThat(jwt.getJwtDecoder()).isEqualTo(decoder);
});
} }
@Test @Test
@ -868,8 +871,9 @@ public class OAuth2ResourceServerConfigurerTests {
context.registerBean("decoderOne", JwtDecoder.class, () -> decoder); context.registerBean("decoderOne", JwtDecoder.class, () -> decoder);
context.registerBean("decoderTwo", JwtDecoder.class, () -> decoder); context.registerBean("decoderTwo", JwtDecoder.class, () -> decoder);
this.spring.context(context).autowire(); this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt(); new OAuth2ResourceServerConfigurer<HttpSecurity>(context)
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(jwtConfigurer::getJwtDecoder); .jwt((jwt) -> assertThatExceptionOfType(NoUniqueBeanDefinitionException.class)
.isThrownBy(jwt::getJwtDecoder));
} }
@Test @Test
@ -1152,19 +1156,19 @@ public class OAuth2ResourceServerConfigurerTests {
@Test @Test
public void getIntrospectionClientWhenConfiguredWithClientAndIntrospectionUriThenLastOneWins() { public void getIntrospectionClientWhenConfiguredWithClientAndIntrospectionUriThenLastOneWins() {
ApplicationContext context = mock(ApplicationContext.class); ApplicationContext context = mock(ApplicationContext.class);
OAuth2ResourceServerConfigurer.OpaqueTokenConfigurer opaqueTokenConfigurer = new OAuth2ResourceServerConfigurer(
context)
.opaqueToken();
OpaqueTokenIntrospector client = mock(OpaqueTokenIntrospector.class); OpaqueTokenIntrospector client = mock(OpaqueTokenIntrospector.class);
opaqueTokenConfigurer.introspectionUri(INTROSPECTION_URI); new OAuth2ResourceServerConfigurer<HttpSecurity>(context).opaqueToken((opaqueToken) -> {
opaqueTokenConfigurer.introspectionClientCredentials(CLIENT_ID, CLIENT_SECRET); opaqueToken.introspectionUri(INTROSPECTION_URI);
opaqueTokenConfigurer.introspector(client); opaqueToken.introspectionClientCredentials(CLIENT_ID, CLIENT_SECRET);
assertThat(opaqueTokenConfigurer.getIntrospector()).isEqualTo(client); opaqueToken.introspector(client);
opaqueTokenConfigurer = new OAuth2ResourceServerConfigurer(context).opaqueToken(); assertThat(opaqueToken.getIntrospector()).isEqualTo(client);
opaqueTokenConfigurer.introspector(client); });
opaqueTokenConfigurer.introspectionUri(INTROSPECTION_URI); new OAuth2ResourceServerConfigurer<HttpSecurity>(context).opaqueToken((opaqueToken) -> {
opaqueTokenConfigurer.introspectionClientCredentials(CLIENT_ID, CLIENT_SECRET); opaqueToken.introspector(client);
assertThat(opaqueTokenConfigurer.getIntrospector()).isNotSameAs(client); opaqueToken.introspectionUri(INTROSPECTION_URI);
opaqueToken.introspectionClientCredentials(CLIENT_ID, CLIENT_SECRET);
assertThat(opaqueToken.getIntrospector()).isNotSameAs(client);
});
} }
@Test @Test
@ -1172,11 +1176,11 @@ public class OAuth2ResourceServerConfigurerTests {
GenericApplicationContext context = new GenericApplicationContext(); GenericApplicationContext context = new GenericApplicationContext();
registerMockBean(context, "introspectionClientOne", OpaqueTokenIntrospector.class); registerMockBean(context, "introspectionClientOne", OpaqueTokenIntrospector.class);
registerMockBean(context, "introspectionClientTwo", OpaqueTokenIntrospector.class); registerMockBean(context, "introspectionClientTwo", OpaqueTokenIntrospector.class);
OAuth2ResourceServerConfigurer.OpaqueTokenConfigurer opaqueToken = new OAuth2ResourceServerConfigurer(context) new OAuth2ResourceServerConfigurer<HttpSecurity>(context).opaqueToken((opaqueToken) -> {
.opaqueToken();
opaqueToken.introspectionUri(INTROSPECTION_URI); opaqueToken.introspectionUri(INTROSPECTION_URI);
opaqueToken.introspectionClientCredentials(CLIENT_ID, CLIENT_SECRET); opaqueToken.introspectionClientCredentials(CLIENT_ID, CLIENT_SECRET);
assertThat(opaqueToken.getIntrospector()).isNotNull(); assertThat(opaqueToken.getIntrospector()).isNotNull();
});
} }
@Test @Test
@ -1263,17 +1267,16 @@ public class OAuth2ResourceServerConfigurerTests {
@Test @Test
public void getAuthenticationManagerWhenConfiguredAuthenticationManagerThenTakesPrecedence() { public void getAuthenticationManagerWhenConfiguredAuthenticationManagerThenTakesPrecedence() {
ApplicationContext context = mock(ApplicationContext.class); ApplicationContext context = mock(ApplicationContext.class);
HttpSecurityBuilder http = mock(HttpSecurityBuilder.class); OAuth2ResourceServerConfigurer<HttpSecurity> oauth2ResourceServer = new OAuth2ResourceServerConfigurer<>(
OAuth2ResourceServerConfigurer oauth2ResourceServer = new OAuth2ResourceServerConfigurer(context); context);
AuthenticationManager authenticationManager = mock(AuthenticationManager.class); AuthenticationManager authenticationManager = mock(AuthenticationManager.class);
oauth2ResourceServer.jwt().authenticationManager(authenticationManager).decoder(mock(JwtDecoder.class)); oauth2ResourceServer
assertThat(oauth2ResourceServer.getAuthenticationManager(http)).isSameAs(authenticationManager); .jwt((jwt) -> jwt.authenticationManager(authenticationManager).decoder(mock(JwtDecoder.class)));
oauth2ResourceServer = new OAuth2ResourceServerConfigurer(context); assertThat(oauth2ResourceServer.getAuthenticationManager(null)).isSameAs(authenticationManager);
oauth2ResourceServer.opaqueToken() oauth2ResourceServer = new OAuth2ResourceServerConfigurer<>(context);
.authenticationManager(authenticationManager) oauth2ResourceServer.opaqueToken((opaqueToken) -> opaqueToken.authenticationManager(authenticationManager)
.introspector(mock(OpaqueTokenIntrospector.class)); .introspector(mock(OpaqueTokenIntrospector.class)));
assertThat(oauth2ResourceServer.getAuthenticationManager(http)).isSameAs(authenticationManager); assertThat(oauth2ResourceServer.getAuthenticationManager(null)).isSameAs(authenticationManager);
verify(http, never()).authenticationProvider(any(AuthenticationProvider.class));
} }
@Test @Test
@ -1343,8 +1346,9 @@ public class OAuth2ResourceServerConfigurerTests {
@Test @Test
public void getJwtAuthenticationConverterWhenNoConverterSpecifiedThenTheDefaultIsUsed() { public void getJwtAuthenticationConverterWhenNoConverterSpecifiedThenTheDefaultIsUsed() {
ApplicationContext context = this.spring.context(new GenericWebApplicationContext()).getContext(); ApplicationContext context = this.spring.context(new GenericWebApplicationContext()).getContext();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt(); new OAuth2ResourceServerConfigurer<HttpSecurity>(context)
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isInstanceOf(JwtAuthenticationConverter.class); .jwt((jwt) -> assertThat(jwt.getJwtAuthenticationConverter())
.isInstanceOf(JwtAuthenticationConverter.class));
} }
@Test @Test
@ -1353,8 +1357,8 @@ public class OAuth2ResourceServerConfigurerTests {
GenericWebApplicationContext context = new GenericWebApplicationContext(); GenericWebApplicationContext context = new GenericWebApplicationContext();
context.registerBean(JwtAuthenticationConverter.class, () -> converterBean); context.registerBean(JwtAuthenticationConverter.class, () -> converterBean);
this.spring.context(context).autowire(); this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt(); new OAuth2ResourceServerConfigurer<HttpSecurity>(context)
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isEqualTo(converterBean); .jwt((jwt) -> assertThat(jwt.getJwtAuthenticationConverter()).isEqualTo(converterBean));
} }
@Test @Test
@ -1364,9 +1368,10 @@ public class OAuth2ResourceServerConfigurerTests {
GenericWebApplicationContext context = new GenericWebApplicationContext(); GenericWebApplicationContext context = new GenericWebApplicationContext();
context.registerBean(JwtAuthenticationConverter.class, () -> converterBean); context.registerBean(JwtAuthenticationConverter.class, () -> converterBean);
this.spring.context(context).autowire(); this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt(); new OAuth2ResourceServerConfigurer<HttpSecurity>(context).jwt((jwt) -> {
jwtConfigurer.jwtAuthenticationConverter(converter); jwt.jwtAuthenticationConverter(converter);
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isEqualTo(converter); assertThat(jwt.getJwtAuthenticationConverter()).isEqualTo(converter);
});
} }
@Test @Test
@ -1377,9 +1382,10 @@ public class OAuth2ResourceServerConfigurerTests {
context.registerBean("converterOne", JwtAuthenticationConverter.class, () -> converterBean); context.registerBean("converterOne", JwtAuthenticationConverter.class, () -> converterBean);
context.registerBean("converterTwo", JwtAuthenticationConverter.class, () -> converterBean); context.registerBean("converterTwo", JwtAuthenticationConverter.class, () -> converterBean);
this.spring.context(context).autowire(); this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt(); new OAuth2ResourceServerConfigurer<HttpSecurity>(context).jwt((jwt) -> {
jwtConfigurer.jwtAuthenticationConverter(converter); jwt.jwtAuthenticationConverter(converter);
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isEqualTo(converter); assertThat(jwt.getJwtAuthenticationConverter()).isEqualTo(converter);
});
} }
@Test @Test
@ -1389,9 +1395,10 @@ public class OAuth2ResourceServerConfigurerTests {
context.registerBean("converterOne", JwtAuthenticationConverter.class, () -> converterBean); context.registerBean("converterOne", JwtAuthenticationConverter.class, () -> converterBean);
context.registerBean("converterTwo", JwtAuthenticationConverter.class, () -> converterBean); context.registerBean("converterTwo", JwtAuthenticationConverter.class, () -> converterBean);
this.spring.context(context).autowire(); this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt(); new OAuth2ResourceServerConfigurer<HttpSecurity>(context).jwt((jwt) -> {
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class) assertThatExceptionOfType(NoUniqueBeanDefinitionException.class)
.isThrownBy(jwtConfigurer::getJwtAuthenticationConverter); .isThrownBy(jwt::getJwtAuthenticationConverter);
});
} }
@Test @Test
@ -1550,12 +1557,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/requires-read-scope").access("hasAuthority('SCOPE_message:read')") .requestMatchers("/requires-read-scope").access("hasAuthority('SCOPE_message:read')")
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1600,14 +1606,12 @@ public class OAuth2ResourceServerConfigurerTests {
DefaultBearerTokenResolver defaultBearerTokenResolver = new DefaultBearerTokenResolver(); DefaultBearerTokenResolver defaultBearerTokenResolver = new DefaultBearerTokenResolver();
defaultBearerTokenResolver.setAllowUriQueryParameter(true); defaultBearerTokenResolver.setAllowUriQueryParameter(true);
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/requires-read-scope").access("hasAuthority('SCOPE_message:read')") .requestMatchers("/requires-read-scope").access("hasAuthority('SCOPE_message:read')")
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer()
.bearerTokenResolver(defaultBearerTokenResolver) .bearerTokenResolver(defaultBearerTokenResolver)
.jwt() .jwt((jwt) -> jwt.jwkSetUri(this.jwkSetUri)));
.jwkSetUri(this.jwkSetUri);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1656,14 +1660,12 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/requires-read-scope").access("hasAuthority('SCOPE_message:read')") .requestMatchers("/requires-read-scope").access("hasAuthority('SCOPE_message:read')")
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .csrf((csrf) -> csrf.disable())
.csrf().disable() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt((jwt) -> jwt.jwkSetUri(this.jwkSetUri)));
.jwt()
.jwkSetUri(this.jwkSetUri);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1678,12 +1680,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .anonymous((anonymous) -> anonymous.disable())
.anonymous().disable() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1699,11 +1700,10 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1718,10 +1718,9 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer(withDefaults());
.oauth2ResourceServer();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1736,12 +1735,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer()
.authenticationEntryPoint(authenticationEntryPoint()) .authenticationEntryPoint(authenticationEntryPoint())
.jwt(); .jwt(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1762,12 +1760,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().denyAll() .anyRequest().denyAll())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer()
.accessDeniedHandler(accessDeniedHandler()) .accessDeniedHandler(accessDeniedHandler())
.jwt(); .jwt(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1788,16 +1785,13 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().denyAll() .anyRequest().denyAll())
.and() .exceptionHandling((handling) -> handling
.exceptionHandling() .defaultAccessDeniedHandlerFor(new AccessDeniedHandlerImpl(), (request) -> false))
.defaultAccessDeniedHandlerFor(new AccessDeniedHandlerImpl(), (request) -> false) .httpBasic(withDefaults())
.and() .oauth2ResourceServer((server) -> server
.httpBasic() .jwt(Customizer.withDefaults()));
.and()
.oauth2ResourceServer()
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1826,12 +1820,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt((jwt) -> jwt
.jwt() .jwtAuthenticationConverter(getJwtAuthenticationConverter())));
.jwtAuthenticationConverter(getJwtAuthenticationConverter());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1851,12 +1844,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/requires-read-scope").access("hasAuthority('message:read')") .requestMatchers("/requires-read-scope").access("hasAuthority('message:read')"))
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt((jwt) -> jwt
.jwt() .jwtAuthenticationConverter(getJwtAuthenticationConverter())));
.jwtAuthenticationConverter(getJwtAuthenticationConverter());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1878,13 +1870,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .httpBasic(withDefaults())
.httpBasic() .oauth2ResourceServer((server) -> server
.and() .jwt(Customizer.withDefaults()));
.oauth2ResourceServer()
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1911,13 +1901,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .formLogin(withDefaults())
.formLogin() .oauth2ResourceServer((server) -> server
.and() .jwt(Customizer.withDefaults()));
.oauth2ResourceServer()
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1957,11 +1945,10 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); // missing key configuration, e.g. jwkSetUri return http.build(); // missing key configuration, e.g. jwkSetUri
// @formatter:on // @formatter:on
} }
@ -1976,11 +1963,10 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.sessionManagement() .sessionManagement((management) -> management
.sessionCreationPolicy(SessionCreationPolicy.ALWAYS) .sessionCreationPolicy(SessionCreationPolicy.ALWAYS))
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -1995,12 +1981,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer()
.bearerTokenResolver(allowRequestBody()) .bearerTokenResolver(allowRequestBody())
.jwt(); .jwt(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2021,11 +2006,10 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2047,11 +2031,10 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2115,12 +2098,10 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt((jwt) -> jwt.decoder(decoder())));
.jwt()
.decoder(decoder());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2170,11 +2151,10 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2194,12 +2174,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt((jwt) -> jwt
.jwt() .authenticationManager(authenticationProvider()::authenticate)));
.authenticationManager(authenticationProvider()::authenticate);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2260,8 +2239,8 @@ public class OAuth2ResourceServerConfigurerTests {
this.jwtDecoder.setJwtValidator(this.jwtValidator); this.jwtDecoder.setJwtValidator(this.jwtValidator);
// @formatter:off // @formatter:off
http http
.oauth2ResourceServer() .oauth2ResourceServer((server) -> server
.jwt(); .jwt(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2288,8 +2267,8 @@ public class OAuth2ResourceServerConfigurerTests {
this.jwtDecoder.setJwtValidator(jwtValidator); this.jwtDecoder.setJwtValidator(jwtValidator);
// @formatter:off // @formatter:off
http http
.oauth2ResourceServer() .oauth2ResourceServer((server) -> server
.jwt(); .jwt(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2312,8 +2291,8 @@ public class OAuth2ResourceServerConfigurerTests {
this.jwtDecoder.setJwtValidator(jwtValidator); this.jwtDecoder.setJwtValidator(jwtValidator);
// @formatter:off // @formatter:off
http http
.oauth2ResourceServer() .oauth2ResourceServer((server) -> server
.jwt(); .jwt(Customizer.withDefaults()));
return http.build(); return http.build();
} }
} }
@ -2333,11 +2312,10 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2359,11 +2337,10 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2389,12 +2366,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/requires-read-scope").hasAuthority("SCOPE_message:read") .requestMatchers("/requires-read-scope").hasAuthority("SCOPE_message:read")
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .opaqueToken(Customizer.withDefaults()));
.opaqueToken();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2433,12 +2409,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .opaqueToken((opaqueToken) -> opaqueToken
.opaqueToken() .authenticationManager(authenticationProvider()::authenticate)));
.authenticationManager(authenticationProvider()::authenticate);
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2523,10 +2498,9 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.oauth2ResourceServer() .oauth2ResourceServer((server) -> server
.jwt() .jwt(Customizer.withDefaults())
.and() .opaqueToken(Customizer.withDefaults()));
.opaqueToken();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2541,12 +2515,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .opaqueToken((opaqueToken) -> opaqueToken
.opaqueToken() .introspectionUri("https://idp.example.com")));
.introspectionUri("https://idp.example.com");
return http.build(); // missing credentials return http.build(); // missing credentials
// @formatter:on // @formatter:on
} }
@ -2561,11 +2534,10 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .jwt(Customizer.withDefaults()));
.jwt();
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2605,9 +2577,8 @@ public class OAuth2ResourceServerConfigurerTests {
issuerOne, issuerTwo); issuerOne, issuerTwo);
// @formatter:off // @formatter:off
http http
.oauth2ResourceServer() .oauth2ResourceServer((server) -> server
.authenticationManagerResolver(authenticationManagerResolver) .authenticationManagerResolver(authenticationManagerResolver))
.and()
.anonymous(AbstractHttpConfigurer::disable); .anonymous(AbstractHttpConfigurer::disable);
return http.build(); return http.build();
// @formatter:on // @formatter:on
@ -2623,12 +2594,11 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer()
.authenticationManagerResolver(mock(AuthenticationManagerResolver.class)) .authenticationManagerResolver(mock(AuthenticationManagerResolver.class))
.opaqueToken(); .opaqueToken(Customizer.withDefaults()));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }
@ -2644,13 +2614,12 @@ public class OAuth2ResourceServerConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers("/requires-read-scope").hasAuthority("SCOPE_message:read") .requestMatchers("/requires-read-scope").hasAuthority("SCOPE_message:read")
.anyRequest().authenticated() .anyRequest().authenticated())
.and() .oauth2ResourceServer((server) -> server
.oauth2ResourceServer() .opaqueToken((opaqueToken) -> opaqueToken
.opaqueToken() .authenticationConverter(authenticationConverter())));
.authenticationConverter(authenticationConverter());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -486,7 +486,7 @@ public class Saml2LoginConfigurerTests {
@Bean @Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.saml2Login().authenticationManager(getAuthenticationManagerMock("ROLE_AUTH_MANAGER")); http.saml2Login((login) -> login.authenticationManager(getAuthenticationManagerMock("ROLE_AUTH_MANAGER")));
return http.build(); return http.build();
} }

View File

@ -591,7 +591,7 @@ public class Saml2LogoutConfigurerTests {
.logout((logout) -> logout.logoutSuccessHandler(this.mockLogoutSuccessHandler)) .logout((logout) -> logout.logoutSuccessHandler(this.mockLogoutSuccessHandler))
.saml2Login(withDefaults()) .saml2Login(withDefaults())
.saml2Logout(withDefaults()) .saml2Logout(withDefaults())
.csrf().disable(); .csrf((csrf) -> csrf.disable());
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -165,8 +165,8 @@ public class GrantedAuthorityDefaultsJcTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.anyRequest().access("hasRole('USER')"); .anyRequest().access("hasRole('USER')"));
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -43,16 +43,16 @@ public class CustomConfigurer extends SecurityConfigurerAdapter<DefaultSecurityF
context.getAutowireCapableBeanFactory().autowireBean(this); context.getAutowireCapableBeanFactory().autowireBean(this);
// @formatter:off // @formatter:off
http http
.authorizeRequests() .authorizeRequests((requests) -> requests
.requestMatchers(new AntPathRequestMatcher(this.permitAllPattern)).permitAll() .requestMatchers(new AntPathRequestMatcher(this.permitAllPattern)).permitAll()
.anyRequest().authenticated(); .anyRequest().authenticated());
// @formatter:on // @formatter:on
if (http.getConfigurer(FormLoginConfigurer.class) == null) { if (http.getConfigurer(FormLoginConfigurer.class) == null) {
// only apply if formLogin() was not invoked by the user // only apply if formLogin() was not invoked by the user
// @formatter:off // @formatter:off
http http
.formLogin() .formLogin((login) -> login
.loginPage(this.loginPage); .loginPage(this.loginPage));
// @formatter:on // @formatter:on
} }
} }

View File

@ -32,6 +32,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockFilterChain; import org.springframework.mock.web.MockFilterChain;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
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.web.FilterChainProxy; import org.springframework.security.web.FilterChainProxy;
@ -144,11 +145,10 @@ public class CustomHttpSecurityConfigurerTests {
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
http http
.apply(CustomConfigurer.customConfigurer()) .with(CustomConfigurer.customConfigurer(), Customizer.withDefaults())
.and() .csrf((csrf) -> csrf.disable())
.csrf().disable() .formLogin((login) -> login
.formLogin() .loginPage("/other"));
.loginPage("/other");
return http.build(); return http.build();
// @formatter:on // @formatter:on
} }

View File

@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpHeaders import org.springframework.http.HttpHeaders
import org.springframework.security.config.Customizer.withDefaults
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.test.SpringTestContext import org.springframework.security.config.test.SpringTestContext
@ -128,7 +129,7 @@ class CorsDslTests {
open class CorsDisabledConfig { open class CorsDisabledConfig {
@Bean @Bean
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http.cors() http.cors(withDefaults())
http { http {
cors { cors {
disable() disable()

View File

@ -25,6 +25,7 @@ import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.security.config.Customizer.withDefaults
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
@ -127,7 +128,7 @@ class FormLoginDslTests {
open class DisabledConfig { open class DisabledConfig {
@Bean @Bean
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http.formLogin() http.formLogin(withDefaults())
http { http {
formLogin { formLogin {
disable() disable()