简体   繁体   English

Hazelcast +春季安全+分布式会话=如何使其工作?

[英]Hazelcast + spring security + distributed sessions = how to make it work?

Being inspired by http://docs.hazelcast.org/docs/latest/manual/html/websessionreplication.html I decided to give it a try and use in my Spring MVC + Security application. http://docs.hazelcast.org/docs/latest/manual/html/websessionreplication.html的启发,我决定尝试在Spring MVC + Security应用程序中使用它。

First issue I hit is - Hazelcast was complaining it can't find sessionRegistry bean. 我遇到的第一个问题是-Hazelcast抱怨无法找到sessionRegistry bean。 I solved it pretty quickly by adding following bean to spring-security context 我通过在spring-security上下文中添加以下bean很快解决了它

<bean id="sessionRegistry"
    class="org.springframework.security.core.session.SessionRegistryImpl" />

But I hit next issue right away and still can't solve it. 但是我马上遇到了下一个问题,但仍然无法解决。 This is what happens: 这是发生了什么:

  1. Start tomcat (assuming app will be started as well) 启动tomcat(假设应用也将启动)
  2. Login to site - ok 登录网站-确定
  3. Logout - ok ( CONCERN : logged out ok, JSESSIONID and REMEMBER_ME_TOKEN cookies are cleared, but hazelcast.sessionId cookie is NOT cleared) 注销- OK( 关注 :注销OK, JSESSIONIDREMEMBER_ME_TOKEN cookies被清除,但hazelcast.sessionId cookie 不会清零)
  4. Restart tomcat 重启tomcat
  5. Navigate to index page 导航到索引页面
  6. ERROR : Infinite redirects to invalidSessionStrategy happens 错误 :发生无限重定向到invalidSessionStrategy

After some debugging I found several facts: 经过一些调试后,我发现了一些事实:

  • it happens because inside SessionManagementFilter#doFilter check for request.isRequestedSessionIdValid() returns false 发生这种情况是因为在SessionManagementFilter#doFilter内部检查request.isRequestedSessionIdValid()返回false
  • JSESSIONID and hazelcast.sessionId are different (which is I assume by design) JSESSIONIDhazelcast.sessionId是不同的(我设计hazelcast.sessionId
  • looks like issue happens because of inconsistency between calls request.isRequestedSessionIdValid() and request.getSession() - looks like if former returns false , then latter suppose to create new session - which doesn't happen 似乎是由于调用request.isRequestedSessionIdValid()request.getSession()之间的不一致而发生问题-好像如果前者返回false ,则后者假设要创建新会话-不会发生

What I tried so far (and it didn't help): 到目前为止,我尝试了什么(但没有帮助):

  • manually clear hazelcast.sessionId cookie using buildin spring security logout handler (no success, coockie appears again with same value) 使用hazelcast.sessionId Spring安全性注销处理程序手动清除hazelcast.sessionId cookie(没有成功,coockie再次以相同的值出现)
  • workaround https://github.com/hazelcast/hazelcast/issues/3049 which is about sending HttpSessionDestroyedEvent on logout. 解决方法https://github.com/hazelcast/hazelcast/issues/3049 ,该方法是在注销时发送HttpSessionDestroyedEvent No effect was noticed 没有发现效果
  • tried ti use JSESSIONID as session id for hazelcast cookie name (in that case I can open ap only once, all subsequent requests result in infinite redirects to invalid-session ) 尝试使用JSESSIONID作为hazelcast cookie名称的会话ID(在这种情况下,我只能打开ap,所有后续请求都会导致无限次重定向到invalid-session

So... Apparently it's not that simple as stated in official Hazelcast docs. 所以...显然,这并非像Hazelcast官方文档中所述的那么简单。 Any ideas how to make it work? 有什么想法使它起作用吗?

So it looks like I found how to fix that infinite redirect to invalid session issue. 因此,看来我找到了解决该infinite redirect to invalid session问题的方法。 I'm not sure if it's shortcut or it's 100% right way to do so... 我不确定这是快捷方式还是100%正确的方式...

I created custom InvalidSessionStrategyImpl with following code: 我使用以下代码创建了自定义InvalidSessionStrategyImpl

@Override
public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    if (request.getSession(false) instanceof HazelcastHttpSession) {
        HazelcastHttpSession hazelCastSession = (HazelcastHttpSession) request.getSession(false);
        hazelCastSession.invalidate();
    } else {
        request.getSession();
    }
    redirectStrategy.sendRedirect(request, response, redirecctTo);
}

ps In case you curious - right after that I hit another issue: excessive CPU consumption. ps如果您好奇-之后我又遇到了另一个问题:CPU消耗过多。 Hazelcast eats 100% CPU. Hazelcast吃了100%的CPU。 Completely unacceptable issue (especially for cloud deployment like Jelastic where you pay by resource usage). 完全无法接受的问题(尤其是对于像Jelastic这样的云部署,您需要根据资源使用情况付费)。

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

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