java - 应用程序上的旧新 "delete/invalidade"Cookie

标签 java tomcat cookies jboss

大家好。

我在这里阅读了很多关于使请求中的 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,它会检查对该应用程序的请求(/* on 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) - 当用户登录时(输入要在数据库中检查的数据),我使用 token 创建一个 session 给这个用户,然后稍后(需要时)在 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 并找到它之后,它获取它的值( token )并调用我的 securityClient(这是我的类路径上的一个 JAR,通过 rest 与 json 返回与 tomcat 通信)来检查如果用户已登录(只是第一次在此服务器上创建用户 session ) - 然后它正常运行应用程序

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

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

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

我只是在写这篇文章时意识到,我并没有使 jboss 端的用户 session 无效(我会这样做,而你们会善意地提出一些建议或您能发现的任何错误)。

提前致谢

最佳答案

事实证明,问题出在 cookie.setDomain(""); 这一行的方法 destroyCookie 正如我在评论中所说,我已经尝试设置我的 cookie 域,但是因为我做了很多更改,所以 setDomain(".myCompany.com") 的使用不起作用。

现在,在我修改我的 client.logout 以清除 jboss 端用户的 session 信息并在 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 上)清理 session 信息的方法:

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);
    }
}

关于java - 应用程序上的旧新 "delete/invalidade"Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21193428/

相关文章:

java - 如何在 Maven 中排除特定的单元测试

java - 停止循环 GIF 图标

node.js - Csurf 无效的 csrf token Express/nodejs

javascript - 访问 cookie,希望在 JavaScript 中

java - 使用 Spring JDBCTemplate 设置 Tomcat JDBC 连接池

php - 如何在Codeigniter中设置session.cookie值httponly

java - 为什么 toHexString 的长度可变?

Java map 内容对比

tomcat - 配置 Tomcat 以记录启动时加载的所有 Jar 文件和/或类

tomcat - 如何在Tomcat中部署AAR