如何访问 tomcat 中的会话并终止其中之一

Mac*_*ski 5 java session tomcat http jakarta-ee

是否可以以编程方式终止 Tomcat 中其他用户的 HTTP 会话?我的用例是这样的:网站上有一些用户和一个管理员。当管理员删除特定用户时,如果他有活动会话,我希望他立即注销。可以通过应用程序代码访问用户会话吗?

Mac*_*ski 0

我在回答其他问题时找到了解决方案:

Tomcat:如何从servlet访问(会话)管理器

诀窍是使用反射Manager从 a HttpSessionor获取 a ServletContext。对我有用的代码:

private Manager manager(HttpSession session) throws Exception {

    Field facadeSessionField = StandardSessionFacade.class.getDeclaredField("session");
    facadeSessionField.setAccessible(true);
    StandardSession stdSession = (StandardSession) facadeSessionField.get(session);

    return stdSession.getManager();
}
Run Code Online (Sandbox Code Playgroud)