|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.web.server.savedrequest; |
| 18 | + |
| 19 | +import org.springframework.http.HttpCookie; |
| 20 | +import org.springframework.http.HttpMethod; |
| 21 | +import org.springframework.http.MediaType; |
| 22 | +import org.springframework.http.ResponseCookie; |
| 23 | +import org.springframework.http.server.reactive.ServerHttpRequest; |
| 24 | +import org.springframework.http.server.reactive.ServerHttpResponse; |
| 25 | +import org.springframework.security.web.server.util.matcher.AndServerWebExchangeMatcher; |
| 26 | +import org.springframework.security.web.server.util.matcher.MediaTypeServerWebExchangeMatcher; |
| 27 | +import org.springframework.security.web.server.util.matcher.NegatedServerWebExchangeMatcher; |
| 28 | +import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher; |
| 29 | +import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.util.MultiValueMap; |
| 32 | +import org.springframework.web.server.ServerWebExchange; |
| 33 | +import reactor.core.publisher.Mono; |
| 34 | + |
| 35 | +import java.net.URI; |
| 36 | +import java.time.Duration; |
| 37 | +import java.util.Base64; |
| 38 | +import java.util.Collections; |
| 39 | + |
| 40 | +/** |
| 41 | + * An implementation of {@link ServerRequestCache} that saves the |
| 42 | + * requested URI in a cookie. |
| 43 | + * |
| 44 | + * @author Eleftheria Stein |
| 45 | + * @since 5.3 |
| 46 | + */ |
| 47 | +public class CookieServerRequestCache implements ServerRequestCache { |
| 48 | + private static final String REDIRECT_URI_COOKIE_NAME = "REDIRECT_URI"; |
| 49 | + |
| 50 | + private static final Duration COOKIE_MAX_AGE = Duration.ofSeconds(-1); |
| 51 | + |
| 52 | + private ServerWebExchangeMatcher saveRequestMatcher = createDefaultRequestMatcher(); |
| 53 | + |
| 54 | + /** |
| 55 | + * Sets the matcher to determine if the request should be saved. The default is to match |
| 56 | + * on any GET request. |
| 57 | + * |
| 58 | + * @param saveRequestMatcher the {@link ServerWebExchangeMatcher} that determines if |
| 59 | + * the request should be saved |
| 60 | + */ |
| 61 | + public void setSaveRequestMatcher(ServerWebExchangeMatcher saveRequestMatcher) { |
| 62 | + Assert.notNull(saveRequestMatcher, "saveRequestMatcher cannot be null"); |
| 63 | + this.saveRequestMatcher = saveRequestMatcher; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Mono<Void> saveRequest(ServerWebExchange exchange) { |
| 68 | + return this.saveRequestMatcher.matches(exchange) |
| 69 | + .filter(m -> m.isMatch()) |
| 70 | + .map(m -> exchange.getResponse()) |
| 71 | + .map(ServerHttpResponse::getCookies) |
| 72 | + .doOnNext(cookies -> cookies.add(REDIRECT_URI_COOKIE_NAME, createRedirectUriCookie(exchange.getRequest()))) |
| 73 | + .then(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Mono<URI> getRedirectUri(ServerWebExchange exchange) { |
| 78 | + MultiValueMap<String, HttpCookie> cookieMap = exchange.getRequest().getCookies(); |
| 79 | + return Mono.justOrEmpty(cookieMap.getFirst(REDIRECT_URI_COOKIE_NAME)) |
| 80 | + .map(HttpCookie::getValue) |
| 81 | + .map(CookieServerRequestCache::decodeCookie) |
| 82 | + .onErrorResume(IllegalArgumentException.class, e -> Mono.empty()) |
| 83 | + .map(URI::create); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Mono<ServerHttpRequest> removeMatchingRequest(ServerWebExchange exchange) { |
| 88 | + return Mono.just(exchange.getResponse()) |
| 89 | + .map(ServerHttpResponse::getCookies) |
| 90 | + .doOnNext(cookies -> cookies.add(REDIRECT_URI_COOKIE_NAME, invalidateRedirectUriCookie(exchange.getRequest()))) |
| 91 | + .thenReturn(exchange.getRequest()); |
| 92 | + } |
| 93 | + |
| 94 | + private static ResponseCookie createRedirectUriCookie(ServerHttpRequest request) { |
| 95 | + String path = request.getPath().pathWithinApplication().value(); |
| 96 | + String query = request.getURI().getRawQuery(); |
| 97 | + String redirectUri = path + (query != null ? "?" + query : ""); |
| 98 | + |
| 99 | + return createResponseCookie(request, encodeCookie(redirectUri), COOKIE_MAX_AGE); |
| 100 | + } |
| 101 | + |
| 102 | + private static ResponseCookie invalidateRedirectUriCookie(ServerHttpRequest request) { |
| 103 | + return createResponseCookie(request, null, Duration.ZERO); |
| 104 | + } |
| 105 | + |
| 106 | + private static ResponseCookie createResponseCookie(ServerHttpRequest request, String cookieValue, Duration age) { |
| 107 | + return ResponseCookie.from(REDIRECT_URI_COOKIE_NAME, cookieValue) |
| 108 | + .path(request.getPath().contextPath().value() + "/") |
| 109 | + .maxAge(age) |
| 110 | + .httpOnly(true) |
| 111 | + .secure("https".equalsIgnoreCase(request.getURI().getScheme())) |
| 112 | + .sameSite("Lax") |
| 113 | + .build(); |
| 114 | + } |
| 115 | + |
| 116 | + private static String encodeCookie(String cookieValue) { |
| 117 | + return new String(Base64.getEncoder().encode(cookieValue.getBytes())); |
| 118 | + } |
| 119 | + |
| 120 | + private static String decodeCookie(String encodedCookieValue) { |
| 121 | + return new String(Base64.getDecoder().decode(encodedCookieValue.getBytes())); |
| 122 | + } |
| 123 | + |
| 124 | + private static ServerWebExchangeMatcher createDefaultRequestMatcher() { |
| 125 | + ServerWebExchangeMatcher get = ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/**"); |
| 126 | + ServerWebExchangeMatcher notFavicon = new NegatedServerWebExchangeMatcher(ServerWebExchangeMatchers.pathMatchers("/favicon.*")); |
| 127 | + MediaTypeServerWebExchangeMatcher html = new MediaTypeServerWebExchangeMatcher(MediaType.TEXT_HTML); |
| 128 | + html.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); |
| 129 | + return new AndServerWebExchangeMatcher(get, notFavicon, html); |
| 130 | + } |
| 131 | +} |
0 commit comments