Skip to content

Commit 9dd3dfe

Browse files
committed
Fix requiresAuthenticationMatcher not being used
The custom server requiresAuthenticationMatcher was not always picked up Fixes: gh-7863
1 parent edb6cd3 commit 9dd3dfe

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3049,7 +3049,9 @@ public FormLoginSpec authenticationSuccessHandler(
30493049
public FormLoginSpec loginPage(String loginPage) {
30503050
this.defaultEntryPoint = new RedirectServerAuthenticationEntryPoint(loginPage);
30513051
this.authenticationEntryPoint = this.defaultEntryPoint;
3052-
this.requiresAuthenticationMatcher = ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, loginPage);
3052+
if (this.requiresAuthenticationMatcher == null) {
3053+
this.requiresAuthenticationMatcher = ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, loginPage);
3054+
}
30533055
if (this.authenticationFailureHandler == null) {
30543056
this.authenticationFailureHandler = new RedirectServerAuthenticationFailureHandler(loginPage + "?error");
30553057
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler;
3838
import org.springframework.security.web.server.context.ServerSecurityContextRepository;
3939
import org.springframework.security.web.server.csrf.CsrfToken;
40+
import org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher;
4041
import org.springframework.stereotype.Controller;
4142
import org.springframework.test.web.reactive.server.WebTestClient;
4243
import org.springframework.web.bind.annotation.GetMapping;
@@ -245,6 +246,31 @@ public void formLoginWhenCustomAuthenticationFailureHandlerThenUsed() {
245246
assertThat(driver.getCurrentUrl()).endsWith("/failure");
246247
}
247248

249+
@Test
250+
public void formLoginWhenCustomRequiresAuthenticationMatcherThenUsed() {
251+
SecurityWebFilterChain securityWebFilter = this.http
252+
.authorizeExchange()
253+
.pathMatchers("/login", "/sign-in").permitAll()
254+
.anyExchange().authenticated()
255+
.and()
256+
.formLogin()
257+
.requiresAuthenticationMatcher(new PathPatternParserServerWebExchangeMatcher("/sign-in"))
258+
.and()
259+
.build();
260+
261+
WebTestClient webTestClient = WebTestClientBuilder
262+
.bindToWebFilters(securityWebFilter)
263+
.build();
264+
265+
WebDriver driver = WebTestClientHtmlUnitDriverBuilder
266+
.webTestClientSetup(webTestClient)
267+
.build();
268+
269+
driver.get("http://localhost/sign-in");
270+
271+
assertThat(driver.getCurrentUrl()).endsWith("/login?error");
272+
}
273+
248274
@Test
249275
public void authenticationSuccess() {
250276
SecurityWebFilterChain securityWebFilter = this.http

0 commit comments

Comments
 (0)