tomcat返回cookie有双引号问题

时间:2022-07-25
本文章向大家介绍tomcat返回cookie有双引号问题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.场景 后端对response进行写入cookie,代码如下:

 @RequestMapping(value = "passToken",method = RequestMethod.POST)
    public void passToken(String token, HttpServletResponse response){
        if (null == token|| "".equals(token) ) return ;
        Cookie cookie = null;
        try {
            String decodeCookie = URLDecoder.decode(token, "utf-8");
            cookie = new Cookie("PASESSION",decodeCookie);
            log.info("after urlDecode is " +decodeCookie);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        cookie.setDomain("xx.xx.xx.xx");
        cookie.setPath("/");
        response.addCookie(cookie);
    }

日志打印出来的value是没有双引号的,但是到了浏览器中,通过F12查看就有。

2.问题发现 发现与平时的cookie属性不一样,多了一个version=1的额外项。 经了解还有version=0的场景。 因为代码是一致的,在其他tomcat版本执行,没有问题,所以想到是tomcat版本不一致导致的。 目前场景所用的版本为: tomcat 8.0.35 而正常可使用的tomcat版本是较高的版本8.5.6

3.问题解决 在版本较低的tomcat下,进行对catalina.properties加上如下配置:

org.apache.tomcat.util.http.ServerCookie.ALLOW_EQUALS_IN_VALUE=true
org.apache.tomcat.util.http.ServerCookie.ALLOW_HTTP_SEPARATORS_IN_V0=true
org.apache.tomcat.util.http.ServerCookie.ALLOW_HTTP_SEPARATORS_IN_V1=true

即可。