[英]Shiro Session Clustering with Hazelcast, not on the Web
I have written a Netty-based REST service which is deployed directly (ie without a Web/Application server). 我已经编写了基于Netty的REST服务,该服务可以直接部署(即无需Web /应用程序服务器)。 For authn, authz and session management I am using Apache Shiro.
对于authn,authz和会话管理,我使用Apache Shiro。 Now I want to be able to load-balance this service without using sticky sessions on our load-balancer;
现在,我希望能够在不使用我们的负载均衡器上的粘性会话的情况下对该服务进行负载均衡; and I am trying to set that up using in-process Hazelcast instances.
我正在尝试使用进程内Hazelcast实例进行设置。
So far I have managed to have two instances of my service running on two different machines create a Hazelcast cluster with two members; 到目前为止,我已经设法在两个不同的计算机上运行我的服务的两个实例,以创建一个由两个成员组成的Hazelcast集群; but even so, if I authenticate on one machine, the other still doesn't recognize the session.
但是即使这样,如果我在一台计算机上进行身份验证,另一台仍然无法识别该会话。 So I think I have probably made a mistake in my shiro.ini and/or my hazelcast.xml configuration files.
因此,我认为我的shiro.ini和/或hazelcast.xml配置文件可能犯了一个错误。
I am essentially using the HazelCastCacheManager class from here https://github.com/stormpath/shiro-hazelcast-web-sample/blob/master/src/main/java/com/stormpath/samples/shiro/hazelcast/cache/HazelcastCacheManager.java 我本质上是在这里使用HazelCastCacheManager类https://github.com/stormpath/shiro-hazelcast-web-sample/blob/master/src/main/java/com/stormpath/samples/shiro/hazelcast/cache/HazelcastCacheManager .java
Here is my hazelcast.xml (our setup doesn't allow multicast, I have to specify the machine IPs directly): 这是我的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>
And a portion of my shiro.ini 还有我的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
What am I missing? 我想念什么? How can I instruct Shiro to share the session among all Hazelcast instances?
如何指示Shiro在所有Hazelcast实例之间共享会话? Or is my mistake in the HazelcastCacheManager class?
还是我在HazelcastCacheManager类中犯了错误?
Here is the log Hazelcast produces when I start up my two services: 这是Hazelcast在启动两项服务时产生的日志:
This is the first service starting up: 这是第一个启动的服务:
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
}
And this is the continuation when the second service starts: 这是第二项服务启动时的延续:
[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
}
This is the second service starting up, with the first one already running: 这是第二项服务启动,其中第一项服务已在运行:
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/
I think my error was in my actual Java code, not in the configuration files: 我认为我的错误出在我的实际Java代码中,而不是在配置文件中:
In all my initial tests, running the service on a single server, I used this to get the current Shiro user: 在所有最初的测试中,在单个服务器上运行服务,我用它来获取当前的Shiro用户:
Subject currentUser = SecurityUtils.getSubject();
However, the session key produced by this is the same for the same user on one server, but different for the same user on different servers; 但是,由此产生的会话密钥对于一台服务器上的同一用户是相同的,但对于不同服务器上的同一用户是不同的。 and the session key is used to identify a session in the cache.
会话密钥用于标识缓存中的会话。 I was originally hoping to be able not having to pass a session key along with a REST call.
我最初希望能够不必与REST调用一起传递会话密钥。 but if I explicitly pass the session key and then use this to get the current user, then sessions can successfully be recognized in different servers:
但是,如果我显式传递会话密钥,然后使用它来获取当前用户,则可以在不同服务器中成功识别会话:
Subject currentUser = null;
if (parameters.containsKey("session")) {
sId = parameters.get("session");
currentUser = new Subject.Builder().sessionId(sId).buildSubject();
} else {
currentUser = SecurityUtils.getSubject();
}
(I am using a Map to store all URL parameters before reaching this code snippet) (在到达此代码段之前,我正在使用地图存储所有URL参数)
Follow-up question: is is possible to share Shiro sessions without cookies or explicitly passing the session key in other ways from the client to the server? 后续问题:是否可以在不使用cookie的情况下共享Shiro会话,也可以通过其他方式将会话密钥从客户端显式传递到服务器?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.