【问题标题】:Old new "delete/invalidade" Cookie on an application应用程序上的旧新“删除/无效”Cookie
【发布时间】:2014-02-07 05:28:11
【问题描述】:

大家好。

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

我读过这个How do you remove...Cookie,这是最接近我的问题的。在我的特殊情况下,没有任何答案和建议对我有帮助(我认为)。

我正在我的机器上测试它,所以我的域就是我的工作域。我有一个配置了 tomcat 服务器的 eclipse 和另一个配置了 JBoss 服务器的 eclipse。

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

我的实施工作流程是:

-用户从jboss
访问app - 它转到一个 securityFilter,检查对该应用程序的请求(web.xml 上的/*)中的所有内容
- 安全过滤器检查用户是否有 cookie(jboss 端)

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

-如果用户没有 cookie,我将他重定向到 login (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 );

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

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

应用程序上,用户点击注销链接,该链接将调用 (#{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); 会将令牌发送到 login 应用程序以从登录服务器 (tomcat) 注销用户,然后将用户重定向到登录应用程序 (tomcat)。

情况是cookie没有失效,它的值也没有改变。所以如果我不关闭浏览器并放置应用程序的链接cookie 仍然存在,用户令牌不是我设置的新值(“notValidSession”)并且在 tomcat 服务器上,如果我检查 request.getCookies(); 它是空的,好像没有 cookie,但我可以看到那里的 cookie firefox 或 chrome 资源并且具有相同的值(用户令牌)。

我在写这篇文章时才意识到,我不会在这种方法上使 jboss 端的用户会话无效(我会这样做,而你们会提出一些建议或任何你能找到的错误)。

提前致谢,

【问题讨论】:

    标签: java tomcat cookies jboss


    【解决方案1】:

    事实证明,问题出在destroyCookie 这一行上的方法cookie.setDomain(""); 上,正如我作为评论所说,我尝试设置我的cookie 域,但由于我做了很多更改,所以使用@987654323 @没用。

    现在,在我修改了我的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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 2020-06-02
      • 1970-01-01
      • 2014-08-10
      • 2013-01-08
      • 1970-01-01
      相关资源
      最近更新 更多