Skip to content

Commit ac3f9b8

Browse files
committed
oauth2Login does not auto-redirect for XHR request
Fixes gh-6812
1 parent f03d726 commit ac3f9b8

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2LoginConfigurer.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -64,6 +64,7 @@
6464
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
6565
import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
6666
import org.springframework.security.web.util.matcher.OrRequestMatcher;
67+
import org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher;
6768
import org.springframework.security.web.util.matcher.RequestMatcher;
6869
import org.springframework.util.Assert;
6970
import org.springframework.util.ClassUtils;
@@ -604,8 +605,11 @@ private AuthenticationEntryPoint getLoginEntryPoint(B http, String providerLogin
604605
RequestMatcher defaultLoginPageMatcher = new AndRequestMatcher(
605606
new OrRequestMatcher(loginPageMatcher, faviconMatcher), defaultEntryPointMatcher);
606607

608+
RequestMatcher notXRequestedWith = new NegatedRequestMatcher(
609+
new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest"));
610+
607611
LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints = new LinkedHashMap<>();
608-
entryPoints.put(new NegatedRequestMatcher(defaultLoginPageMatcher),
612+
entryPoints.put(new AndRequestMatcher(notXRequestedWith, new NegatedRequestMatcher(defaultLoginPageMatcher)),
609613
new LoginUrlAuthenticationEntryPoint(providerLoginPage));
610614

611615
DelegatingAuthenticationEntryPoint loginEntryPoint = new DelegatingAuthenticationEntryPoint(entryPoints);

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

+15
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,21 @@ public void oauth2LoginWithMultipleClientsConfiguredThenRedirectDefaultLoginPage
353353
assertThat(this.response.getRedirectedUrl()).matches("http://localhost/login");
354354
}
355355

356+
// gh-6812
357+
@Test
358+
public void oauth2LoginWithOneClientConfiguredAndRequestXHRNotAuthenticatedThenDoesNotRedirectForAuthorization() throws Exception {
359+
loadConfig(OAuth2LoginConfig.class);
360+
361+
String requestUri = "/";
362+
this.request = new MockHttpServletRequest("GET", requestUri);
363+
this.request.setServletPath(requestUri);
364+
this.request.addHeader("X-Requested-With", "XMLHttpRequest");
365+
366+
this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain);
367+
368+
assertThat(this.response.getRedirectedUrl()).doesNotMatch("http://localhost/oauth2/authorization/google");
369+
}
370+
356371
@Test
357372
public void oauth2LoginWithCustomLoginPageThenRedirectCustomLoginPage() throws Exception {
358373
loadConfig(OAuth2LoginConfigCustomLoginPage.class);

0 commit comments

Comments
 (0)