使用 Hazelcast 的 Shiro 会话集群,而不是在 Web 上

Shiro Session Clustering with Hazelcast, not on the Web

我写了一个直接部署的基于 Netty 的 REST 服务(即没有 Web/Application 服务器)。对于 authn、authz 和会话管理,我使用 Apache Shiro。现在我希望能够在不在我们的负载均衡器上使用粘性会话的情况下对该服务进行负载均衡;我正在尝试使用进程内 Hazelcast 实例进行设置。

到目前为止,我已经设法让我的服务的两个实例 运行 在两台不同的机器上创建了一个包含两个成员的 Hazelcast 集群;但即便如此,如果我在一台机器上进行身份验证,另一台机器仍然无法识别该会话。所以我想我的 shiro.ini and/or 我的 hazelcast.xml 配置文件可能有误。

我实际上是在使用此处 https://github.com/stormpath/shiro-hazelcast-web-sample/blob/master/src/main/java/com/stormpath/samples/shiro/hazelcast/cache/HazelcastCacheManager.java

中的 HazelCastCacheManager class

这是我的 hazelcast.xml(我们的设置不允许多播,我必须直接指定机器 IP):

<hazelcast>
  <properties>
    <property name="hazelcast.logging.type">slf4j</property>
  </properties>

  <map name="shiro-activeSessionsCache">
    <async-backup-count>1</async-backup-count>
    <time-to-live-seconds>600</time-to-live-seconds>
  </map>

  <network>
    <join>
      <multicast enabled="false"></multicast>
      <tcp-ip enabled="true">
        <member>x.x.x.x:8050</member> <!-- server A -->
        <member>x.x.x.x:8050</member> <!-- server B -->
      </tcp-ip>
    </join>
    <interfaces enabled="true">
        <interface>x.x.x.*</interface>
    </interfaces>
    <port port-count="20" auto-increment="false">8050</port>    
  </network>

</hazelcast>

还有我的一部分 shiro.ini

sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
sessionManager = org.apache.shiro.session.mgt.DefaultSessionManager

sessionDAO.activeSessionsCacheName = shiro-activeSessionsCache
securityManager.sessionManager.sessionDAO = $sessionDAO

cacheManager = HazelcastCacheManager

securityManager.cacheManager = $cacheManager

sessionManager.globalSessionTimeout = 600000
sessionValidationScheduler = org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler
sessionValidationScheduler.interval = 600000
sessionManager.sessionValidationScheduler = $sessionValidationScheduler

securityManager.sessionManager=$sessionManager

我错过了什么?我如何指示 Shiro 在所有 Hazelcast 实例之间共享会话?或者是我在 HazelcastCacheManager class 中的错误?

这是我启动两个服务时 Hazelcast 生成的日志:

这是启动的第一个服务:

log4j:WARN No appenders could be found for logger (org.apache.commons.beanutils.converters.BooleanConverter).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Nov 24, 2015 9:03:42 AM com.hazelcast.config.XmlConfigLocator
INFO: Loading 'hazelcast.xml' from working directory.
[main] INFO com.hazelcast.instance.DefaultAddressPicker - [LOCAL] [dev] [3.5.3] Interfaces is enabled, trying to pick one address matching to one of: [x.x.x.*]
[main] INFO com.hazelcast.instance.DefaultAddressPicker - [LOCAL] [dev] [3.5.3] Prefer IPv4 stack is true.
[main] INFO com.hazelcast.instance.DefaultAddressPicker - [LOCAL] [dev] [3.5.3] Picked Address[x.x.x.x]:8050, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=8050], bind any local is true
[main] INFO com.hazelcast.spi.OperationService - [x.x.x.x]:8050 [dev] [3.5.3] Backpressure is disabled
[main] INFO com.hazelcast.spi.impl.operationexecutor.classic.ClassicOperationExecutor - [x.x.x.x]:8050 [dev] [3.5.3] Starting with 16 generic operation threads and 32 partition operation threads.
[main] INFO com.hazelcast.system - [x.x.x.x]:8050 [dev] [3.5.3] Hazelcast 3.5.3 (20151011 - 64c663a) starting at Address[x.x.x.x]:8050
[main] INFO com.hazelcast.system - [x.x.x.x]:8050 [dev] [3.5.3] Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
[main] INFO com.hazelcast.instance.Node - [x.x.x.x]:8050 [dev] [3.5.3] Creating TcpIpJoiner
[main] INFO com.hazelcast.core.LifecycleService - [x.x.x.x]:8050 [dev] [3.5.3] Address[x.x.x.x]:8050 is STARTING
[cached1] INFO com.hazelcast.nio.tcp.SocketConnector - [x.x.x.x]:8050 [dev] [3.5.3] Connecting to /x.x.x.x:8050, timeout: 0, bind-any: true
[cached1] INFO com.hazelcast.nio.tcp.SocketConnector - [x.x.x.x]:8050 [dev] [3.5.3] Could not connect to: /x.x.x.x:8050. Reason: SocketException[Connection refused to address /x.x.x.x:8050]
[cached1] INFO com.hazelcast.cluster.impl.TcpIpJoiner - [x.x.x.x]:8050 [dev] [3.5.3] Address[x.x.x.x]:8050 is added to the blacklist.
[main] INFO com.hazelcast.cluster.impl.TcpIpJoiner - [x.x.x.x]:8050 [dev] [3.5.3]

Members [1] {
        Member [x.x.x.x]:8050 this
}

这是第二个服务启动时的延续:

[main] INFO com.hazelcast.core.LifecycleService - [x.x.x.x]:8050 [dev] [3.5.3] Address[x.x.x.x]:8050 is STARTED
[main] INFO org.apache.shiro.config.IniSecurityManagerFactory - Realms have been explicitly set on the SecurityManager instance - auto-setting of realms will not occur.
Open your web browser and navigate to https://127.0.0.1:8113/
[hz._hzInstance_1_dev.IO.thread-Acceptor] INFO com.hazelcast.nio.tcp.SocketAcceptor - [x.x.x.x]:8050 [dev] [3.5.3] Accepting socket connection from /x.x.x.x:35316
[cached2] INFO com.hazelcast.nio.tcp.TcpIpConnectionManager - [x.x.x.x]:8050 [dev] [3.5.3] Established socket connection between /x.x.x.x:8050
[hz._hzInstance_1_dev.generic-operation.thread-10] INFO com.hazelcast.cluster.ClusterService - [x.x.x.x]:8050 [dev] [3.5.3]

Members [2] {
        Member [x.x.x.x]:8050 this
        Member [x.x.x.x]:8050
}

这是启动的第二个服务,第一个服务已经启动 运行:

log4j:WARN No appenders could be found for logger (org.apache.commons.beanutils.converters.BooleanConverter).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Nov 24, 2015 9:04:16 AM com.hazelcast.config.XmlConfigLocator
INFO: Loading 'hazelcast.xml' from working directory.
[main] INFO com.hazelcast.instance.DefaultAddressPicker - [LOCAL] [dev] [3.5.3] Interfaces is enabled, trying to pick one address matching to one of: [x.x.x.*]
[main] INFO com.hazelcast.instance.DefaultAddressPicker - [LOCAL] [dev] [3.5.3] Prefer IPv4 stack is true.
[main] INFO com.hazelcast.instance.DefaultAddressPicker - [LOCAL] [dev] [3.5.3] Picked Address[x.x.x.x]:8050, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=8050], bind any local is true
[main] INFO com.hazelcast.spi.OperationService - [x.x.x.x]:8050 [dev] [3.5.3] Backpressure is disabled
[main] INFO com.hazelcast.spi.impl.operationexecutor.classic.ClassicOperationExecutor - [x.x.x.x]:8050 [dev] [3.5.3] Starting with 8 generic operation threads and 16 partition operation threads.
[main] INFO com.hazelcast.system - [x.x.x.x]:8050 [dev] [3.5.3] Hazelcast 3.5.3 (20151011 - 64c663a) starting at Address[x.x.x.x]:8050
[main] INFO com.hazelcast.system - [x.x.x.x]:8050 [dev] [3.5.3] Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
[main] INFO com.hazelcast.instance.Node - [x.x.x.x]:8050 [dev] [3.5.3] Creating TcpIpJoiner
[main] INFO com.hazelcast.core.LifecycleService - [x.x.x.x]:8050 [dev] [3.5.3] Address[x.x.x.x]:8050 is STARTING
[cached1] INFO com.hazelcast.nio.tcp.SocketConnector - [x.x.x.x]:8050 [dev] [3.5.3] Connecting to /x.x.x.x:8050, timeout: 0, bind-any: true
[cached1] INFO com.hazelcast.nio.tcp.TcpIpConnectionManager - [x.x.x.x]:8050 [dev] [3.5.3] Established socket connection between /x.x.x.x:35316
[hz._hzInstance_1_dev.generic-operation.thread-3] INFO com.hazelcast.cluster.ClusterService - [x.x.x.x]:8050 [dev] [3.5.3]

Members [2] {
        Member [x.x.x.x]:8050
        Member [x.x.x.x]:8050 this
}

[main] INFO com.hazelcast.core.LifecycleService - [x.x.x.x]:8050 [dev] [3.5.3] Address[x.x.x.x]:8050 is STARTED
[main] INFO org.apache.shiro.config.IniSecurityManagerFactory - Realms have been explicitly set on the SecurityManager instance - auto-setting of realms will not occur.
Open your web browser and navigate to https://127.0.0.1:8113/

我认为我的错误是在我实际的 Java 代码中,而不是在配置文件中:

在我所有的初始测试中,运行 单个服务器上的服务,我用它来获取当前的 Shiro 用户:

Subject currentUser = SecurityUtils.getSubject();

但是这样产生的session key对于同一个用户在一台服务器上是一样的,但是对于同一个用户在不同的服务器上是不同的;会话密钥用于标识缓存中的会话。我最初希望能够不必将会话密钥与 REST 调用一起传递。但是如果我显式传递会话密钥然后使用它来获取当前用户,那么会话可以在不同的服务器中成功识别:

Subject currentUser = null;
if (parameters.containsKey("session")) {
    sId = parameters.get("session");
    currentUser = new Subject.Builder().sessionId(sId).buildSubject();
} else {
    currentUser = SecurityUtils.getSubject();
}

(在到达此代码段之前,我正在使用 Map 存储所有 URL 参数)

后续问题:是否可以在没有 cookie 的情况下共享 Shiro 会话,或者以其他方式从客户端显式地将会话密钥传递给服务器?