一、背景:
公司最近将jboss的版本从4.5 升级到7,之后发现从cookie中,获取不到用户的信息了。经过debug发现,保存用户信息的cookie,在request.getCookie()之后就是被截断了。本来是 key=x_l=1&x_locale=zh_CN&no_popup_today=n&user=xxx|xxx|xxx|xxx|xxx&xxx=xxx 但是获取到值是key=x_l。
二、相关代码以及问题解决:
在jboss包里 org.apache.tomcat.util.http.Cookies 类里 有如下方法:
/**
* Given the starting position of a token, this gets the end of the token, with no separator characters in between.
* JVK
*/
private static final int getTokenEndPosition(byte bytes[], int off, int end, int version, boolean isName) {
int pos = off;
while (pos < end
&& (!CookieSupport.isHttpSeparator((char) bytes[pos]) || version == 0
&& CookieSupport.ALLOW_HTTP_SEPARATORS_IN_V0 && bytes[pos] != '='
&& !CookieSupport.isV0Separator((char) bytes[pos]) || !isName && bytes[pos] == '='
&& CookieSupport.ALLOW_EQUALS_IN_VALUE)) {
pos++;
}
if (pos > end) return end;
return pos;
}
当拿到x_l=1&x_locale=zh_CN&no_popup_today=n&x_user=user=xxx|xxx|xxx|xxx|xxx&xxx=xxx 这个字符串的时候,会通过上面的方法,来找到字符串最后一个可用字符的位置,然后截断。自然我们获取到key=x_l 也正式这个方法截断导致的。当遇到“=”的时候
bytes[pos] == '=' && CookieSupport.ALLOW_EQUALS_IN_VALUE 这个判断依赖与CookieSupport.ALLOW_EQUALS_IN_VALUE的值。
在CookieSupport 类中可以发现:
ALLOW_EQUALS_IN_VALUE = Boolean.valueOf(System.getProperty(
"org.apache.tomcat.util.http.ServerCookie.ALLOW_EQUALS_IN_VALUE",
"false")).booleanValue();
所以找到问题的答案,将 org.apache.tomcat.util.http.ServerCookie.ALLOW_EQUALS_IN_VALUE配置为true,及解决问题。
但是为了之前jboss4.5没有这个问题呢? 参见:http://thenitai.com/2013/05/02/tomcat-truncating-cookies-with-values/
三、jboss 7 cookie 解析流程:
四、扩展:
cookie版本的问题:网上很容易找到对应资料,摘录一段:
1. Cookie的兼容性问题
Cookie的格式有2个不同的版本,第一个版本,我们称为Cookie Version 0,是最初由Netscape公司制定的,也被几乎所有的浏览器支持。而较新的版本,Cookie Version 1,则是根据RFC 2109文档制定的。为了确保兼容性,JAVA规定,前面所提到的涉及Cookie的操作都是针对旧版本的Cookie进行的。而新版本的Cookie目前还不被Javax.servlet.http.Cookie包所支持。
2. Cookie的内容
同样的Cookie的内容的字符限制针对不同的Cookie版本也有不同。在Cookie Version 0中,某些特殊的字符,例如:空格,方括号,圆括号,等于号(=),逗号,双引号,斜杠,问号,@符号,冒号,分号都不能作为Cookie的内容。
虽然在Cookie Version 1规定中放宽了限制,可以使用这些字符,但是考虑到新版本的Cookie规范目前仍然没有为所有的浏览器所支持,因而为保险起见,我们应该在Cookie的内容中尽量避免使用这些字符。
3、cookie version 1规范
RFC 2109 参见:http://www.faqs.org/rfcs/rfc2109.html
关于cookie中value定义 参见:http://www.faqs.org/rfcs/rfc2068.html
4、cookie vesion 2 规范:http://www.faqs.org/rfcs/rfc2965.html
在 CookieSupport 类也可以看到定义的特殊字符:
/*
Excluding the '/' char by default violates the RFC, but
it looks like a lot of people put '/'
in unquoted values: '/': ; //47
'\t':9 ' ':32 '\"':34 '(':40 ')':41 ',':44 ':':58 ';':59 '<':60
'=':61 '>':62 '?':63 '@':64 '[':91 '\\':92 ']':93 '{':123 '}':125
*/
if (CookieSupport.FWD_SLASH_IS_SEPARATOR) {
HTTP_SEPARATORS = new char[] { '\t', ' ', '\"', '(', ')', ',', '/',
':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '{', '}' };
} else {
HTTP_SEPARATORS = new char[] { '\t', ' ', '\"', '(', ')', ',',
':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '{', '}' };
}