如何通过Java类获取Tomcat会话ID

问题描述 投票:0回答:2

如何使用 Java 类检索 Tomcat 会话 ID?

我需要以编程方式访问 Tomcat 中活动会话的会话 ID。我尝试过使用各种方法,包括实现 HttpSessionListener,但没有成功。在 Tomcat 环境中获取 Java 类中的会话 ID 的正确方法是什么?

如何使用 Java 类以编程方式终止会话?

一旦获得会话 ID,我希望能够以编程方式终止特定会话。我尝试过使用 HttpSession.invalidate() 方法,但我不确定识别和终止特定会话的正确方法。如何通过 Tomcat 服务器中运行的 Java 类中的 ID 可靠地终止会话?

java class session tomcat tomcat9
2个回答
0
投票
    public class SessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        ServletContext context = session.getServletContext();
        Map<String, HttpSession> activeSessions = (Map<String, HttpSession>) context.getAttribute("activeSessions");
        if (activeSessions == null) {
            activeSessions = new ConcurrentHashMap<>();
            context.setAttribute("activeSessions", activeSessions);
        }
        activeSessions.put(session.getId(), session);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        ServletContext context = session.getServletContext();
        Map<String, HttpSession> activeSessions = (Map<String, HttpSession>) context.getAttribute("activeSessions");
        if (activeSessions != null) {
            activeSessions.remove(session.getId());
        }
    }
 }

要访问活动会话,您可以从“ServletContext”中检索“activeSessions”属性。

示例:

ServletContext context = request.getSession().getServletContext();
Map<String, HttpSession> activeSessions = (Map<String, HttpSession>) context.getAttribute("activeSessions");
if (activeSessions != null) {
    for (Map.Entry<String, HttpSession> entry : activeSessions.entrySet()) {
        System.out.println("Session ID: " + entry.getKey());
    }
}


假设您有会话 ID 和活动会话映射,您可以像这样使会话无效:

String sessionIdToTerminate = "someSessionId";
HttpSession sessionToTerminate = activeSessions.get(sessionIdToTerminate);
if (sessionToTerminate != null) {
    sessionToTerminate.invalidate();
    activeSessions.remove(sessionIdToTerminate); // Ensure to remove from the active sessions list
}

记住配置你的'web.xml'来注册'SessionListener'

<listener>
    <listener-class>example.package.SessionListener</listener-class>
</listener>


0
投票

包 zaberp.zab;

导入 javax.servlet.http.*;

导入java.util.concurrent.ConcurrentHashMap;

导入 javax.servlet.*;

公共类 SessionListener {

public static void invalidateAllSessions(ServletContext context) {
    // Get all active sessions
    HttpSession[] sessions = getSessions(context);
    
    // Invalidate each session
    for (HttpSession session : sessions) {
        session.invalidate();
    }
}

private static HttpSession[] getSessions(ServletContext context) {
    // Get the session collection from the servlet context
    Object sessionCollection = context.getAttribute("activeSessions");
    if (sessionCollection instanceof ConcurrentHashMap) {
        ConcurrentHashMap<String, HttpSession> activeSessions = (ConcurrentHashMap<String, HttpSession>) sessionCollection;
        // Convert the collection to an array of HttpSession
        return activeSessions.values().toArray(new HttpSession[0]);
    } else {
        return new HttpSession[0];
    }
}

}

当我运行此代码时,出现错误

11:23:22.967 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - 缓存 http://localhost:8080 的“基本”身份验证方案 无法使会话无效。状态代码:405 11:23:22.974 [main] 调试 o.a.h.i.c.PoolingHttpClientConnectionManager - 连接管理器正在关闭 11:23:22.974 [main] 调试 o.a.h.i.c.DefaultManagedHttpClientConnection - http-outgoing-0:关闭连接 11:23:22.974 [main] 调试 o.a.h.i.c.PoolingHttpClientConnectionManager - 连接管理器关闭

如何解决此代码或问题

© www.soinside.com 2019 - 2024. All rights reserved.