Skip to content

Commit

Permalink
CookieRequestCache Should Preserve Request Locale
Browse files Browse the repository at this point in the history
Closes gh-13792
  • Loading branch information
marcusdacoregio committed Sep 12, 2023
1 parent 96d1763 commit ce012a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package org.springframework.security.web.savedrequest;

import java.util.Base64;
import java.util.Collections;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -78,7 +79,7 @@ public SavedRequest getRequest(HttpServletRequest request, HttpServletResponse r
int port = getPort(uriComponents);
return builder.setScheme(uriComponents.getScheme()).setServerName(uriComponents.getHost())
.setRequestURI(uriComponents.getPath()).setQueryString(uriComponents.getQuery()).setServerPort(port)
.setMethod(request.getMethod()).build();
.setMethod(request.getMethod()).setLocales(Collections.list(request.getLocales())).build();
}

private int getPort(UriComponents uriComponents) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,10 @@

package org.springframework.security.web.savedrequest;

import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.Locale;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -183,6 +186,25 @@ public void removeRequestWhenInvokedThenSetsAnExpiredCookieOnResponse() {
assertThat(expiredCookie.getMaxAge()).isZero();
}

// gh-13792
@Test
public void matchingRequestWhenMatchThenKeepOriginalRequestLocale() {
CookieRequestCache cookieRequestCache = new CookieRequestCache();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setServerPort(443);
request.setSecure(true);
request.setScheme("https");
request.setServerName("example.com");
request.setRequestURI("/destination");
request.setPreferredLocales(Arrays.asList(Locale.FRENCH, Locale.GERMANY));
String redirectUrl = "https://example.com/destination";
request.setCookies(new Cookie(DEFAULT_COOKIE_NAME, encodeCookie(redirectUrl)));
MockHttpServletResponse response = new MockHttpServletResponse();
HttpServletRequest matchingRequest = cookieRequestCache.getMatchingRequest(request, response);
assertThat(matchingRequest).isNotNull();
assertThat(Collections.list(matchingRequest.getLocales())).contains(Locale.FRENCH, Locale.GERMANY);
}

private static String encodeCookie(String cookieValue) {
return Base64.getEncoder().encodeToString(cookieValue.getBytes());
}
Expand Down

0 comments on commit ce012a4

Please sign in to comment.