- 浏览: 1315674 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (471)
- 原创文章 (4)
- Database (84)
- J2SE (63)
- Web (26)
- Javascript (30)
- Lucene (11)
- os (13)
- 算法 (8)
- Webservice (1)
- Open projects (18)
- Hibernate (18)
- Spring (15)
- Css (2)
- J2ee (2)
- 综合技术 (18)
- 安全管理 (13)
- PatternsInJava (27)
- NIO (5)
- Ibatis (2)
- 书籍收藏 (1)
- quartz (7)
- 并发编程 (15)
- oracle问题 (2)
- ios (60)
- coco2d-iphone (3)
- C++ (6)
- Zookeeper (2)
- golang (4)
- animation (2)
- android (1)
最新评论
-
dandingge123:
【引用】限制UITextField输入长度的方法 -
qja:
...
对List顺序,逆序,随机排列实例代码 -
安静听歌:
现在在搞这个,,,,,哎~头都大了,,,又freemarker ...
通用大型网站页面静态化解决方案(一) -
springdata-jpa:
java quartz定时任务demo教程源代码下载,地址:h ...
Quartz 配置参考 -
马清天:
[b][/b][list][*]引用[u][/u][/list ...
通用大型网站页面静态化解决方案(一)
项目中遇到了每个用户一个二级域名的应用,但在主域名登录后,在二级域名的session中不能获当前已登录的用户,在网上找了一种方式,由于他写的不够完善,而且在我的应用中还有错,所以我再重写一次。
新建一个java项目,新建org.three3s.valves包,新建如下CrossSubdomainSessionValve类
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.catalina.*;
import org.apache.catalina.connector.*;
import org.apache.catalina.valves.*;
import org.apache.tomcat.util.buf.*;
import org.apache.tomcat.util.http.*;
/**
* <p>
* Replaces the domain of the session cookie generated by Tomcat with a domain
* that allows that session cookie to be shared across subdomains. This valve
* digs down into the response headers and replaces the Set-Cookie header for
* the session cookie, instead of futilely trying to modify an existing Cookie
* object like the example at http://www.esus.be/blog/?p=3. That approach does
* not work (at least as of Tomcat 6.0.14) because the
* <code>org.apache.catalina.connector.Response.addCookieInternal</code> method
* renders the cookie into the Set-Cookie response header immediately, making
* any subsequent modifying calls on the Cookie object ultimately pointless.
* </p>
*
* <p>
* This results in a single, cross-subdomain session cookie on the client that
* allows the session to be shared across all subdomains. However, see the
* {@link getCookieDomain(Request)} method for limits on the subdomains.
* </p>
*
* <p>
* Note though, that this approach will fail if the response has already been
* committed. Thus, this valve forces Tomcat to generate the session cookie and
* then replaces it before invoking the next valve in the chain. Hopefully this
* is early enough in the valve-processing chain that the response will not have
* already been committed. You are advised to define this valve as early as
* possible in server.xml to ensure that the response has not already been
* committed when this valve is invoked.
* </p>
*
* <p>
* We recommend that you define this valve in server.xml immediately after the
* Catalina Engine as follows:
*
* <pre>
* <Engine name="Catalina"...>
* <Valve className="org.three3s.valves.CrossSubdomainSessionValve"/>
* </pre>
*
* </p>
*/
public class CrossSubdomainSessionValve
extends ValveBase
{
public CrossSubdomainSessionValve()
{
super();
info = "org.three3s.valves.CrossSubdomainSessionValve/1.0";
}
@Override
public void invoke(Request request, Response response) throws
IOException, ServletException
{
//this will cause Request.doGetSession to create the session cookie if necessary
request.getSession(true);
//replace any Tomcat-generated session cookies with our own
Cookie[] cookies = response.getCookies();
if (cookies != null)
{
for (int i = 0; i < cookies.length; i++)
{
Cookie cookie = cookies[i];
if (Globals.SESSION_COOKIE_NAME.equals(cookie.getName()))
replaceCookie(request, response, cookie);
}
}
//process the next valve
getNext().invoke(request, response);
}
/**
* Replaces the value of the response header used to set the specified
* cookie to a value with the cookie's domain set to the value returned by
* <code>getCookieDomain(request)</code>
*
* @param request
* @param response
* @param cookie
* cookie to be replaced.
*/
protected void replaceCookie(Request request, Response response,
Cookie cookie)
{
//copy the existing session cookie, but use a different domain
Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue());
if (cookie.getPath() != null)
newCookie.setPath(cookie.getPath());
newCookie.setDomain(getCookieDomain(request));
newCookie.setMaxAge(cookie.getMaxAge());
newCookie.setVersion(cookie.getVersion());
if (cookie.getComment() != null)
newCookie.setComment(cookie.getComment());
newCookie.setSecure(cookie.getSecure());
//if the response has already been committed, our replacementstrategy will have no effect
//if (response.isCommitted())
//find the Set-Cookie header for the existing cookie andreplace its value with new cookie
MimeHeaders headers = response.getCoyoteResponse().getMimeHeaders();
for (int i = 0, size = headers.size(); i < size; i++)
{
if (headers.getName(i).equals("Set-Cookie"))
{
MessageBytes value = headers.getValue(i);
if (value.indexOf(cookie.getName()) >= 0)
{
StringBuffer buffer = new StringBuffer();
ServerCookie.appendCookieValue(buffer,newCookie.getVersion(), newCookie
.getName(), newCookie.getValue(),newCookie.getPath(), newCookie
.getDomain(), newCookie.getComment(),newCookie.getMaxAge(), newCookie
.getSecure());
value.setString(buffer.toString());
}
}
}
}
/**
* Returns the last two parts of the specified request's server name
* preceded by a dot. Using this as the session cookie's domain allows the
* session to be shared across subdomains. Note that this implies the
* session can only be used with domains consisting of two or three parts,
* according to the domain-matching rules specified in RFC 2109 and RFC
* 2965.
*
* <p>
* Examples:
* </p>
* <ul>
* <li>foo.com => .foo.com</li>
* <li>www.foo.com => .foo.com</li>
* <li>bar.foo.com => .foo.com</li>
* <li>abc.bar.foo.com => .foo.com - this means cookie won't work on
* abc.bar.foo.com!</li>
* </ul>
*
* @param request
* provides the server name used to create cookie domain.
* @return the last two parts of the specified request's server name
* preceded by a dot.
*/
protected String getCookieDomain(Request request)
{
String cookieDomain = request.getServerName();
String[] parts = cookieDomain.split("\\.");
if (parts.length >= 2)
cookieDomain = parts[parts.length - 2] + "."
+ parts[parts.length - 1];
return "." + cookieDomain;
}
public String toString()
{
return ("CrossSubdomainSessionValve[container=" + container.getName() + ']');
}
}
注:我删了“clin8888”的log日志记录,因为在我的应用里报错.
在项目中导入$CATALINA_HOME/lib下所有的jar包,就不会报错了,然后导出一个jar文件,放入 $CATALINA_HOME/lib中,修改 $CATALINA_HOME/conf/server.xml文件,将
放入<Host>标签中,也可以放到<Engine>标签中。个人猜想:如果放入<Host>标签中应该只是当前项目的主域名和二级域名session共享,如果放到<Engine>标签中,应该是该tomcat下所有的项目都是主域名和二级域名共享(没有实验)。
方便对于tomcat的二级域名的使用..而导致session失效的解决方法..
需要引用的包是..
下载CrossSubdomainSessionValve包.. 把下载的包,放到$CATALINA_HOME/server/lib 里面
然后修改tomcat的配置文件: $CATALINA_HOME/conf/server.xml
在标签"Engine",中添加依家配置标签..
<Valve className="org.three3s.valves.CrossSubdomainSessionValve"/>
类似于:
<Engine name="Catalina"...>
<valve className="org.three3s.valves.CrossSubdomainSessionValve"/>
</Engine>
以下是图片显示:
发表
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wqfeng520/archive/2010/11/29/6042596.aspx
http://skyluck.iteye.com/blog/747465
- CrossSubdomainSessionValve.jar (3.2 KB)
- 下载次数: 98
发表评论
-
JavaScript初学者应注意的七个细节
2011-01-16 22:34 1073每种语言都有它特别的地方,对于JavaScript来说,使 ... -
META功能总结
2011-01-07 21:13 4623一、META的作用: meta标签通常用来为搜索引擎ro ... -
实践中整理出tomcat集群和负载均衡
2010-11-25 23:33 1140(一)环境说明 (1)服务器有4台,一台安装apac ... -
httpsession的原理及负载均衡
2010-11-15 12:06 13507前阵子去面试正好被问到httpsession和cookie,今 ... -
Apache负载均衡+Tomcat集群
2010-10-26 21:56 1374核心提示:目标 : 使用 apache 和 tomcat ... -
Spring Ldap 域认证
2010-10-26 21:17 4437核心提示:近段时间接触了一个项目,用户认证部分是通过域认证 ... -
Spring + Tomcat 中配置连接池
2010-10-26 21:12 4812核心提示:Tomcat5 及 Tomcat6 下CP配置。 主 ... -
基于总线的消息服务(BBMS)的设计与实现
2010-10-26 21:09 2561核心提示:前言 异步事件的通知机制在比较有规模的软件设计中 ... -
JSON --- JAVA 使用方法
2010-10-25 22:51 1133JSON 即 JavaScript Object Natati ... -
大型门户网站架构分析
2010-10-24 15:48 5145千万人同时访问的网站,一般是有很多个数据库同时工作,说明白一点 ... -
fck config
2010-10-19 10:48 1499写道 * * FCKeditor - The text ... -
功能强大的fck编辑器(完整详解)
2010-10-19 10:39 3672一直都没找到完整的Fck ... -
ORACLE中的ROWID
2010-10-14 16:36 10631、rowid是一个伪列,是用来确保表中行的唯一性,它 ... -
FCKeditor在线编辑器
2010-10-14 16:20 1542FCKeditor在线编辑器 FCKeditor 这个开源 ... -
用java获取真实的ip地址
2010-10-14 16:18 2163在JSP里,获取客户端的IP地址的方法是:reques ... -
实践中整理出tomcat集群和负载均衡
2010-10-14 15:50 1119实践中整理出tomcat集群 ... -
关于MapleFetion
2010-07-28 17:40 1793http://code.google.com/p/maplef ... -
jfreechart
2010-06-28 18:13 1421http://dev.firnow.com/course/3_ ... -
Java中如何实现Comet风格的Web应用(二)
2010-03-30 17:00 2814CometProcessor 接口要求实现 event 方法 ... -
Java中如何实现Comet风格的Web应用(一)
2010-03-30 16:55 1920开始 在本文中,我将展示如何使用各种不同的 Java ...
相关推荐
Tomcat二级域名Session共享例子,具体操作看博客:http://blog.csdn.net/luogqing/article/details/78595200
Tomcat二级域名Session共享例子,具体操作看博客:http://blog.csdn.net/luogqing/article/details/78595200
方便新手使用的tomcat二级域名所需要的包.. 详细的使用方法.查看.. http://blog.csdn.net/wqfeng520/archive/2010/11/29/6042596.aspx
如何实现服务器集群session共享,redis用来tomcat共享。
Tomcat8+redis实现session共享 Tomcat8+redis实现session共享
apache + tomcat整合配置二级域名
Tomcat集群配置,实现session共享 环境:jdk1.8.0_131,tomcat8
tomcat配置session共享,亲试过tomcat 7.0.82版本。提供了三个依赖的jar,拷贝到tomcat的lib下面。修改content.xml文件。里面有案例。
tomcat7集群实现session共享
tomcat修改sessionId,同一台服务器部署多个tomcat需要修改sessionId,否则会出现session冲突的问题
nginx+tomcat8+memcached session共享所需jar包 直接放到tomcat/lib下即可
很好用,使用也很简单,把其中的三个Jar包拷贝到你的tomcat中的...tomcat8、8.5、9与redis实现session共享,并可以通过修改源码可自定义session键,访问地址:http://blog.csdn.net/fackyou200/article/details/78929008
配置tomcat+redis,将session存到redis,实现多个tomcat session共享
Nginx+Tomcat+Memcached实现tomcat集群和session共享 nginx配置
redis+tomcat集群配置session共享
使用Tomcat配置域名的方法。
linux.tomcat-session共享.redis.安装包
使用说明:请严格按照资源内的readme文档来说明部署,不要搞错版本,完美支持Tomcat8和Tomcat9等新版本Tomcat,仅支持JDK8以上版本JDK。
用于tomcat6 使用memcache 实现session共享的jar包,将此处5个jar包拷贝到tomcat目录下的lib目录下即可。
tomcat8配置session共享所需jar包