Tomcat 8 CookieProcessor 实现变化

栏目: 服务器 · 发布时间: 5年前

内容简介:Tomcat 从7升级到8的时候出现了配置中有这么段配置:在tomcat context.xml中配置

问题

Tomcat 从7升级到8的时候出现了 java.lang.IllegalArgumentException: An invalid domain [.xxx.com] was specified for this cookie 异常。

配置中有这么段配置:

<Context useHttpOnly="true" sessionCookiePath="/" sessionCookieDomain=".jobmd.cn" />

解决

在tomcat context.xml中配置 <CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" /> 即可,或者你也可以把域名前面的 . 去掉。

分析

Tomcat 8更换了默认的 CookieProcessor 实现为 Rfc6265CookieProcessor ,之前的实现为 LegacyCookieProcessor 。前者是基于 RFC6265 ,而后者基于 RFC6265RFC2109RFC2616

RFC6265 中关于domain有这么一段描述:

Domain=domain  Optional. The Domain attribute specifies the domain for which the  cookie is valid. An explicitly specified domain must always start  with a dot.

而在 RFC6265 中:

If the attribute-name case-insensitively matches the string “Domain”,  the user agent MUST process the cookie-av as follows.
If the attribute-value is empty, the behavior is undefined. However,  the user agent SHOULD ignore the cookie-av entirely.
If the first character of the attribute-value string is %x2E (“.”):
Let cookie-domain be the attribute-value without the leading %x2E  (“.”) character.
Otherwise:
Let cookie-domain be the entire attribute-value.
Convert the cookie-domain to lower case.

一个说要 . 一个说不要,那再来看看具体代码实现(generateHeader方法)。

LegacyCookieProcessor (省略了部分代码)

@Override
public String generateHeader(Cookie cookie) {
        ......
        String domain = cookie.getDomain();
        ......
        // Add domain information, if present
        if (domain != null) {
                buf.append("; Domain=");
                maybeQuote(buf, domain, version);
        }
        ......
}

再看看maybeQuote方法实现:

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);
        }
}

可以看到并没有做任何domain校验的操作;

Rfc6265CookieProcessor

@Override
public String generateHeader(Cookie cookie) {
        ......
        String domain = cookie.getDomain();
        if (domain != null && domain.length() > 0) {
            validateDomain(domain);
            header.append("; Domain=");
            header.append(domain);
        }
        ......
}
private void validateDomain(String domain) {
        int i = 0;
        int prev = -1;
        int cur = -1;
        char[] chars = domain.toCharArray();
        while (i < chars.length) {
            prev = cur;
            cur = chars[i];
            if (!domainValid.get(cur)) {
                throw new IllegalArgumentException(sm.getString(
                        "rfc6265CookieProcessor.invalidDomain", domain));
            }
            // labels must start with a letter or number
            // RFC6265实现
            if ((prev == '.' || prev == -1) && (cur == '.' || cur == '-')) {
                throw new IllegalArgumentException(sm.getString(
                        "rfc6265CookieProcessor.invalidDomain", domain));
            }
            // labels must end with a letter or number
            // RFC6265实现
            if (prev == '-' && cur == '.') {
                throw new IllegalArgumentException(sm.getString(
                        "rfc6265CookieProcessor.invalidDomain", domain));
            }
            i++;
        }
        // domain must end with a label
        if (cur == '.' || cur == '-') {
            throw new IllegalArgumentException(sm.getString(
                    "rfc6265CookieProcessor.invalidDomain", domain));
        }
}

validateDomain方法中实现了 RFC6265 ,这蛋疼的标准改动还真是随意。

BugHome版权所有丨转载请注明出处:https://minei.me/archives/407.html


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

史蒂夫·乔布斯传

史蒂夫·乔布斯传

[美] 沃尔特·艾萨克森 / 管延圻、魏群、余倩、赵萌萌、汤崧 / 中信出版社 / 2011-10-24 / 68.00元

这本乔布斯唯一授权的官方传记,在2011年上半年由美国出版商西蒙舒斯特对外发布出版消息以来,备受全球媒体和业界瞩目,这本书的全球出版日期最终确定为2011年11月21日,简体中文版也将同步上市。 两年多的时间,与乔布斯40多次的面对面倾谈,以及与乔布斯一百多个家庭成员、 朋友、竞争对手、同事的不受限的采访,造就了这本独家传记。 尽管乔布斯给予本书的采访和创作全面的配合,但他对内容从不干......一起来看看 《史蒂夫·乔布斯传》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

在线进制转换器
在线进制转换器

各进制数互转换器

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具