20
20
import java .time .Duration ;
21
21
import java .util .Base64 ;
22
22
import java .util .Collections ;
23
+ import java .util .function .Consumer ;
23
24
24
25
import org .apache .commons .logging .Log ;
25
26
import org .apache .commons .logging .LogFactory ;
@@ -59,6 +60,9 @@ public class CookieServerRequestCache implements ServerRequestCache {
59
60
60
61
private ServerWebExchangeMatcher saveRequestMatcher = createDefaultRequestMatcher ();
61
62
63
+ private Consumer <ResponseCookie .ResponseCookieBuilder > cookieCustomizer = (cookieBuilder ) -> {
64
+ };
65
+
62
66
/**
63
67
* Sets the matcher to determine if the request should be saved. The default is to
64
68
* match on any GET request.
@@ -77,8 +81,10 @@ public Mono<Void> saveRequest(ServerWebExchange exchange) {
77
81
.map ((m ) -> exchange .getResponse ())
78
82
.map (ServerHttpResponse ::getCookies )
79
83
.doOnNext ((cookies ) -> {
80
- ResponseCookie redirectUriCookie = createRedirectUriCookie (exchange .getRequest ());
81
- cookies .add (REDIRECT_URI_COOKIE_NAME , redirectUriCookie );
84
+ ResponseCookie .ResponseCookieBuilder redirectUriCookie = createRedirectUriCookieBuilder (
85
+ exchange .getRequest ());
86
+ this .cookieCustomizer .accept (redirectUriCookie );
87
+ cookies .add (REDIRECT_URI_COOKIE_NAME , redirectUriCookie .build ());
82
88
logger .debug (LogMessage .format ("Request added to Cookie: %s" , redirectUriCookie ));
83
89
})
84
90
.then ();
@@ -103,25 +109,35 @@ public Mono<ServerHttpRequest> removeMatchingRequest(ServerWebExchange exchange)
103
109
.thenReturn (exchange .getRequest ());
104
110
}
105
111
106
- private static ResponseCookie createRedirectUriCookie (ServerHttpRequest request ) {
112
+ /**
113
+ * Sets the {@link Consumer}, allowing customization of cookie.
114
+ * @param cookieCustomizer customize for cookie
115
+ * @since 6.4
116
+ */
117
+ public void setCookieCustomizer (Consumer <ResponseCookie .ResponseCookieBuilder > cookieCustomizer ) {
118
+ Assert .notNull (cookieCustomizer , "cookieCustomizer cannot be null" );
119
+ this .cookieCustomizer = cookieCustomizer ;
120
+ }
121
+
122
+ private static ResponseCookie .ResponseCookieBuilder createRedirectUriCookieBuilder (ServerHttpRequest request ) {
107
123
String path = request .getPath ().pathWithinApplication ().value ();
108
124
String query = request .getURI ().getRawQuery ();
109
125
String redirectUri = path + ((query != null ) ? "?" + query : "" );
110
- return createResponseCookie (request , encodeCookie (redirectUri ), COOKIE_MAX_AGE );
126
+ return createResponseCookieBuilder (request , encodeCookie (redirectUri ), COOKIE_MAX_AGE );
111
127
}
112
128
113
129
private static ResponseCookie invalidateRedirectUriCookie (ServerHttpRequest request ) {
114
- return createResponseCookie (request , null , Duration .ZERO );
130
+ return createResponseCookieBuilder (request , null , Duration .ZERO ). build ( );
115
131
}
116
132
117
- private static ResponseCookie createResponseCookie (ServerHttpRequest request , String cookieValue , Duration age ) {
133
+ private static ResponseCookie .ResponseCookieBuilder createResponseCookieBuilder (ServerHttpRequest request ,
134
+ String cookieValue , Duration age ) {
118
135
return ResponseCookie .from (REDIRECT_URI_COOKIE_NAME , cookieValue )
119
136
.path (request .getPath ().contextPath ().value () + "/" )
120
137
.maxAge (age )
121
138
.httpOnly (true )
122
139
.secure ("https" .equalsIgnoreCase (request .getURI ().getScheme ()))
123
- .sameSite ("Lax" )
124
- .build ();
140
+ .sameSite ("Lax" );
125
141
}
126
142
127
143
private static String encodeCookie (String cookieValue ) {
0 commit comments