Browse Source

Add urlDecode property to ServletCookieValueMethodArgumentResolver

Closes gh-26989
pull/31781/head
rstoyanchev 2 years ago
parent
commit
753409083d
  1. 21
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java
  2. 16
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolverTests.java

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

@ -1,5 +1,5 @@ @@ -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.
@ -37,7 +37,7 @@ import org.springframework.web.util.WebUtils; @@ -37,7 +37,7 @@ import org.springframework.web.util.WebUtils;
*/
public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValueMethodArgumentResolver {
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
private UrlPathHelper urlPathHelper = new UrlPathHelper();
public ServletCookieValueMethodArgumentResolver(@Nullable ConfigurableBeanFactory beanFactory) {
@ -45,6 +45,23 @@ public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValu @@ -45,6 +45,23 @@ public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValu
}
/**
* Whether to apply URL decoding to cookie values via
* {@link UrlPathHelper#decodeRequestString(HttpServletRequest, String)}.
* A shortcut for doing the same by setting a {@link UrlPathHelper} with
* its {@code urlDecode} property set accordingly.
* <p>By default set to "true" in which case cookie values are decoded.
* @since 6.1.2
*/
public void setUrlDecode(boolean urlDecode) {
this.urlPathHelper.setUrlDecode(urlDecode);
}
/**
* Set the {@code UrlPathHelper} to use to decode cookie values with via
* {@link UrlPathHelper#decodeRequestString(HttpServletRequest, String)}.
* For most cases you can use {@link #setUrlDecode(boolean)} instead.
*/
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
this.urlPathHelper = urlPathHelper;
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
@ -79,8 +79,20 @@ public class ServletCookieValueMethodArgumentResolverTests { @@ -79,8 +79,20 @@ public class ServletCookieValueMethodArgumentResolverTests {
assertThat(result).as("Invalid result").isEqualTo(cookie.getValue());
}
@Test // gh-26989
public void resolveCookieWithEncodingTurnedOff() throws Exception {
Cookie cookie = new Cookie("name", "Tl=Q/0AUSOx[n)2z4(t]20FZv#?[Ge%H");
request.setCookies(cookie);
this.resolver.setUrlDecode(false);
String result = (String) resolver.resolveArgument(cookieStringParameter, null, webRequest, null);
assertThat(result).as("Invalid result").isEqualTo(cookie.getValue());
}
public void params(@CookieValue("name") Cookie cookie,
public void params(
@CookieValue("name") Cookie cookie,
@CookieValue(name = "name", defaultValue = "bar") String cookieString) {
}

Loading…
Cancel
Save