|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
23 | 23 | import org.openqa.selenium.WebElement;
|
24 | 24 | import org.openqa.selenium.support.FindBy;
|
25 | 25 | import org.openqa.selenium.support.PageFactory;
|
| 26 | +import org.springframework.security.authentication.ReactiveAuthenticationManager; |
| 27 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
26 | 28 | import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
|
27 | 29 | import org.springframework.security.htmlunit.server.WebTestClientHtmlUnitDriverBuilder;
|
28 | 30 | import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
|
|
40 | 42 | import static org.assertj.core.api.Assertions.assertThat;
|
41 | 43 | import static org.assertj.core.api.Assertions.assertThatCode;
|
42 | 44 | 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; |
43 | 49 |
|
44 | 50 | /**
|
45 | 51 | * @author Rob Winch
|
@@ -152,6 +158,42 @@ public void authenticationSuccess() {
|
152 | 158 | assertThat(driver.getCurrentUrl()).endsWith("/custom");
|
153 | 159 | }
|
154 | 160 |
|
| 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 | + |
155 | 197 | public static class CustomLoginPage {
|
156 | 198 |
|
157 | 199 | private WebDriver driver;
|
|
0 commit comments