关键字: 二级域名, session
Tomcat下,不同的二级域名,Session默认是不共享的,因为Cookie名称为JSESSIONID的Cookie根域是默认是没设置的,访问不同的二级域名,其Cookie就重新生成,而session就是根据这个Cookie来生成的,所以在不同的二级域名下生成的Session也不一样。找到了其原因,就可根据这个原因对Tomcat在生成Session时进行相应的修改(注:本文针对Tomcat 6.0)。
在Tomcat 对Request的实现类中org.apache.catalina.connector.Request,找到
- ......
- protected void configureSessionCookie(Cookie cookie) {
- cookie.setMaxAge(-1);
- String contextPath = null;
- if (!connector.getEmptySessionPath() && (getContext() != null)) {
- contextPath = getContext().getEncodedPath();
- }
- if ((contextPath != null) && (contextPath.length() > 0)) {
- cookie.setPath(contextPath);
- } else {
- cookie.setPath("/");
- }
- <span style="color: #ff0000;">cookie.setDomain("xxx.com");</span>
- if (isSecure()) {
- cookie.setSecure(true);
- }
- }
- ...
添加上面加注红色的部分即可,在不同的二级域名下,用Request获取Session都将是同一个。
也在在Tomcat的配置文件(D:/Tomcat6.0/conf/catalina.properties)中添加
- herostarDomain=xxx.com
在上面的实现类中,红色部分做相应的修改
- ...
- if(null !=System.getProperty("herostarDomain") && ! "".equals(System.getProperty("herostarDomain")) )
- cookie.setDomain(System.getProperty("herostarDomain"));
- ....
好了,OK!