Skip to content

Commit 53e94bc

Browse files
committed
Add oauth2Login() tests
Issue gh-9548 gh-9660 gh-9266
1 parent 5afeaa3 commit 53e94bc

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2LoginConfigurerTests.java

+66
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.context.ConfigurableApplicationContext;
3737
import org.springframework.context.annotation.Bean;
3838
import org.springframework.context.annotation.Configuration;
39+
import org.springframework.http.HttpStatus;
3940
import org.springframework.http.MediaType;
4041
import org.springframework.mock.web.MockFilterChain;
4142
import org.springframework.mock.web.MockHttpServletRequest;
@@ -85,9 +86,11 @@
8586
import org.springframework.security.oauth2.jwt.JwtDecoderFactory;
8687
import org.springframework.security.oauth2.jwt.TestJwts;
8788
import org.springframework.security.web.FilterChainProxy;
89+
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
8890
import org.springframework.security.web.context.HttpRequestResponseHolder;
8991
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
9092
import org.springframework.security.web.context.SecurityContextRepository;
93+
import org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher;
9194
import org.springframework.test.web.servlet.MockMvc;
9295
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
9396

@@ -401,6 +404,30 @@ public void oauth2LoginWithOneClientConfiguredAndRequestXHRNotAuthenticatedThenD
401404
assertThat(this.response.getRedirectedUrl()).doesNotMatch("http://localhost/oauth2/authorization/google");
402405
}
403406

407+
@Test
408+
public void oauth2LoginWithHttpBasicOneClientConfiguredAndRequestXHRNotAuthenticatedThenUnauthorized()
409+
throws Exception {
410+
loadConfig(OAuth2LoginWithHttpBasicConfig.class);
411+
String requestUri = "/";
412+
this.request = new MockHttpServletRequest("GET", requestUri);
413+
this.request.setServletPath(requestUri);
414+
this.request.addHeader("X-Requested-With", "XMLHttpRequest");
415+
this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain);
416+
assertThat(this.response.getStatus()).isEqualTo(401);
417+
}
418+
419+
@Test
420+
public void oauth2LoginWithXHREntryPointOneClientConfiguredAndRequestXHRNotAuthenticatedThenUnauthorized()
421+
throws Exception {
422+
loadConfig(OAuth2LoginWithXHREntryPointConfig.class);
423+
String requestUri = "/";
424+
this.request = new MockHttpServletRequest("GET", requestUri);
425+
this.request.setServletPath(requestUri);
426+
this.request.addHeader("X-Requested-With", "XMLHttpRequest");
427+
this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain);
428+
assertThat(this.response.getStatus()).isEqualTo(401);
429+
}
430+
404431
// gh-9457
405432
@Test
406433
public void oauth2LoginWithOneAuthorizationCodeClientAndOtherClientsConfiguredThenRedirectForAuthorization()
@@ -896,6 +923,45 @@ ClientRegistrationRepository clientRegistrationRepository() {
896923

897924
}
898925

926+
@EnableWebSecurity
927+
static class OAuth2LoginWithHttpBasicConfig extends CommonWebSecurityConfigurerAdapter {
928+
929+
@Override
930+
protected void configure(HttpSecurity http) throws Exception {
931+
// @formatter:off
932+
http
933+
.oauth2Login()
934+
.clientRegistrationRepository(
935+
new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION))
936+
.and()
937+
.httpBasic();
938+
// @formatter:on
939+
super.configure(http);
940+
}
941+
942+
}
943+
944+
@EnableWebSecurity
945+
static class OAuth2LoginWithXHREntryPointConfig extends CommonWebSecurityConfigurerAdapter {
946+
947+
@Override
948+
protected void configure(HttpSecurity http) throws Exception {
949+
// @formatter:off
950+
http
951+
.oauth2Login()
952+
.clientRegistrationRepository(
953+
new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION))
954+
.and()
955+
.exceptionHandling()
956+
.defaultAuthenticationEntryPointFor(
957+
new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED),
958+
new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest"));
959+
// @formatter:on
960+
super.configure(http);
961+
}
962+
963+
}
964+
899965
private abstract static class CommonWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
900966

901967
@Override

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

+37
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,19 @@ public void defaultLoginPageWithSingleClientRegistrationAndXhrRequestThenDoesNot
232232
// @formatter:on
233233
}
234234

235+
@Test
236+
public void defaultLoginPageWithOAuth2LoginHttpBasicAndXhrRequestThenUnauthorized() {
237+
this.spring.register(OAuth2LoginWithSingleClientRegistrations.class, OAuth2LoginWithHttpBasic.class,
238+
WebFluxConfig.class).autowire();
239+
// @formatter:off
240+
this.client.get()
241+
.uri("/")
242+
.header("X-Requested-With", "XMLHttpRequest")
243+
.exchange()
244+
.expectStatus().isUnauthorized();
245+
// @formatter:on
246+
}
247+
235248
@Test
236249
public void oauth2AuthorizeWhenCustomObjectsThenUsed() {
237250
this.spring.register(OAuth2LoginWithSingleClientRegistrations.class, OAuth2AuthorizeWithMockObjectsConfig.class,
@@ -660,6 +673,30 @@ SecurityWebFilterChain springSecurityFilter(ServerHttpSecurity http) {
660673

661674
}
662675

676+
@Configuration
677+
static class OAuth2LoginWithHttpBasic {
678+
679+
@Bean
680+
SecurityWebFilterChain springSecurityFilter(ServerHttpSecurity http) {
681+
ReactiveUserDetailsService reactiveUserDetailsService = ReactiveAuthenticationTestConfiguration
682+
.userDetailsService();
683+
ReactiveAuthenticationManager authenticationManager = new UserDetailsRepositoryReactiveAuthenticationManager(
684+
reactiveUserDetailsService);
685+
http.authenticationManager(authenticationManager);
686+
// @formatter:off
687+
http
688+
.authorizeExchange()
689+
.anyExchange().authenticated()
690+
.and()
691+
.oauth2Login()
692+
.and()
693+
.httpBasic();
694+
// @formatter:on
695+
return http.build();
696+
}
697+
698+
}
699+
663700
@Configuration
664701
static class OAuth2LoginMockAuthenticationManagerConfig {
665702

0 commit comments

Comments
 (0)