From bf83e4e8612512ef51272d8a7dd8533686e8a4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20=C5=A0vorc?= Date: Sun, 7 May 2017 20:59:09 +0200 Subject: [PATCH] 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 --- .../support/AbstractFlashMapManager.java | 5 +++-- .../servlet/support/FlashMapManagerTests.java | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java index 61f3ef9216..06a04df339 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java @@ -167,13 +167,14 @@ public abstract class AbstractFlashMapManager implements FlashMapManager { */ protected boolean isFlashMapForRequest(FlashMap flashMap, HttpServletRequest request) { String expectedPath = flashMap.getTargetRequestPath(); + String requestUri = getUrlPathHelper().getOriginatingRequestUri(request); if (expectedPath != null) { - String requestUri = getUrlPathHelper().getOriginatingRequestUri(request); if (!requestUri.equals(expectedPath) && !requestUri.equals(expectedPath + "/")) { return false; } } - UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(request).build(); + String queryString = getUrlPathHelper().getOriginatingQueryString(request); + UriComponents uriComponents = ServletUriComponentsBuilder.fromUriString(requestUri).query(queryString).build(); MultiValueMap actualParams = uriComponents.getQueryParams(); MultiValueMap expectedParams = flashMap.getTargetRequestParams(); for (String expectedName : expectedParams.keySet()) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/support/FlashMapManagerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/support/FlashMapManagerTests.java index 8fa215672e..829975b0c3 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/support/FlashMapManagerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/support/FlashMapManagerTests.java @@ -318,6 +318,26 @@ public class FlashMapManagerTests { assertEquals("value", flashMap.get("key")); } + // SPR-15505 + + @Test + public void retrieveAndUpdateMatchByOriginatingPathAndQueryString() { + FlashMap flashMap = new FlashMap(); + flashMap.put("key", "value"); + flashMap.setTargetRequestPath("/accounts"); + flashMap.addTargetRequestParam("a", "b"); + + this.flashMapManager.setFlashMaps(Arrays.asList(flashMap)); + + this.request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/accounts"); + this.request.setAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE, "a=b"); + this.request.setRequestURI("/mvc/accounts"); + this.request.setQueryString("x=y"); + FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response); + + assertEquals(flashMap, inputFlashMap); + assertEquals("Input FlashMap should have been removed", 0, this.flashMapManager.getFlashMaps().size()); + } private static class TestFlashMapManager extends AbstractFlashMapManager {