Merge branch '6.0.x' into 6.1.x

Closes gh-13482
This commit is contained in:
Josh Cummings 2023-07-10 16:17:13 -06:00
commit c58e0dd113
No known key found for this signature in database
GPG Key ID: A306A51F43B8E5A5
2 changed files with 13 additions and 4 deletions

View File

@ -37,6 +37,7 @@ import org.springframework.security.web.PortResolver;
import org.springframework.security.web.util.UrlUtils; import org.springframework.security.web.util.UrlUtils;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.web.util.UriComponentsBuilder;
/** /**
* Represents central information from a {@code HttpServletRequest}. * Represents central information from a {@code HttpServletRequest}.
@ -371,10 +372,8 @@ public class DefaultSavedRequest implements SavedRequest {
if (queryString == null || queryString.length() == 0) { if (queryString == null || queryString.length() == 0) {
return matchingRequestParameterName; return matchingRequestParameterName;
} }
if (queryString.endsWith("&")) { return UriComponentsBuilder.newInstance().query(queryString).replaceQueryParam(matchingRequestParameterName)
return queryString + matchingRequestParameterName; .queryParam(matchingRequestParameterName).build().getQuery();
}
return queryString + "&" + matchingRequestParameterName;
} }
/** /**

View File

@ -122,4 +122,14 @@ public class DefaultSavedRequestTests {
assertThat(new URL(savedRequest.getRedirectUrl())).hasQuery("foo=bar&success"); assertThat(new URL(savedRequest.getRedirectUrl())).hasQuery("foo=bar&success");
} }
// gh-13438
@Test
public void getRedirectUrlWhenQueryAlreadyHasSuccessThenDoesNotAdd() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setQueryString("foo=bar&success");
DefaultSavedRequest savedRequest = new DefaultSavedRequest(request, new MockPortResolver(8080, 8443),
"success");
assertThat(savedRequest.getRedirectUrl()).contains("foo=bar&success");
}
} }