ServletContainerSessionManager

ServletContainerSessionManager

我想实现在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(),因此您将不得不以另一种方式获取此信息-真正引出了一个真正的问题:“为什么首先要知道这一点?”

07-24 09:24