Skip to content

Commit e0a7b07

Browse files
svorcmarrstoyanchev
authored andcommitted
Use original query string of forwarded request
Prior to this commit, the AbstractFlashMapManager has used the originating URI but the query string of the forwarded request. That resulted to FlashMap not being matched even when both originating URI and query string matched the FlashMap attributes. The originating query string is now used to match the forwarded request. Issue: SPR-15505
1 parent 4fc41ee commit e0a7b07

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.util.StringUtils;
3434
import org.springframework.web.servlet.FlashMap;
3535
import org.springframework.web.servlet.FlashMapManager;
36-
import org.springframework.web.util.UriComponents;
3736
import org.springframework.web.util.UrlPathHelper;
3837

3938

@@ -173,8 +172,7 @@ protected boolean isFlashMapForRequest(FlashMap flashMap, HttpServletRequest req
173172
return false;
174173
}
175174
}
176-
UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(request).build();
177-
MultiValueMap<String, String> actualParams = uriComponents.getQueryParams();
175+
MultiValueMap<String, String> actualParams = getOriginatingRequestParams(request);
178176
MultiValueMap<String, String> expectedParams = flashMap.getTargetRequestParams();
179177
for (String expectedName : expectedParams.keySet()) {
180178
List<String> actualValues = actualParams.get(expectedName);
@@ -190,6 +188,11 @@ protected boolean isFlashMapForRequest(FlashMap flashMap, HttpServletRequest req
190188
return true;
191189
}
192190

191+
private MultiValueMap<String, String> getOriginatingRequestParams(HttpServletRequest request) {
192+
String query = getUrlPathHelper().getOriginatingQueryString(request);
193+
return ServletUriComponentsBuilder.fromPath("/").query(query).build().getQueryParams();
194+
}
195+
193196
@Override
194197
public final void saveOutputFlashMap(FlashMap flashMap, HttpServletRequest request, HttpServletResponse response) {
195198
if (CollectionUtils.isEmpty(flashMap)) {

spring-webmvc/src/test/java/org/springframework/web/servlet/support/FlashMapManagerTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.URLEncoder;
2222
import java.util.ArrayList;
2323
import java.util.Arrays;
24+
import java.util.Collections;
2425
import java.util.List;
2526
import java.util.concurrent.CopyOnWriteArrayList;
2627

@@ -318,6 +319,25 @@ public void flashAttributesWithQueryParamsWithSpace() throws Exception {
318319
assertEquals("value", flashMap.get("key"));
319320
}
320321

322+
@Test // SPR-15505
323+
public void retrieveAndUpdateMatchByOriginatingPathAndQueryString() {
324+
FlashMap flashMap = new FlashMap();
325+
flashMap.put("key", "value");
326+
flashMap.setTargetRequestPath("/accounts");
327+
flashMap.addTargetRequestParam("a", "b");
328+
329+
this.flashMapManager.setFlashMaps(Collections.singletonList(flashMap));
330+
331+
this.request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/accounts");
332+
this.request.setAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE, "a=b");
333+
this.request.setRequestURI("/mvc/accounts");
334+
this.request.setQueryString("x=y");
335+
FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);
336+
337+
assertEquals(flashMap, inputFlashMap);
338+
assertEquals("Input FlashMap should have been removed", 0, this.flashMapManager.getFlashMaps().size());
339+
}
340+
321341

322342
private static class TestFlashMapManager extends AbstractFlashMapManager {
323343

0 commit comments

Comments
 (0)