Skip to content

WebFlux oauth2Login with formLogin test #9326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ public DefaultLoginPage assertLoginFormNotPresent() {
return this;
}

public DefaultLoginPage assertLoginFormPresent() {
loginForm().username("");
return this;
}

public LoginForm loginForm() {
if (this.loginForm == null) {
this.loginForm = PageFactory.initElements(this.driver, LoginForm.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.authentication.UserDetailsRepositoryReactiveAuthenticationManager;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
import org.springframework.security.config.test.SpringTestRule;
import org.springframework.security.config.users.ReactiveAuthenticationTestConfiguration;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.htmlunit.server.WebTestClientHtmlUnitDriverBuilder;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
Expand Down Expand Up @@ -176,6 +179,24 @@ public void defaultLoginPageWithSingleClientRegistrationThenRedirect() {
assertThat(driver.getCurrentUrl()).startsWith("https://github.com/login/oauth/authorize");
}

@Test
public void defaultLoginPageWithSingleClientRegistrationAndFormLoginThenLinks() {
this.spring.register(OAuth2LoginWithSingleClientRegistrations.class, OAuth2LoginWithFormLogin.class).autowire();
// @formatter:off
WebTestClient webTestClient = WebTestClientBuilder
.bindToWebFilters(new GitHubWebFilter(), this.springSecurity)
.build();
WebDriver driver = WebTestClientHtmlUnitDriverBuilder
.webTestClientSetup(webTestClient)
.build();
FormLoginTests.HomePage.to(driver, FormLoginTests.DefaultLoginPage.class)
.assertAt()
.assertLoginFormPresent()
.oauth2Login()
.assertClientRegistrationByName(OAuth2LoginTests.github.getClientName());
// @formatter:on
}

// gh-8118
@Test
public void defaultLoginPageWithSingleClientRegistrationAndXhrRequestThenDoesNotRedirectForAuthorization() {
Expand Down Expand Up @@ -584,6 +605,30 @@ String home(@RegisteredOAuth2AuthorizedClient("github") OAuth2AuthorizedClient a

}

@Configuration
static class OAuth2LoginWithFormLogin {

@Bean
SecurityWebFilterChain springSecurityFilter(ServerHttpSecurity http) {
ReactiveUserDetailsService reactiveUserDetailsService = ReactiveAuthenticationTestConfiguration
.userDetailsService();
ReactiveAuthenticationManager authenticationManager = new UserDetailsRepositoryReactiveAuthenticationManager(
reactiveUserDetailsService);
http.authenticationManager(authenticationManager);
// @formatter:off
http
.authorizeExchange()
.anyExchange().authenticated()
.and()
.oauth2Login()
.and()
.formLogin();
// @formatter:on
return http.build();
}

}

@Configuration
static class OAuth2LoginMockAuthenticationManagerConfig {

Expand Down