Skip to content

Commit 7534090

Browse files
committed
Add urlDecode property to ServletCookieValueMethodArgumentResolver
Closes gh-26989
1 parent e36d035 commit 7534090

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java

Lines changed: 19 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.
@@ -37,14 +37,31 @@
3737
*/
3838
public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValueMethodArgumentResolver {
3939

40-
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
40+
private UrlPathHelper urlPathHelper = new UrlPathHelper();
4141

4242

4343
public ServletCookieValueMethodArgumentResolver(@Nullable ConfigurableBeanFactory beanFactory) {
4444
super(beanFactory);
4545
}
4646

4747

48+
/**
49+
* Whether to apply URL decoding to cookie values via
50+
* {@link UrlPathHelper#decodeRequestString(HttpServletRequest, String)}.
51+
* A shortcut for doing the same by setting a {@link UrlPathHelper} with
52+
* its {@code urlDecode} property set accordingly.
53+
* <p>By default set to "true" in which case cookie values are decoded.
54+
* @since 6.1.2
55+
*/
56+
public void setUrlDecode(boolean urlDecode) {
57+
this.urlPathHelper.setUrlDecode(urlDecode);
58+
}
59+
60+
/**
61+
* Set the {@code UrlPathHelper} to use to decode cookie values with via
62+
* {@link UrlPathHelper#decodeRequestString(HttpServletRequest, String)}.
63+
* For most cases you can use {@link #setUrlDecode(boolean)} instead.
64+
*/
4865
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
4966
this.urlPathHelper = urlPathHelper;
5067
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolverTests.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -79,8 +79,20 @@ public void resolveCookieStringArgument() throws Exception {
7979
assertThat(result).as("Invalid result").isEqualTo(cookie.getValue());
8080
}
8181

82+
@Test // gh-26989
83+
public void resolveCookieWithEncodingTurnedOff() throws Exception {
84+
Cookie cookie = new Cookie("name", "Tl=Q/0AUSOx[n)2z4(t]20FZv#?[Ge%H");
85+
request.setCookies(cookie);
86+
87+
this.resolver.setUrlDecode(false);
88+
String result = (String) resolver.resolveArgument(cookieStringParameter, null, webRequest, null);
89+
90+
assertThat(result).as("Invalid result").isEqualTo(cookie.getValue());
91+
}
92+
8293

83-
public void params(@CookieValue("name") Cookie cookie,
94+
public void params(
95+
@CookieValue("name") Cookie cookie,
8496
@CookieValue(name = "name", defaultValue = "bar") String cookieString) {
8597
}
8698

0 commit comments

Comments
 (0)