在tomcat中的两个子域之间共享会话属性 [英] sharing session attribute between two subdomains in tomcat
问题描述
我使用的是 tomcat 7.0 和 java 6.我想在 examples.com 和 abc.examples.com
I am using tomcat 7.0 and java 6. I want to share session between domains and subdomain for examples.com and abc.examples.com
so 在 context.xml 中放置标签
so in context.xml put tag
<Context antiResourceLocking="false" privileged="true" sessionCookieDomain=".examples.com" sessionCookiePath="/" >
成功共享 Cookie JSESSIONID 但当我在 examples.com
successfully share Cookies JSESSIONID but when i set attribute in examples.com
logger.info( "Demo1 : "+httpSession.getId());
httpSession.setAttribute("data", "subhash lamba");
logger.info(" Demo1 data"+ httpSession.getAttribute("data") );
在 test.examples.com 子域中无法访问
HttpSession httpSession=request.getSession();
logger.info( "Demo3 : "+httpSession.getId());
logger.info(" Demo3 data "+ httpSession.getAttribute("data") );
当我在域和子域中登录 JSESSIONID 时相同但当我尝试共享属性时在他们之间它不起作用.
when i log JSESSIONID in both domain and subdomain are same but when i try to share attribute between them it is not working.
推荐答案
我自己找到了解决方案.我成功地获得了两个域之间的共享会话,现在我的问题是识别同一个用户.当我尝试在这两个域和子域之间共享会话数据时,它不起作用,因为服务器为单个应用程序维护会话.
I find solution my self. I successfully get sharing session between two domain and now just my problem was identify same user. When i try for sharing session data between those two domain and sub-domains at that time it's not work because server maintain session for individual application.
现在我的解决方案是:
在Tomcat服务器中有上下文,在上下文中我们可以存储对象,并且同一个Tomcat中的每个应用程序都可以访问相同的上下文.所以现在我在 java.util.Map 对象存储键值对中的 java.util.Map 对象上创建,其中键是 JSESSIONID,值是登录的用户 ID.所以现在我可以轻松访问登录域和所有子域的用户.
In Tomcat server there is context, In context we can store Object and same Context can be accessible by every application in same Tomcat. So Now I create on java.util.Map object in java.util.Map object store key-value pair in which key is JSESSIONID and value is user Id who login. So now i can access easily user who login in domain and all sub-domains.
这篇关于在tomcat中的两个子域之间共享会话属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!