mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-28 23:02:15 +00:00
Disable XMLHttpRequest for formLogin entry point
Previously the following: http http://localhost:8080/user \ "X-Requested-With:XMLHttpRequest" "Accept:text/plain" Produced a 302 instead of a 401 Fixes gh-3887
This commit is contained in:
parent
2a73f3cdf7
commit
66858e22ad
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.security.config.annotation.web.configurers;
|
package org.springframework.security.config.annotation.web.configurers;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@ -37,7 +38,10 @@ import org.springframework.security.web.authentication.SimpleUrlAuthenticationFa
|
|||||||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
||||||
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
|
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
|
||||||
import org.springframework.security.web.util.matcher.RequestMatcher;
|
import org.springframework.security.web.util.matcher.RequestMatcher;
|
||||||
|
import org.springframework.security.web.util.matcher.AndRequestMatcher;
|
||||||
import org.springframework.security.web.util.matcher.MediaTypeRequestMatcher;
|
import org.springframework.security.web.util.matcher.MediaTypeRequestMatcher;
|
||||||
|
import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
|
||||||
|
import org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher;
|
||||||
import org.springframework.web.accept.ContentNegotiationStrategy;
|
import org.springframework.web.accept.ContentNegotiationStrategy;
|
||||||
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
|
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
|
||||||
|
|
||||||
@ -243,10 +247,17 @@ public abstract class AbstractAuthenticationFilterConfigurer<B extends HttpSecur
|
|||||||
if (contentNegotiationStrategy == null) {
|
if (contentNegotiationStrategy == null) {
|
||||||
contentNegotiationStrategy = new HeaderContentNegotiationStrategy();
|
contentNegotiationStrategy = new HeaderContentNegotiationStrategy();
|
||||||
}
|
}
|
||||||
MediaTypeRequestMatcher preferredMatcher = new MediaTypeRequestMatcher(
|
|
||||||
|
MediaTypeRequestMatcher mediaMatcher = new MediaTypeRequestMatcher(
|
||||||
contentNegotiationStrategy, MediaType.APPLICATION_XHTML_XML,
|
contentNegotiationStrategy, MediaType.APPLICATION_XHTML_XML,
|
||||||
new MediaType("image", "*"), MediaType.TEXT_HTML, MediaType.TEXT_PLAIN);
|
new MediaType("image", "*"), MediaType.TEXT_HTML, MediaType.TEXT_PLAIN);
|
||||||
preferredMatcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
|
mediaMatcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
|
||||||
|
|
||||||
|
RequestMatcher notXRequestedWith = new NegatedRequestMatcher(
|
||||||
|
new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest"));
|
||||||
|
|
||||||
|
RequestMatcher preferredMatcher = new AndRequestMatcher(Arrays.asList(notXRequestedWith, mediaMatcher));
|
||||||
|
|
||||||
exceptionHandling.defaultAuthenticationEntryPointFor(
|
exceptionHandling.defaultAuthenticationEntryPointFor(
|
||||||
postProcess(authenticationEntryPoint), preferredMatcher);
|
postProcess(authenticationEntryPoint), preferredMatcher);
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,18 @@ class ExceptionHandlingConfigurerTests extends BaseSpringSpec {
|
|||||||
then:
|
then:
|
||||||
def entryPoints = delegateEntryPoint.entryPoints.keySet() as List
|
def entryPoints = delegateEntryPoint.entryPoints.keySet() as List
|
||||||
entryPoints[0].requestMatchers[1].contentNegotiationStrategy.class == HeaderContentNegotiationStrategy
|
entryPoints[0].requestMatchers[1].contentNegotiationStrategy.class == HeaderContentNegotiationStrategy
|
||||||
entryPoints[1].contentNegotiationStrategy.class == HeaderContentNegotiationStrategy
|
entryPoints[1].requestMatchers[1].contentNegotiationStrategy.class == HeaderContentNegotiationStrategy
|
||||||
|
}
|
||||||
|
|
||||||
|
def "401 for text/plain and X-Requested-With:XMLHttpRequest"() {
|
||||||
|
setup:
|
||||||
|
loadConfig(HttpBasicAndFormLoginEntryPointsConfig)
|
||||||
|
when:
|
||||||
|
request.addHeader("Accept", MediaType.TEXT_PLAIN_VALUE)
|
||||||
|
request.addHeader("X-Requested-With", "XMLHttpRequest")
|
||||||
|
springSecurityFilterChain.doFilter(request,response,chain)
|
||||||
|
then:
|
||||||
|
response.status == HttpServletResponse.SC_UNAUTHORIZED
|
||||||
}
|
}
|
||||||
|
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@ -126,7 +137,7 @@ class ExceptionHandlingConfigurerTests extends BaseSpringSpec {
|
|||||||
DelegatingAuthenticationEntryPoint delegateEntryPoint = findFilter(ExceptionTranslationFilter).authenticationEntryPoint
|
DelegatingAuthenticationEntryPoint delegateEntryPoint = findFilter(ExceptionTranslationFilter).authenticationEntryPoint
|
||||||
then:
|
then:
|
||||||
def entryPoints = delegateEntryPoint.entryPoints.keySet() as List
|
def entryPoints = delegateEntryPoint.entryPoints.keySet() as List
|
||||||
entryPoints[0].contentNegotiationStrategy == OverrideContentNegotiationStrategySharedObjectConfig.CNS
|
entryPoints[0].requestMatchers[1].contentNegotiationStrategy == OverrideContentNegotiationStrategySharedObjectConfig.CNS
|
||||||
entryPoints[1].requestMatchers[1].contentNegotiationStrategy == OverrideContentNegotiationStrategySharedObjectConfig.CNS
|
entryPoints[1].requestMatchers[1].contentNegotiationStrategy == OverrideContentNegotiationStrategySharedObjectConfig.CNS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user