|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -1181,22 +1181,54 @@ protected void configure(ServerHttpSecurity http) {
|
1181 | 1181 | authenticationFilter.setAuthenticationFailureHandler(getAuthenticationFailureHandler());
|
1182 | 1182 | authenticationFilter.setSecurityContextRepository(this.securityContextRepository);
|
1183 | 1183 |
|
1184 |
| - MediaTypeServerWebExchangeMatcher htmlMatcher = new MediaTypeServerWebExchangeMatcher( |
1185 |
| - MediaType.TEXT_HTML); |
1186 |
| - htmlMatcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); |
| 1184 | + setDefaultEntryPoints(http); |
| 1185 | + |
| 1186 | + http.addFilterAt(oauthRedirectFilter, SecurityWebFiltersOrder.HTTP_BASIC); |
| 1187 | + http.addFilterAt(authenticationFilter, SecurityWebFiltersOrder.AUTHENTICATION); |
| 1188 | + } |
| 1189 | + |
| 1190 | + private void setDefaultEntryPoints(ServerHttpSecurity http) { |
| 1191 | + String defaultLoginPage = "/login"; |
1187 | 1192 | Map<String, String> urlToText = http.oauth2Login.getLinks();
|
1188 |
| - String authenticationEntryPointRedirectPath; |
| 1193 | + String providerLoginPage = null; |
1189 | 1194 | if (urlToText.size() == 1) {
|
1190 |
| - authenticationEntryPointRedirectPath = urlToText.keySet().iterator().next(); |
1191 |
| - } else { |
1192 |
| - authenticationEntryPointRedirectPath = "/login"; |
| 1195 | + providerLoginPage = urlToText.keySet().iterator().next(); |
1193 | 1196 | }
|
1194 |
| - RedirectServerAuthenticationEntryPoint entryPoint = new RedirectServerAuthenticationEntryPoint(authenticationEntryPointRedirectPath); |
1195 |
| - entryPoint.setRequestCache(http.requestCache.requestCache); |
1196 |
| - http.defaultEntryPoints.add(new DelegateEntry(htmlMatcher, entryPoint)); |
1197 | 1197 |
|
1198 |
| - http.addFilterAt(oauthRedirectFilter, SecurityWebFiltersOrder.HTTP_BASIC); |
1199 |
| - http.addFilterAt(authenticationFilter, SecurityWebFiltersOrder.AUTHENTICATION); |
| 1198 | + MediaTypeServerWebExchangeMatcher htmlMatcher = new MediaTypeServerWebExchangeMatcher( |
| 1199 | + MediaType.APPLICATION_XHTML_XML, new MediaType("image", "*"), |
| 1200 | + MediaType.TEXT_HTML, MediaType.TEXT_PLAIN); |
| 1201 | + htmlMatcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); |
| 1202 | + |
| 1203 | + ServerWebExchangeMatcher xhrMatcher = exchange -> { |
| 1204 | + if (exchange.getRequest().getHeaders().getOrEmpty("X-Requested-With").contains("XMLHttpRequest")) { |
| 1205 | + return ServerWebExchangeMatcher.MatchResult.match(); |
| 1206 | + } |
| 1207 | + return ServerWebExchangeMatcher.MatchResult.notMatch(); |
| 1208 | + }; |
| 1209 | + ServerWebExchangeMatcher notXhrMatcher = new NegatedServerWebExchangeMatcher(xhrMatcher); |
| 1210 | + |
| 1211 | + ServerWebExchangeMatcher defaultEntryPointMatcher = new AndServerWebExchangeMatcher( |
| 1212 | + notXhrMatcher, htmlMatcher); |
| 1213 | + |
| 1214 | + if (providerLoginPage != null) { |
| 1215 | + ServerWebExchangeMatcher loginPageMatcher = new PathPatternParserServerWebExchangeMatcher(defaultLoginPage); |
| 1216 | + ServerWebExchangeMatcher faviconMatcher = new PathPatternParserServerWebExchangeMatcher("/favicon.ico"); |
| 1217 | + ServerWebExchangeMatcher defaultLoginPageMatcher = new AndServerWebExchangeMatcher( |
| 1218 | + new OrServerWebExchangeMatcher(loginPageMatcher, faviconMatcher), defaultEntryPointMatcher); |
| 1219 | + |
| 1220 | + ServerWebExchangeMatcher matcher = new AndServerWebExchangeMatcher( |
| 1221 | + notXhrMatcher, new NegatedServerWebExchangeMatcher(defaultLoginPageMatcher)); |
| 1222 | + RedirectServerAuthenticationEntryPoint entryPoint = |
| 1223 | + new RedirectServerAuthenticationEntryPoint(providerLoginPage); |
| 1224 | + entryPoint.setRequestCache(http.requestCache.requestCache); |
| 1225 | + http.defaultEntryPoints.add(new DelegateEntry(matcher, entryPoint)); |
| 1226 | + } |
| 1227 | + |
| 1228 | + RedirectServerAuthenticationEntryPoint defaultEntryPoint = |
| 1229 | + new RedirectServerAuthenticationEntryPoint(defaultLoginPage); |
| 1230 | + defaultEntryPoint.setRequestCache(http.requestCache.requestCache); |
| 1231 | + http.defaultEntryPoints.add(new DelegateEntry(defaultEntryPointMatcher, defaultEntryPoint)); |
1200 | 1232 | }
|
1201 | 1233 |
|
1202 | 1234 | private ServerAuthenticationSuccessHandler getAuthenticationSuccessHandler(ServerHttpSecurity http) {
|
|
0 commit comments