|
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.
|
@@ -621,18 +621,54 @@ protected void configure(ServerHttpSecurity http) {
|
621 | 621 | authenticationFilter.setAuthenticationFailureHandler(new RedirectServerAuthenticationFailureHandler("/login?error"));
|
622 | 622 | authenticationFilter.setSecurityContextRepository(new WebSessionServerSecurityContextRepository());
|
623 | 623 |
|
624 |
| - MediaTypeServerWebExchangeMatcher htmlMatcher = new MediaTypeServerWebExchangeMatcher( |
625 |
| - MediaType.TEXT_HTML); |
626 |
| - htmlMatcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); |
| 624 | + setDefaultEntryPoints(http); |
| 625 | + |
| 626 | + http.addFilterAt(oauthRedirectFilter, SecurityWebFiltersOrder.HTTP_BASIC); |
| 627 | + http.addFilterAt(authenticationFilter, SecurityWebFiltersOrder.AUTHENTICATION); |
| 628 | + } |
| 629 | + |
| 630 | + private void setDefaultEntryPoints(ServerHttpSecurity http) { |
| 631 | + String defaultLoginPage = "/login"; |
627 | 632 | Map<String, String> urlToText = http.oauth2Login.getLinks();
|
| 633 | + String providerLoginPage = null; |
628 | 634 | if (urlToText.size() == 1) {
|
629 |
| - http.defaultEntryPoints.add(new DelegateEntry(htmlMatcher, new RedirectServerAuthenticationEntryPoint(urlToText.keySet().iterator().next()))); |
630 |
| - } else { |
631 |
| - http.defaultEntryPoints.add(new DelegateEntry(htmlMatcher, new RedirectServerAuthenticationEntryPoint("/login"))); |
| 635 | + providerLoginPage = urlToText.keySet().iterator().next(); |
632 | 636 | }
|
633 | 637 |
|
634 |
| - http.addFilterAt(oauthRedirectFilter, SecurityWebFiltersOrder.HTTP_BASIC); |
635 |
| - http.addFilterAt(authenticationFilter, SecurityWebFiltersOrder.AUTHENTICATION); |
| 638 | + MediaTypeServerWebExchangeMatcher htmlMatcher = new MediaTypeServerWebExchangeMatcher( |
| 639 | + MediaType.APPLICATION_XHTML_XML, new MediaType("image", "*"), |
| 640 | + MediaType.TEXT_HTML, MediaType.TEXT_PLAIN); |
| 641 | + htmlMatcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); |
| 642 | + |
| 643 | + ServerWebExchangeMatcher xhrMatcher = exchange -> { |
| 644 | + if (exchange.getRequest().getHeaders().getOrDefault("X-Requested-With", Collections.emptyList()).contains("XMLHttpRequest")) { |
| 645 | + return ServerWebExchangeMatcher.MatchResult.match(); |
| 646 | + } |
| 647 | + return ServerWebExchangeMatcher.MatchResult.notMatch(); |
| 648 | + }; |
| 649 | + ServerWebExchangeMatcher notXhrMatcher = new NegatedServerWebExchangeMatcher(xhrMatcher); |
| 650 | + |
| 651 | + ServerWebExchangeMatcher defaultEntryPointMatcher = new AndServerWebExchangeMatcher( |
| 652 | + notXhrMatcher, htmlMatcher); |
| 653 | + |
| 654 | + if (providerLoginPage != null) { |
| 655 | + ServerWebExchangeMatcher loginPageMatcher = new PathPatternParserServerWebExchangeMatcher(defaultLoginPage); |
| 656 | + ServerWebExchangeMatcher faviconMatcher = new PathPatternParserServerWebExchangeMatcher("/favicon.ico"); |
| 657 | + ServerWebExchangeMatcher defaultLoginPageMatcher = new AndServerWebExchangeMatcher( |
| 658 | + new OrServerWebExchangeMatcher(loginPageMatcher, faviconMatcher), defaultEntryPointMatcher); |
| 659 | + |
| 660 | + ServerWebExchangeMatcher matcher = new AndServerWebExchangeMatcher( |
| 661 | + notXhrMatcher, new NegatedServerWebExchangeMatcher(defaultLoginPageMatcher)); |
| 662 | + RedirectServerAuthenticationEntryPoint entryPoint = |
| 663 | + new RedirectServerAuthenticationEntryPoint(providerLoginPage); |
| 664 | + entryPoint.setRequestCache(http.requestCache.requestCache); |
| 665 | + http.defaultEntryPoints.add(new DelegateEntry(matcher, entryPoint)); |
| 666 | + } |
| 667 | + |
| 668 | + RedirectServerAuthenticationEntryPoint defaultEntryPoint = |
| 669 | + new RedirectServerAuthenticationEntryPoint(defaultLoginPage); |
| 670 | + defaultEntryPoint.setRequestCache(http.requestCache.requestCache); |
| 671 | + http.defaultEntryPoints.add(new DelegateEntry(defaultEntryPointMatcher, defaultEntryPoint)); |
636 | 672 | }
|
637 | 673 |
|
638 | 674 | private ServerWebExchangeMatcher createAttemptAuthenticationRequestMatcher() {
|
|
0 commit comments