Skip to content

Commit 9fcfacf

Browse files
committed
Use ServerHttpSecurity Lambda DSL in Tests
Issue gh-13067
1 parent 1a7b1fc commit 9fcfacf

14 files changed

+325
-381
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/reactive/EnableWebFluxSecurityTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,7 @@ static class MultiSecurityHttpConfig {
377377
@Bean
378378
SecurityWebFilterChain apiHttpSecurity(ServerHttpSecurity http) {
379379
http.securityMatcher(new PathPatternParserServerWebExchangeMatcher("/api/**"))
380-
.authorizeExchange()
381-
.anyExchange()
382-
.denyAll();
380+
.authorizeExchange((exchange) -> exchange.anyExchange().denyAll());
383381
return http.build();
384382
}
385383

config/src/test/java/org/springframework/security/config/web/server/AuthorizeExchangeSpecTests.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ public class AuthorizeExchangeSpecTests {
3535

3636
@Test
3737
public void antMatchersWhenMethodAndPatternsThenDiscriminatesByMethod() {
38-
this.http.csrf()
39-
.disable()
40-
.authorizeExchange()
41-
.pathMatchers(HttpMethod.POST, "/a", "/b")
42-
.denyAll()
43-
.anyExchange()
44-
.permitAll();
38+
this.http.csrf((csrf) -> csrf.disable())
39+
.authorizeExchange((authorize) -> authorize.pathMatchers(HttpMethod.POST, "/a", "/b")
40+
.denyAll()
41+
.anyExchange()
42+
.permitAll());
4543
WebTestClient client = buildClient();
4644
// @formatter:off
4745
client.get()
@@ -65,7 +63,8 @@ public void antMatchersWhenMethodAndPatternsThenDiscriminatesByMethod() {
6563

6664
@Test
6765
public void antMatchersWhenPatternsThenAnyMethod() {
68-
this.http.csrf().disable().authorizeExchange().pathMatchers("/a", "/b").denyAll().anyExchange().permitAll();
66+
this.http.csrf((csrf) -> csrf.disable())
67+
.authorizeExchange((authorize) -> authorize.pathMatchers("/a", "/b").denyAll().anyExchange().permitAll());
6968
WebTestClient client = buildClient();
7069
// @formatter:off
7170
client.get()
@@ -114,25 +113,25 @@ public void antMatchersWhenPatternsInLambdaThenAnyMethod() {
114113

115114
@Test
116115
public void antMatchersWhenNoAccessAndAnotherMatcherThenThrowsException() {
117-
this.http.authorizeExchange().pathMatchers("/incomplete");
116+
this.http.authorizeExchange((exchange) -> exchange.pathMatchers("/incomplete"));
118117
assertThatIllegalStateException()
119-
.isThrownBy(() -> this.http.authorizeExchange().pathMatchers("/throws-exception"));
118+
.isThrownBy(() -> this.http.authorizeExchange((exchange) -> exchange.pathMatchers("/throws-exception")));
120119
}
121120

122121
@Test
123122
public void anyExchangeWhenFollowedByMatcherThenThrowsException() {
124123
assertThatIllegalStateException().isThrownBy(() ->
125124
// @formatter:off
126-
this.http.authorizeExchange()
127-
.anyExchange().denyAll()
128-
.pathMatchers("/never-reached")
125+
this.http.authorizeExchange((exchange) -> exchange
126+
.anyExchange().denyAll()
127+
.pathMatchers("/never-reached"))
129128
// @formatter:on
130129
);
131130
}
132131

133132
@Test
134133
public void buildWhenMatcherDefinedWithNoAccessThenThrowsException() {
135-
this.http.authorizeExchange().pathMatchers("/incomplete");
134+
this.http.authorizeExchange((exchange) -> exchange.pathMatchers("/incomplete"));
136135
assertThatIllegalStateException().isThrownBy(this.http::build);
137136
}
138137

config/src/test/java/org/springframework/security/config/web/server/CorsSpecTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void givenGetCorsConfigurationWillReturnWildcard() {
7373
@Test
7474
public void corsWhenEnabledThenAccessControlAllowOriginAndSecurityHeaders() {
7575
givenGetCorsConfigurationWillReturnWildcard();
76-
this.http.cors().configurationSource(this.source);
76+
this.http.cors((cors) -> cors.configurationSource(this.source));
7777
this.expectedHeaders.set("Access-Control-Allow-Origin", "*");
7878
this.expectedHeaders.set("X-Frame-Options", "DENY");
7979
assertHeaders();

config/src/test/java/org/springframework/security/config/web/server/ExceptionHandlingSpecTests.java

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.http.HttpStatus;
22+
import org.springframework.security.config.Customizer;
2223
import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
2324
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
2425
import org.springframework.security.web.server.SecurityWebFilterChain;
@@ -42,12 +43,11 @@ public class ExceptionHandlingSpecTests {
4243
public void defaultAuthenticationEntryPoint() {
4344
// @formatter:off
4445
SecurityWebFilterChain securityWebFilter = this.http
45-
.csrf().disable()
46-
.authorizeExchange()
47-
.anyExchange().authenticated()
48-
.and()
49-
.exceptionHandling().and()
50-
.build();
46+
.csrf((csrf) -> csrf.disable())
47+
.authorizeExchange((authorize) -> authorize
48+
.anyExchange().authenticated())
49+
.exceptionHandling(withDefaults())
50+
.build();
5151
WebTestClient client = WebTestClientBuilder
5252
.bindToWebFilters(securityWebFilter)
5353
.build();
@@ -83,14 +83,12 @@ public void requestWhenExceptionHandlingWithDefaultsInLambdaThenDefaultAuthentic
8383
public void customAuthenticationEntryPoint() {
8484
// @formatter:off
8585
SecurityWebFilterChain securityWebFilter = this.http
86-
.csrf().disable()
87-
.authorizeExchange()
88-
.anyExchange().authenticated()
89-
.and()
90-
.exceptionHandling()
91-
.authenticationEntryPoint(redirectServerAuthenticationEntryPoint("/auth"))
92-
.and()
93-
.build();
86+
.csrf((csrf) -> csrf.disable())
87+
.authorizeExchange((authorize) -> authorize
88+
.anyExchange().authenticated())
89+
.exceptionHandling((handling) -> handling
90+
.authenticationEntryPoint(redirectServerAuthenticationEntryPoint("/auth")))
91+
.build();
9492
WebTestClient client = WebTestClientBuilder
9593
.bindToWebFilters(securityWebFilter)
9694
.build();
@@ -128,13 +126,12 @@ public void requestWhenCustomAuthenticationEntryPointInLambdaThenCustomAuthentic
128126
public void defaultAccessDeniedHandler() {
129127
// @formatter:off
130128
SecurityWebFilterChain securityWebFilter = this.http
131-
.csrf().disable()
132-
.httpBasic().and()
133-
.authorizeExchange()
134-
.anyExchange().hasRole("ADMIN")
135-
.and()
136-
.exceptionHandling().and()
137-
.build();
129+
.csrf((csrf) -> csrf.disable())
130+
.httpBasic(Customizer.withDefaults())
131+
.authorizeExchange((exchange) -> exchange
132+
.anyExchange().hasRole("ADMIN"))
133+
.exceptionHandling(withDefaults())
134+
.build();
138135
WebTestClient client = WebTestClientBuilder
139136
.bindToWebFilters(securityWebFilter)
140137
.build();
@@ -171,15 +168,13 @@ public void requestWhenExceptionHandlingWithDefaultsInLambdaThenDefaultAccessDen
171168
public void customAccessDeniedHandler() {
172169
// @formatter:off
173170
SecurityWebFilterChain securityWebFilter = this.http
174-
.csrf().disable()
175-
.httpBasic().and()
176-
.authorizeExchange()
177-
.anyExchange().hasRole("ADMIN")
178-
.and()
179-
.exceptionHandling()
180-
.accessDeniedHandler(httpStatusServerAccessDeniedHandler(HttpStatus.BAD_REQUEST))
181-
.and()
182-
.build();
171+
.csrf((csrf) -> csrf.disable())
172+
.httpBasic(Customizer.withDefaults())
173+
.authorizeExchange((exchange) -> exchange
174+
.anyExchange().hasRole("ADMIN"))
175+
.exceptionHandling((handling) -> handling
176+
.accessDeniedHandler(httpStatusServerAccessDeniedHandler(HttpStatus.BAD_REQUEST)))
177+
.build();
183178
WebTestClient client = WebTestClientBuilder
184179
.bindToWebFilters(securityWebFilter)
185180
.build();

config/src/test/java/org/springframework/security/config/web/server/FormLoginTests.java

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,10 @@ public class FormLoginTests {
6969
public void defaultLoginPage() {
7070
// @formatter:off
7171
SecurityWebFilterChain securityWebFilter = this.http
72-
.authorizeExchange()
73-
.anyExchange().authenticated()
74-
.and()
75-
.formLogin()
76-
.and()
77-
.build();
72+
.authorizeExchange((exchange) -> exchange
73+
.anyExchange().authenticated())
74+
.formLogin(withDefaults())
75+
.build();
7876
WebTestClient webTestClient = WebTestClientBuilder
7977
.bindToWebFilters(securityWebFilter)
8078
.build();
@@ -129,14 +127,12 @@ public void formLoginWhenDefaultsInLambdaThenCreatesDefaultLoginPage() {
129127
public void customLoginPage() {
130128
// @formatter:off
131129
SecurityWebFilterChain securityWebFilter = this.http
132-
.authorizeExchange()
133-
.pathMatchers("/login").permitAll()
134-
.anyExchange().authenticated()
135-
.and()
136-
.formLogin()
137-
.loginPage("/login")
138-
.and()
139-
.build();
130+
.authorizeExchange((exchange) -> exchange
131+
.pathMatchers("/login").permitAll()
132+
.anyExchange().authenticated())
133+
.formLogin((login) -> login
134+
.loginPage("/login"))
135+
.build();
140136
WebTestClient webTestClient = WebTestClient
141137
.bindToController(new CustomLoginPageController(), new WebTestClientBuilder.Http200RestController())
142138
.webFilter(new WebFilterChainProxy(securityWebFilter))
@@ -189,14 +185,12 @@ public void formLoginWhenCustomLoginPageInLambdaThenUsed() {
189185
public void formLoginWhenCustomAuthenticationFailureHandlerThenUsed() {
190186
// @formatter:off
191187
SecurityWebFilterChain securityWebFilter = this.http
192-
.authorizeExchange()
193-
.pathMatchers("/login", "/failure").permitAll()
194-
.anyExchange().authenticated()
195-
.and()
196-
.formLogin()
197-
.authenticationFailureHandler(new RedirectServerAuthenticationFailureHandler("/failure"))
198-
.and()
199-
.build();
188+
.authorizeExchange((exchange) -> exchange
189+
.pathMatchers("/login", "/failure").permitAll()
190+
.anyExchange().authenticated())
191+
.formLogin((login) -> login
192+
.authenticationFailureHandler(new RedirectServerAuthenticationFailureHandler("/failure")))
193+
.build();
200194
WebTestClient webTestClient = WebTestClientBuilder
201195
.bindToWebFilters(securityWebFilter)
202196
.build();
@@ -218,14 +212,12 @@ public void formLoginWhenCustomAuthenticationFailureHandlerThenUsed() {
218212
public void formLoginWhenCustomRequiresAuthenticationMatcherThenUsed() {
219213
// @formatter:off
220214
SecurityWebFilterChain securityWebFilter = this.http
221-
.authorizeExchange()
222-
.pathMatchers("/login", "/sign-in").permitAll()
223-
.anyExchange().authenticated()
224-
.and()
225-
.formLogin()
226-
.requiresAuthenticationMatcher(new PathPatternParserServerWebExchangeMatcher("/sign-in"))
227-
.and()
228-
.build();
215+
.authorizeExchange((exchange) -> exchange
216+
.pathMatchers("/login", "/sign-in").permitAll()
217+
.anyExchange().authenticated())
218+
.formLogin((login) -> login
219+
.requiresAuthenticationMatcher(new PathPatternParserServerWebExchangeMatcher("/sign-in")))
220+
.build();
229221
WebTestClient webTestClient = WebTestClientBuilder
230222
.bindToWebFilters(securityWebFilter)
231223
.build();
@@ -241,13 +233,11 @@ public void formLoginWhenCustomRequiresAuthenticationMatcherThenUsed() {
241233
public void authenticationSuccess() {
242234
// @formatter:off
243235
SecurityWebFilterChain securityWebFilter = this.http
244-
.authorizeExchange()
245-
.anyExchange().authenticated()
246-
.and()
247-
.formLogin()
248-
.authenticationSuccessHandler(new RedirectServerAuthenticationSuccessHandler("/custom"))
249-
.and()
250-
.build();
236+
.authorizeExchange((exchange) -> exchange
237+
.anyExchange().authenticated())
238+
.formLogin((login) -> login
239+
.authenticationSuccessHandler(new RedirectServerAuthenticationSuccessHandler("/custom")))
240+
.build();
251241
WebTestClient webTestClient = WebTestClientBuilder
252242
.bindToWebFilters(securityWebFilter)
253243
.build();
@@ -275,11 +265,10 @@ public void customAuthenticationManager() {
275265
.willReturn(Mono.just(new TestingAuthenticationToken("user", "password", "ROLE_USER", "ROLE_ADMIN")));
276266
// @formatter:off
277267
SecurityWebFilterChain securityWebFilter = this.http
278-
.authenticationManager(defaultAuthenticationManager)
279-
.formLogin()
280-
.authenticationManager(customAuthenticationManager)
281-
.and()
282-
.build();
268+
.authenticationManager(defaultAuthenticationManager)
269+
.formLogin((login) -> login
270+
.authenticationManager(customAuthenticationManager))
271+
.build();
283272
WebTestClient webTestClient = WebTestClientBuilder
284273
.bindToWebFilters(securityWebFilter)
285274
.build();
@@ -309,14 +298,12 @@ public void formLoginSecurityContextRepository() {
309298
given(formLoginSecContextRepository.load(any())).willReturn(authentication(token));
310299
// @formatter:off
311300
SecurityWebFilterChain securityWebFilter = this.http
312-
.authorizeExchange()
313-
.anyExchange().authenticated()
314-
.and()
315-
.securityContextRepository(defaultSecContextRepository)
316-
.formLogin()
317-
.securityContextRepository(formLoginSecContextRepository)
318-
.and()
319-
.build();
301+
.authorizeExchange((exchange) -> exchange
302+
.anyExchange().authenticated())
303+
.securityContextRepository(defaultSecContextRepository)
304+
.formLogin((login) -> login
305+
.securityContextRepository(formLoginSecContextRepository))
306+
.build();
320307
WebTestClient webTestClient = WebTestClientBuilder
321308
.bindToWebFilters(securityWebFilter)
322309
.build();

0 commit comments

Comments
 (0)