Tomcat中LegacyCookieProcessor与Rfc6265CookieProcessor

本文讲述了在升级Tomcat依赖库后遇到的cookie解析失败问题,主要聚焦于LegacyCookieProcessor和Rfc6265CookieProcessor的区别,以及如何通过配置解决跨版本兼容性问题。

最近升级某个依赖库,遇到cookie解析失败的问题,网上查了查资料,在这里学习记录一下。

背景

       近日有用户反馈tomcat升级后应用出现了一些问题,出现问题的这段时间内,tomcat从8.0.47升级到了8.5.43。 问题主要分为两类:

  1. cookie写入过程中,domain如果以.开头则无法写入,比如.xx.com写入会报错,而写入xx.com则没问题。
  2. cookie读取后应用无法解析,写入cookie的值采用的是Base64算法。

定位

       经过一番搜索,发现tomcat在这两个版本中,cookie的写入和解析策略确实发生了一些变化,可见Tomcat的文档,里面有这么一段提示:

The standard implementation of CookieProcessor is org.apache.tomcat.util.http.LegacyCookieProcessor. Note that it is anticipated that this will change to org.apache.tomcat.util.http.Rfc6265CookieProcessor in a future Tomcat 8 release.

由于8.0过后就直接到了8.5,

  • 8.5开始就默认使用了org.apache.tomcat.util.http.Rfc6265CookieProcessor
  • 而之前的版本中一直使用的是org.apache.tomcat.util.http.LegacyCookieProcessor,

下面就来看看这两种策略到底有哪些不同.

LegacyCookieProcessor

org.apache.tomcat.util.http.LegacyCookieProcessor主要是实现了标准RFC6265, RFC2109RFC2616.

写入cookie

写入cookie的逻辑都在generateHeader方法中. 这个方法逻辑大概是:

  1. 直接拼接 cookie.getName()然后拼接=.
  2. 校验cookie.getValue()以确定是否需要为value加上引号.
 private void maybeQuote(StringBuffer buf, String value, int version) {
        if (value == null || value.length() == 0) {
            buf.append("\"\"");
        } else if (alreadyQuoted(value)) {
            buf.append('"');
            escapeDoubleQuotes(buf, value,1,value.length()-1);
            buf.append('"');
        } else if (needsQuotes(value, version)) {
            buf.append('"');
            escapeDoubleQuotes(buf, value,0,value.length());
            buf.append('"');
        } else {
            buf.append(value);
        }
    }
    
     private boolean needsQuotes(String value, int version) {
        ...
        for (; i < len; i++) {
            char c = value.charAt(i);
            if ((c < 0x20 && c != '\t') || c >= 0x7f) {
                throw new IllegalArgumentException(
                        "Control character in cookie value or attribute.");
            }
            if (version == 0 && !allowedWithoutQuotes.get(c) ||
                    version == 1 && isHttpSeparator(c)) {
                return tru
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值