问题描述
我正在开发一个应用程序网络,其中我想向我的用户提供多个应用程序,他们只需要注册/登录一次并使用所有具有相同登录名的应用程序。
I am developing an application network where I want to offer multiple applications to my users with them only having to register/login a single time and use all of the applications with the same login.
为了实现这一点,我创建了一个cookie,其中存储用户的会话ID(当他登录时他收到)。每当用户打开一个应用程序时,启动模块会尝试查找该Cookie,并向服务器发送一个请求,以检查该会话是否仍然有效。
To achieve this, I created a cookie in which I store the Session ID of the user(which he receives when he logs in). Everytime a user opens up an application, the Launch Module tries to find the cookie and sends a request to the server to check if that session is still valid.
当我测试这在我的本地dev机器,一切工作正常,但在我的预生活测试服务器,我在一个应用程序创建的cookie是没有找到其他应用程序。
When I tested this on my local dev machine, everything worked fine, but on my pre-live test server, the cookie I created in the one application is not found by the other application.
我的测试服务器是一个Tomcat 7.
通过.war-upload部署的应用
My Test Server is a Tomcat 7.Apllications deployed via .war-upload
Cookie最初是使用以下命令创建的: .setCookie(WebsiteName,result.getSessionId());
Cookies are initially created using this command: Cookies.setCookie("WebsiteName", result.getSessionId());
感谢每一个帮助!
编辑1
我得到了一些进一步的工作,因为我想要的工作。
I got a little bit further on getting everything to work as I wanted it.
最初我没有实现HttpServlet会话,但是一个自己的会话管理系统,它存储我自己的会话ID在cookie中。检查服务器日志,我发现,如果两个应用程序都部署在子路径下,应用程序1的会话输出如下:
Originally I did not implement the HttpServlet Session, but an own Session management System, which stored my own session ID in the cookie. Checking the Server Logs, I found out, that if both applications are deployed under a sub path, the Session Output is the following for application 1:
Test HttpSession ID: 34446C7F3F345F74A0AAB5E292A47021 | Own Session ID: 3a25884692c499d5e72f07bb2b214a40f63f5a4a842852c58f30e1b46cf7bee7bc8a3394a9fe83a04ac4e1dcb4069dbca95a8f0001e012bc643934e08af35ec2
和申请2
Test HttpSession ID: 429388DE8F0F76F877B077433FE16B66 | Own Session ID: 4e6b19a7621893de0cd0826b298ea4d8eb5ffecf4a7503f3274f729c2df28f4a1e9ce52179730b4139804f256591149fd712be76ad3afd87bade4f58aab4234f
我有这样的Tomcat生成每个Web应用程序的另一个会话ID的嫌疑,所以我将我的主应用程序的.war-File重命名为 ROOT.war
,并将其部署为Root应用程序。输出是用于根apllication以下内容:
I had the suspicion that Tomcat generates another Session ID for every web application, so I renamed my main application's .war-File to ROOT.war
and deployed it as the Root application. The Output was the following for the ROOT apllication:
Test HttpSession ID: B9F2D14A716C3A06E328F58ED0995D95 | Own Session ID: 039b1c0cd2726dcfa1d8585da589e05984cfb6c971e083423d8456673837ea954ead42728aecf93be358ed13c757e3848ee3eb061a2253350c7d6dc1a14c970c
和在路径apllication 根/ webapp1 /
:
AND for the apllication at path root/webapp1/
:
Test HttpSession ID: BFD836C297B1BF8204D243387401CCA7 | Own Session ID: 039b1c0cd2726dcfa1d8585da589e05984cfb6c971e083423d8456673837ea954ead42728aecf93be358ed13c757e3848ee3eb061a2253350c7d6dc1a14c970c
在自己的会话ID存储在cookie中,并通过服务器进行检查。我的结论是,浏览器为我的域的每个子路径创建不同的cookie。
The own Session ID is stored in the cookie and checked by the server. My conclusion is, that the browsers create Different cookies for each subpath of my domain.
添加一个域名到Cookie最后做的伎俩,傻我: / p>
Adding a Domain name to the Cookie finally did the trick, silly me :-)
推荐答案
您检查过日志吗?在日志或开发人员工具控制台(firebug?)中得到的错误是什么?详细的日志将有助于得到答案。
Have you checked the logs? What is the error are you getting in the logs or developer tool console (firebug?)?. A detailed log would be helpful in getting answer.
但是,如果你遵循正确的方法,你所尝试的方法是绝对可能的。
However the approach you are trying is definitely possible, if you have followed the correct approach.
步骤1 :创建会话
void createSession(String Username)
{
getThreadLocalRequest().getSession().setAttribute("Username", Username);
}
第2步:正确设置Cookie
Step 2 : Set the cookie properly
String sessionID = /*(Get sessionID from server's response to your login request.)*/;
final long DURATION = 1000 * 60 * 60 * 24 * 14;
Date expires = new Date(System.currentTimeMillis() + DURATION);
Cookies.setCookie("sessionId", sessionID, expires, null, "/", false);
步骤3 :从您设置的Cookie验证用户会话,
Step 3 : Validate the user session from the cookie you have set and user credentials.
boolean sessionAndUserValidFlag = false;
String sessionID = Cookies.getCookie("sessionId");
if ( sessionID != null )
{
sessionAndUserValidFlag = validateSession(sessionID);
}
if (sessionAndUserValidFlag)
{
//Continue login
}
else
{
//Display Login Popup
}
您的validateSession方法可能类似于 / p>
Your validateSession method may look something like this
public boolean validateSession(String sessionId)
{
if (getThreadLocalRequest().getSession().getId.equals(sessionId))
{
if ( getThreadLocalRequest().getSession().getAttribute("UserName") != null )
{
return true;
}
}
return false;
}
这篇关于不能对多个GWT应用程序使用相同的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!