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