繁体   English   中英

应用程序上的旧的新“删除/无效” Cookie

[英]Old new “delete/invalidade” Cookie on an application

大家好

我在这里已经阅读了很多有关使请求中的cookie无效并将其添加到响应中的问题(我知道不能将其物理删除)。

我已经读过这篇文章《 如何删除...饼干》 ,它与我的问题最接近。 而且,没有任何答案和建议对我的特殊情况有帮助(我认为)。

我正在计算机上对其进行测试,因此我的域是我的工作域。 我有一个配置了tomcat服务器的日食,另一个有了配置JBoss服务器的日食。

因此,我的情况是,我已经建立了一个单一登录系统,其中有一个域是单一登录系统(例如: myMachine.myCompany.com:8081/login : myMachine.myCompany.com:8081/login login myMachine.myCompany.com:8081/login命名为login ),并且我有一个系统使用此登录名,它将在另一个地址上,现在一切都在我的机器上,所以这个另一个系统( myMachine.myCompany.com:8080/system/something/index.jsf命名为app

我的实施工作流程是:

-来自jboss的用户访问应用
-转到一个安全过滤器,该过滤器检查对这个应用程序的请求中的所有内容(在web.xml中为/*
-安全过滤器检查用户是否具有cookie(jboss端)

Cookie[] cookies = request.getCookies();
if(cookies != null){ 
    for (Cookie cookie : cookies) {
        if (cookie.getName().equalsIgnoreCase("ssoSecurity")) {
            return cookie;
        }
    }
}

-如果用户没有cookie,则将其重定向到登录名 (tomcat)-当用户登录(输入要在数据库中检查的数据)时,我将为此用户创建一个带有令牌的会话,以供日后识别(当所需),tomcat服务器上的cookie:

Cookie cookie = new Cookie("ssoSecurity", token);
cookie.setDomain(domain); //.myCompany.com
cookie.setVersion(0);
cookie.setPath("/");
cookie.setSecure(request.isSecure());
cookie.setMaxAge(-1); //deleted when the browser close
response.addCookie( cookie );

-然后登录将用户重定向到应用程序 ,它将再次在securityFilter上-在securityFilter检查cookie并找到它之后,它将获取其值(令牌)并调用我的securityClient(这是我的类路径上的JAR,用于与之通信) tomcat通过json返回休息)来检查用户是否已登录(这是第一次在此服务器上创建用户会话)-然后它可以正常运行应用程序

我的问题是注销功能无法正常工作。 我会解释。

应用程序上 ,用户单击注销链接,该链接将调用( #{appUserBean.logout} )我的注销方法,该方法是:

HttpServletResponse response = (HttpServletResponse)FacesContext
              .getCurrentInstance().getExternalContext()
              .getResponse();
HttpServletRequest request = (HttpServletRequest)FacesContext
              .getCurrentInstance().getExternalContext()
              .getRequest();
destroyCookie(getCookie(request), response);
SSOClient client = new SSOClient(); //from the jar that I mentioned
client.logout(this.getUserSession().getToken(), request, response);

destroyCookie方法:

private void destroyCookie(Cookie cookie, HttpServletResponse response) {
    if (cookie != null) {
        response.setContentType("text/html");
        cookie.setPath("/");
        cookie.setValue("notValidSession");
        cookie.setComment("EXPIRING COOKIE at " + System.currentTimeMillis());
        cookie.setVersion(0);
        cookie.setDomain(""); //I've tried ".myCompany.com" wont work
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}

这行代码: client.logout(this.getUserSession().getToken(), request, response); 将令牌发送到登录应用程序以从登录服务器(tomcat)注销用户,然后将用户重定向到登录应用程序(tomcat)。

这种情况是cookie尚未失效,其值也没有改变。 因此,如果我不关闭浏览器并放置应用程序的链接,则cookie仍在其中,而用户令牌不是我设置的新值(“ notValidSession”),并且在tomcat服务器上,如果我检查request.getCookies(); 就像没有cookie一样,它为null,但是我可以在firefox或chrome资源上看到具有相同值(用户令牌)的cookie。

我只是在写这篇文章的时候意识到,我并没有从jboss方面使用户会话无效(我会这样做,而你们会建议一些您可能会发现的错误)。

提前致谢,

事实证明问题出在这行cookie.setDomain("");上的方法destroyCookiecookie.setDomain(""); 如我的评论所述,我尝试设置Cookie域,但是由于进行了大量更改,因此setDomain(".myCompany.com")的使用不起作用。

现在,在我修改了client.logout以清除jboss一侧的用户的会话信息并进行了修改后,在destroyCookie方法上设置了域后,它工作正常。 所以最后的修改是这样的:

private void destroyCookie(Cookie cookie, HttpServletResponse response) {
    if (cookie != null) {
        response.setContentType("text/html");
        cookie.setPath("/");
        cookie.setValue("notValidSession");
        cookie.setComment("EXPIRING COOKIE at " + System.currentTimeMillis());
        cookie.setVersion(0);
        //I've configured the domain on the custom-params-service.xml on JBoss
        cookie.setDomain( AppUtils.getJndiConfig("SSOCookieDomain") ); 
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}

以及在JBoss端(在我的客户端jar上)清除会话信息的方法:

private void cleanUserSession(HttpServletRequest _request) {
    HttpSession session = (HttpSession) _request.getSession(false);
    if ( session != null ){
        session.setAttribute(ClientUtil.AUTH_TOKEN, "" );
        session.setAttribute(ClientUtil.USUER_INFO, null);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM