diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java index e41c49ce0bf..160cd0cf3ad 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java @@ -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; */ 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 } + /** + * 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. + *

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; } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolverTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolverTests.java index 80507cad758..d6847d7e8ed 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolverTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolverTests.java @@ -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 { 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) { }