Skip to content

Commit ce012a4

Browse files
CookieRequestCache Should Preserve Request Locale
Closes gh-13792
1 parent 96d1763 commit ce012a4

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

web/src/main/java/org/springframework/security/web/savedrequest/CookieRequestCache.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.security.web.savedrequest;
1818

1919
import java.util.Base64;
20+
import java.util.Collections;
2021

2122
import javax.servlet.http.Cookie;
2223
import javax.servlet.http.HttpServletRequest;
@@ -78,7 +79,7 @@ public SavedRequest getRequest(HttpServletRequest request, HttpServletResponse r
7879
int port = getPort(uriComponents);
7980
return builder.setScheme(uriComponents.getScheme()).setServerName(uriComponents.getHost())
8081
.setRequestURI(uriComponents.getPath()).setQueryString(uriComponents.getQuery()).setServerPort(port)
81-
.setMethod(request.getMethod()).build();
82+
.setMethod(request.getMethod()).setLocales(Collections.list(request.getLocales())).build();
8283
}
8384

8485
private int getPort(UriComponents uriComponents) {

web/src/test/java/org/springframework/security/web/savedrequest/CookieRequestCacheTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,10 @@
1616

1717
package org.springframework.security.web.savedrequest;
1818

19+
import java.util.Arrays;
1920
import java.util.Base64;
21+
import java.util.Collections;
22+
import java.util.Locale;
2023

2124
import javax.servlet.http.Cookie;
2225
import javax.servlet.http.HttpServletRequest;
@@ -183,6 +186,25 @@ public void removeRequestWhenInvokedThenSetsAnExpiredCookieOnResponse() {
183186
assertThat(expiredCookie.getMaxAge()).isZero();
184187
}
185188

189+
// gh-13792
190+
@Test
191+
public void matchingRequestWhenMatchThenKeepOriginalRequestLocale() {
192+
CookieRequestCache cookieRequestCache = new CookieRequestCache();
193+
MockHttpServletRequest request = new MockHttpServletRequest();
194+
request.setServerPort(443);
195+
request.setSecure(true);
196+
request.setScheme("https");
197+
request.setServerName("example.com");
198+
request.setRequestURI("/destination");
199+
request.setPreferredLocales(Arrays.asList(Locale.FRENCH, Locale.GERMANY));
200+
String redirectUrl = "https://example.com/destination";
201+
request.setCookies(new Cookie(DEFAULT_COOKIE_NAME, encodeCookie(redirectUrl)));
202+
MockHttpServletResponse response = new MockHttpServletResponse();
203+
HttpServletRequest matchingRequest = cookieRequestCache.getMatchingRequest(request, response);
204+
assertThat(matchingRequest).isNotNull();
205+
assertThat(Collections.list(matchingRequest.getLocales())).contains(Locale.FRENCH, Locale.GERMANY);
206+
}
207+
186208
private static String encodeCookie(String cookieValue) {
187209
return Base64.getEncoder().encodeToString(cookieValue.getBytes());
188210
}

0 commit comments

Comments
 (0)