|
41 | 41 | import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
42 | 42 | import org.springframework.core.convert.converter.Converter;
|
43 | 43 | import org.springframework.http.HttpMethod;
|
| 44 | +import org.springframework.http.HttpStatus; |
44 | 45 | import org.springframework.http.MediaType;
|
45 | 46 | import org.springframework.security.authentication.AbstractAuthenticationToken;
|
46 | 47 | import org.springframework.security.authentication.DelegatingReactiveAuthenticationManager;
|
|
113 | 114 | import org.springframework.security.web.server.authentication.AuthenticationConverterServerWebExchangeMatcher;
|
114 | 115 | import org.springframework.security.web.server.authentication.AuthenticationWebFilter;
|
115 | 116 | import org.springframework.security.web.server.authentication.HttpBasicServerAuthenticationEntryPoint;
|
| 117 | +import org.springframework.security.web.server.authentication.HttpStatusServerEntryPoint; |
116 | 118 | import org.springframework.security.web.server.authentication.ReactivePreAuthenticatedAuthenticationManager;
|
117 | 119 | import org.springframework.security.web.server.authentication.RedirectServerAuthenticationEntryPoint;
|
118 | 120 | import org.springframework.security.web.server.authentication.RedirectServerAuthenticationFailureHandler;
|
@@ -1911,13 +1913,25 @@ public ServerHttpSecurity disable() {
|
1911 | 1913 | */
|
1912 | 1914 | public final class HttpBasicSpec {
|
1913 | 1915 |
|
| 1916 | + private final ServerWebExchangeMatcher xhrMatcher = (exchange) -> Mono.just(exchange.getRequest().getHeaders()) |
| 1917 | + .filter((h) -> h.getOrEmpty("X-Requested-With").contains("XMLHttpRequest")) |
| 1918 | + .flatMap((h) -> ServerWebExchangeMatcher.MatchResult.match()) |
| 1919 | + .switchIfEmpty(ServerWebExchangeMatcher.MatchResult.notMatch()); |
| 1920 | + |
1914 | 1921 | private ReactiveAuthenticationManager authenticationManager;
|
1915 | 1922 |
|
1916 | 1923 | private ServerSecurityContextRepository securityContextRepository;
|
1917 | 1924 |
|
1918 |
| - private ServerAuthenticationEntryPoint entryPoint = new HttpBasicServerAuthenticationEntryPoint(); |
| 1925 | + private ServerAuthenticationEntryPoint entryPoint; |
1919 | 1926 |
|
1920 | 1927 | private HttpBasicSpec() {
|
| 1928 | + List<DelegateEntry> entryPoints = new ArrayList<>(); |
| 1929 | + entryPoints |
| 1930 | + .add(new DelegateEntry(this.xhrMatcher, new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED))); |
| 1931 | + DelegatingServerAuthenticationEntryPoint defaultEntryPoint = new DelegatingServerAuthenticationEntryPoint( |
| 1932 | + entryPoints); |
| 1933 | + defaultEntryPoint.setDefaultEntryPoint(new HttpBasicServerAuthenticationEntryPoint()); |
| 1934 | + this.entryPoint = defaultEntryPoint; |
1921 | 1935 | }
|
1922 | 1936 |
|
1923 | 1937 | /**
|
@@ -1982,7 +1996,13 @@ protected void configure(ServerHttpSecurity http) {
|
1982 | 1996 | MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_XML, MediaType.MULTIPART_FORM_DATA,
|
1983 | 1997 | MediaType.TEXT_XML);
|
1984 | 1998 | restMatcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
|
1985 |
| - ServerHttpSecurity.this.defaultEntryPoints.add(new DelegateEntry(restMatcher, this.entryPoint)); |
| 1999 | + ServerWebExchangeMatcher notHtmlMatcher = new NegatedServerWebExchangeMatcher( |
| 2000 | + new MediaTypeServerWebExchangeMatcher(MediaType.TEXT_HTML)); |
| 2001 | + ServerWebExchangeMatcher restNotHtmlMatcher = new AndServerWebExchangeMatcher( |
| 2002 | + Arrays.asList(notHtmlMatcher, restMatcher)); |
| 2003 | + ServerWebExchangeMatcher preferredMatcher = new OrServerWebExchangeMatcher( |
| 2004 | + Arrays.asList(this.xhrMatcher, restNotHtmlMatcher)); |
| 2005 | + ServerHttpSecurity.this.defaultEntryPoints.add(new DelegateEntry(preferredMatcher, this.entryPoint)); |
1986 | 2006 | AuthenticationWebFilter authenticationFilter = new AuthenticationWebFilter(this.authenticationManager);
|
1987 | 2007 | authenticationFilter
|
1988 | 2008 | .setAuthenticationFailureHandler(new ServerAuthenticationEntryPointFailureHandler(this.entryPoint));
|
|
0 commit comments