Skip to content

Commit 9d543ce

Browse files
committed
Allow custom ReactiveAuthenticationManager for basic and form auth
Prior to this change, "HttpBasicSpec#authenticationManager" and "FormLoginSpec#authenticationManager" were always overridden by "ServerHttpSecurity#authenticationManager". This commit makes sure override only happens when custom authentication manager was not specified. Fixes: gh-5660
1 parent aac8544 commit 9d543ce

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1277,11 +1277,15 @@ public SecurityWebFilterChain build() {
12771277
this.cors.configure(this);
12781278
}
12791279
if (this.httpBasic != null) {
1280-
this.httpBasic.authenticationManager(this.authenticationManager);
1280+
if (this.httpBasic.authenticationManager == null) {
1281+
this.httpBasic.authenticationManager(this.authenticationManager);
1282+
}
12811283
this.httpBasic.configure(this);
12821284
}
12831285
if (this.formLogin != null) {
1284-
this.formLogin.authenticationManager(this.authenticationManager);
1286+
if (this.formLogin.authenticationManager == null) {
1287+
this.formLogin.authenticationManager(this.authenticationManager);
1288+
}
12851289
if (this.securityContextRepository != null) {
12861290
this.formLogin.securityContextRepository(this.securityContextRepository);
12871291
}

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

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@
2323
import org.openqa.selenium.WebElement;
2424
import org.openqa.selenium.support.FindBy;
2525
import org.openqa.selenium.support.PageFactory;
26+
import org.springframework.security.authentication.ReactiveAuthenticationManager;
27+
import org.springframework.security.authentication.TestingAuthenticationToken;
2628
import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
2729
import org.springframework.security.htmlunit.server.WebTestClientHtmlUnitDriverBuilder;
2830
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
@@ -40,6 +42,10 @@
4042
import static org.assertj.core.api.Assertions.assertThat;
4143
import static org.assertj.core.api.Assertions.assertThatCode;
4244
import static org.assertj.core.api.Assertions.assertThatThrownBy;
45+
import static org.mockito.ArgumentMatchers.any;
46+
import static org.mockito.BDDMockito.given;
47+
import static org.mockito.Mockito.mock;
48+
import static org.mockito.Mockito.verifyZeroInteractions;
4349

4450
/**
4551
* @author Rob Winch
@@ -152,6 +158,42 @@ public void authenticationSuccess() {
152158
assertThat(driver.getCurrentUrl()).endsWith("/custom");
153159
}
154160

161+
@Test
162+
public void customAuthenticationManager() {
163+
ReactiveAuthenticationManager defaultAuthenticationManager = mock(ReactiveAuthenticationManager.class);
164+
ReactiveAuthenticationManager customAuthenticationManager = mock(ReactiveAuthenticationManager.class);
165+
166+
given(defaultAuthenticationManager.authenticate(any())).willThrow(new RuntimeException("should not interact with default auth manager"));
167+
given(customAuthenticationManager.authenticate(any())).willReturn(Mono.just(new TestingAuthenticationToken("user", "password", "ROLE_USER", "ROLE_ADMIN")));
168+
169+
SecurityWebFilterChain securityWebFilter = this.http
170+
.authenticationManager(defaultAuthenticationManager)
171+
.formLogin()
172+
.authenticationManager(customAuthenticationManager)
173+
.and()
174+
.build();
175+
176+
WebTestClient webTestClient = WebTestClientBuilder
177+
.bindToWebFilters(securityWebFilter)
178+
.build();
179+
180+
WebDriver driver = WebTestClientHtmlUnitDriverBuilder
181+
.webTestClientSetup(webTestClient)
182+
.build();
183+
184+
DefaultLoginPage loginPage = DefaultLoginPage.to(driver)
185+
.assertAt();
186+
187+
HomePage homePage = loginPage.loginForm()
188+
.username("user")
189+
.password("password")
190+
.submit(HomePage.class);
191+
192+
homePage.assertAt();
193+
194+
verifyZeroInteractions(defaultAuthenticationManager);
195+
}
196+
155197
public static class CustomLoginPage {
156198

157199
private WebDriver driver;

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

+21
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.mockito.BDDMockito.given;
2121
import static org.mockito.Matchers.any;
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.verifyZeroInteractions;
2224
import static org.mockito.Mockito.when;
2325

2426
import java.util.Arrays;
@@ -190,6 +192,25 @@ public void csrfServerLogoutHandlerAppliedIfCsrfIsEnabled() {
190192
.isEqualTo(Arrays.asList(SecurityContextServerLogoutHandler.class, CsrfServerLogoutHandler.class));
191193
}
192194

195+
@Test
196+
public void basicWithCustomAuthenticationManager() {
197+
ReactiveAuthenticationManager customAuthenticationManager = mock(ReactiveAuthenticationManager.class);
198+
given(customAuthenticationManager.authenticate(any())).willReturn(Mono.just(new TestingAuthenticationToken("rob", "rob", "ROLE_USER", "ROLE_ADMIN")));
199+
200+
SecurityWebFilterChain securityFilterChain = this.http.httpBasic().authenticationManager(customAuthenticationManager).and().build();
201+
WebFilterChainProxy springSecurityFilterChain = new WebFilterChainProxy(securityFilterChain);
202+
WebTestClient client = WebTestClientBuilder.bindToWebFilters(springSecurityFilterChain).build();
203+
204+
client.get()
205+
.uri("/")
206+
.headers(headers -> headers.setBasicAuth("rob", "rob"))
207+
.exchange()
208+
.expectStatus().isOk()
209+
.expectBody(String.class).consumeWith(b -> assertThat(b.getResponseBody()).isEqualTo("ok"));
210+
211+
verifyZeroInteractions(this.authenticationManager);
212+
}
213+
193214
private <T extends WebFilter> Optional<T> getWebFilter(SecurityWebFilterChain filterChain, Class<T> filterClass) {
194215
return (Optional<T>) filterChain.getWebFilters()
195216
.filter(Objects::nonNull)

0 commit comments

Comments
 (0)