|
4 | 4 |
|
5 | 5 | import java.net.URI;
|
6 | 6 | import java.util.Collections;
|
| 7 | +import java.util.Objects; |
7 | 8 | import org.apache.commons.lang3.StringUtils;
|
8 | 9 | import org.springframework.http.HttpMethod;
|
9 | 10 | import org.springframework.http.MediaType;
|
@@ -58,24 +59,49 @@ public Mono<URI> getRedirectUri(ServerWebExchange exchange) {
|
58 | 59 |
|
59 | 60 | @Override
|
60 | 61 | public Mono<ServerHttpRequest> removeMatchingRequest(ServerWebExchange exchange) {
|
61 |
| - return super.removeMatchingRequest(exchange); |
| 62 | + return getRedirectUri(exchange) |
| 63 | + .flatMap(redirectUri -> { |
| 64 | + if (redirectUri.getFragment() != null) { |
| 65 | + var redirectUriInApplication = |
| 66 | + uriInApplication(exchange.getRequest(), redirectUri, false); |
| 67 | + var uriInApplication = |
| 68 | + uriInApplication(exchange.getRequest(), exchange.getRequest().getURI()); |
| 69 | + // compare the path and query only |
| 70 | + if (!Objects.equals(redirectUriInApplication, uriInApplication)) { |
| 71 | + return Mono.empty(); |
| 72 | + } |
| 73 | + // remove the exchange |
| 74 | + return exchange.getSession().map(WebSession::getAttributes) |
| 75 | + .doOnNext(attributes -> attributes.remove(this.sessionAttrName)) |
| 76 | + .thenReturn(exchange.getRequest()); |
| 77 | + } |
| 78 | + return super.removeMatchingRequest(exchange); |
| 79 | + }); |
62 | 80 | }
|
63 | 81 |
|
64 | 82 | private Mono<Void> saveRedirectUri(ServerWebExchange exchange, URI redirectUri) {
|
65 |
| - var requestPath = exchange.getRequest().getPath(); |
66 |
| - var redirectPath = RequestPath.parse(redirectUri, requestPath.contextPath().value()); |
67 |
| - var query = redirectUri.getRawQuery(); |
68 |
| - var fragment = redirectUri.getRawFragment(); |
69 |
| - var finalRedirect = redirectPath.pathWithinApplication() |
70 |
| - + (query == null ? "" : "?" + query) |
71 |
| - + (fragment == null ? "" : "#" + fragment); |
72 |
| - |
| 83 | + var redirectUriInApplication = uriInApplication(exchange.getRequest(), redirectUri); |
73 | 84 | return exchange.getSession()
|
74 | 85 | .map(WebSession::getAttributes)
|
75 |
| - .doOnNext(attributes -> attributes.put(this.sessionAttrName, finalRedirect)) |
| 86 | + .doOnNext(attributes -> attributes.put(this.sessionAttrName, redirectUriInApplication)) |
76 | 87 | .then();
|
77 | 88 | }
|
78 | 89 |
|
| 90 | + private static String uriInApplication(ServerHttpRequest request, URI uri) { |
| 91 | + return uriInApplication(request, uri, true); |
| 92 | + } |
| 93 | + |
| 94 | + private static String uriInApplication( |
| 95 | + ServerHttpRequest request, URI uri, boolean appendFragment |
| 96 | + ) { |
| 97 | + var path = RequestPath.parse(uri, request.getPath().contextPath().value()); |
| 98 | + var query = uri.getRawQuery(); |
| 99 | + var fragment = uri.getRawFragment(); |
| 100 | + return path.pathWithinApplication().value() |
| 101 | + + (query == null ? "" : "?" + query) |
| 102 | + + (fragment == null || !appendFragment ? "" : "#" + fragment); |
| 103 | + } |
| 104 | + |
79 | 105 | private static ServerWebExchangeMatcher createDefaultRequestMatcher() {
|
80 | 106 | var get = pathMatchers(HttpMethod.GET, "/**");
|
81 | 107 | var notFavicon = new NegatedServerWebExchangeMatcher(
|
|
0 commit comments