gpt4 book ai didi

java - Shiro session 集群与 Hazelcast,而不是在 Web 上

转载 作者:太空宇宙 更新时间:2023-11-04 13:11:54 25 4
gpt4 key购买 nike

我编写了一个基于 Netty 的 REST 服务,该服务直接部署(即没有 Web/应用程序服务器)。对于 authn、authz 和 session 管理,我使用 Apache Shiro。现在我希望能够负载平衡此服务,而无需在负载平衡器上使用粘性 session ;我正在尝试使用进程内 Hazelcast 实例进行设置。

到目前为止,我已经成功让我的服务的两个实例在两台不同的计算机上运行,​​创建了一个具有两个成员的 Hazelcast 集群;但即便如此,如果我在一台机器上进行身份验证,另一台机器仍然无法识别该 session 。所以我认为我可能在 shiro.ini 和/或 hazelcast.xml 配置文件中犯了一个错误。

我本质上是使用这里的 HazelCastCacheManager 类 https://github.com/stormpath/shiro-hazelcast-web-sample/blob/master/src/main/java/com/stormpath/samples/shiro/hazelcast/cache/HazelcastCacheManager.java

这是我的 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 实例之间共享 session ?或者是我在 HazelcastCacheManager 类中的错误?

这是我启动两个服务时 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 对于一台服务器上的同一用户是相同的,但对于不同服务器上的同一用户来说是不同的; session key 用于标识缓存中的 session 。我最初希望能够不必在 REST 调用中传递 session key 。但是如果我显式传递 session key ,然后使用它来获取当前用户,那么 session 可以在不同的服务器中成功识别:

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 session ,或者以其他方式从客户端显式地将 session key 传递到服务器?

关于java - Shiro session 集群与 Hazelcast,而不是在 Web 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33875435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com