gpt4 book ai didi

java - 从 Apache Shiro ServletContainerSessionManager 列出经过身份验证的用户

转载 作者:行者123 更新时间:2023-12-01 09:56:48 25 4
gpt4 key购买 nike

我想实现在 Apache Shiro 中列出登录用户的解决方案。我尝试了这段代码:

public Collection<Session> listAccounts() throws IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException
{
DefaultSecurityManager manager = (DefaultSecurityManager) SecurityUtils.getSecurityManager();
DefaultWebSessionManager sessionManager = (DefaultWebSessionManager) manager.getSessionManager();
// invoke "sessionManager.getActiveSessions()" via reflection:
Method getActiveSessionsMethod = DefaultSessionManager.class.getDeclaredMethod("getActiveSessions");
getActiveSessionsMethod.setAccessible(true);
Collection<Session> activeSessions = (Collection<Session>) getActiveSessionsMethod.invoke(sessionManager);

return activeSessions;
}

但是当我运行代码时,我收到此错误消息:

Caused by: java.lang.ClassCastException: org.apache.shiro.web.session.mgt.ServletContainerSessionManager cannot be cast to org.apache.shiro.web.session.mgt.DefaultWebSessionManager
at com.crm.web.authentication.ActiveAccounts.listAccounts(ActiveAccounts.java:22)

您能给我一些如何解决此问题的建议吗?

最佳答案

您的调用manager.getSessionManager()不会返回DefaultWebSessionManager,而是返回ServletContainerSessionManager

ServletContainerSessionManager 不拥有方法 getActiveSessions(),因此您必须以其他方式获取此信息 - 这真正导致了“”的真正问题你为什么首先想知道这个?'

关于java - 从 Apache Shiro ServletContainerSessionManager 列出经过身份验证的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37156637/

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