如何在tomcat中访问会话并终止其中一个会话

时间:2014-07-22 18:37:39

标签: java http session tomcat java-ee

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

3 个答案:

答案 0 :(得分:4)

您需要维护userId->HttpSession的并发地图。用户登录时,将其放入地图中。会话为destroyed时,请将其从地图中删除。

现在给定userId,您可以从地图中检索会话,并invalidate

答案 1 :(得分:1)

是的,可以列出Tomcat中所有登录或工作用户会话,并终止任何一个会话的表单
(如果在安装时你有选择主机管理器,则可以使用user_name和密码)

> Type URI as http://localhost:8080/ 
    (8080 is default connector port of your tomcat if you had changed it then write it) 
> Select Manage App Option
> Enter user_name and password which you had entered at installation time 
> After log-in successfully Click on session (0 or 1,2,3...etc) in Sessions column see in first column which display the name of your project 
> Click on `Refresh Session List` Button 
> Select session Id check-box 
> Click on `Invalidate Selected Session` Button
   after click on this Button that session will be invalidate     

答案 2 :(得分:0)

我已经找到了解决其他问题的解决方案:

Tomcat: how to access (session) Manager from servlet

诀窍是使用反射从ManagerHttpSession获取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();
}