|
@@ -20,6 +20,7 @@ import java.net.URI;
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
import java.util.Base64;
|
|
import java.util.Base64;
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
|
+import java.util.function.Consumer;
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.apache.commons.logging.LogFactory;
|
|
@@ -59,6 +60,9 @@ public class CookieServerRequestCache implements ServerRequestCache {
|
|
|
|
|
|
private ServerWebExchangeMatcher saveRequestMatcher = createDefaultRequestMatcher();
|
|
private ServerWebExchangeMatcher saveRequestMatcher = createDefaultRequestMatcher();
|
|
|
|
|
|
|
|
+ private Consumer<ResponseCookie.ResponseCookieBuilder> cookieCustomizer = (cookieBuilder) -> {
|
|
|
|
+ };
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Sets the matcher to determine if the request should be saved. The default is to
|
|
* Sets the matcher to determine if the request should be saved. The default is to
|
|
* match on any GET request.
|
|
* match on any GET request.
|
|
@@ -77,8 +81,10 @@ public class CookieServerRequestCache implements ServerRequestCache {
|
|
.map((m) -> exchange.getResponse())
|
|
.map((m) -> exchange.getResponse())
|
|
.map(ServerHttpResponse::getCookies)
|
|
.map(ServerHttpResponse::getCookies)
|
|
.doOnNext((cookies) -> {
|
|
.doOnNext((cookies) -> {
|
|
- ResponseCookie redirectUriCookie = createRedirectUriCookie(exchange.getRequest());
|
|
|
|
- cookies.add(REDIRECT_URI_COOKIE_NAME, redirectUriCookie);
|
|
|
|
|
|
+ ResponseCookie.ResponseCookieBuilder redirectUriCookie = createRedirectUriCookieBuilder(
|
|
|
|
+ exchange.getRequest());
|
|
|
|
+ this.cookieCustomizer.accept(redirectUriCookie);
|
|
|
|
+ cookies.add(REDIRECT_URI_COOKIE_NAME, redirectUriCookie.build());
|
|
logger.debug(LogMessage.format("Request added to Cookie: %s", redirectUriCookie));
|
|
logger.debug(LogMessage.format("Request added to Cookie: %s", redirectUriCookie));
|
|
})
|
|
})
|
|
.then();
|
|
.then();
|
|
@@ -103,25 +109,35 @@ public class CookieServerRequestCache implements ServerRequestCache {
|
|
.thenReturn(exchange.getRequest());
|
|
.thenReturn(exchange.getRequest());
|
|
}
|
|
}
|
|
|
|
|
|
- private static ResponseCookie createRedirectUriCookie(ServerHttpRequest request) {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the {@link Consumer}, allowing customization of cookie.
|
|
|
|
+ * @param cookieCustomizer customize for cookie
|
|
|
|
+ * @since 6.4
|
|
|
|
+ */
|
|
|
|
+ public void setCookieCustomizer(Consumer<ResponseCookie.ResponseCookieBuilder> cookieCustomizer) {
|
|
|
|
+ Assert.notNull(cookieCustomizer, "cookieCustomizer cannot be null");
|
|
|
|
+ this.cookieCustomizer = cookieCustomizer;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static ResponseCookie.ResponseCookieBuilder createRedirectUriCookieBuilder(ServerHttpRequest request) {
|
|
String path = request.getPath().pathWithinApplication().value();
|
|
String path = request.getPath().pathWithinApplication().value();
|
|
String query = request.getURI().getRawQuery();
|
|
String query = request.getURI().getRawQuery();
|
|
String redirectUri = path + ((query != null) ? "?" + query : "");
|
|
String redirectUri = path + ((query != null) ? "?" + query : "");
|
|
- return createResponseCookie(request, encodeCookie(redirectUri), COOKIE_MAX_AGE);
|
|
|
|
|
|
+ return createResponseCookieBuilder(request, encodeCookie(redirectUri), COOKIE_MAX_AGE);
|
|
}
|
|
}
|
|
|
|
|
|
private static ResponseCookie invalidateRedirectUriCookie(ServerHttpRequest request) {
|
|
private static ResponseCookie invalidateRedirectUriCookie(ServerHttpRequest request) {
|
|
- return createResponseCookie(request, null, Duration.ZERO);
|
|
|
|
|
|
+ return createResponseCookieBuilder(request, null, Duration.ZERO).build();
|
|
}
|
|
}
|
|
|
|
|
|
- private static ResponseCookie createResponseCookie(ServerHttpRequest request, String cookieValue, Duration age) {
|
|
|
|
|
|
+ private static ResponseCookie.ResponseCookieBuilder createResponseCookieBuilder(ServerHttpRequest request,
|
|
|
|
+ String cookieValue, Duration age) {
|
|
return ResponseCookie.from(REDIRECT_URI_COOKIE_NAME, cookieValue)
|
|
return ResponseCookie.from(REDIRECT_URI_COOKIE_NAME, cookieValue)
|
|
.path(request.getPath().contextPath().value() + "/")
|
|
.path(request.getPath().contextPath().value() + "/")
|
|
.maxAge(age)
|
|
.maxAge(age)
|
|
.httpOnly(true)
|
|
.httpOnly(true)
|
|
.secure("https".equalsIgnoreCase(request.getURI().getScheme()))
|
|
.secure("https".equalsIgnoreCase(request.getURI().getScheme()))
|
|
- .sameSite("Lax")
|
|
|
|
- .build();
|
|
|
|
|
|
+ .sameSite("Lax");
|
|
}
|
|
}
|
|
|
|
|
|
private static String encodeCookie(String cookieValue) {
|
|
private static String encodeCookie(String cookieValue) {
|