在spring中销毁另一个用户的会话
在我的应用程序中,我有一个可以删除用户的管理员。因此,当我从管理会话中删除用户时,我希望被删除的用户应该自动注销。 我知道我删除的用户的会话id,但我不知道如何使用会话id使会话无效 我想要的是:invalidate(SessionId) 有可能吗?我认为可以使用一个过滤器并在每次请求时检查数据库,但是否有另一种方法不需要在每次httprequest上检查数据库在spring中销毁另一个用户的会话,spring,session,spring-security,sessionid,Spring,Session,Spring Security,Sessionid,在我的应用程序中,我有一个可以删除用户的管理员。因此,当我从管理会话中删除用户时,我希望被删除的用户应该自动注销。 我知道我删除的用户的会话id,但我不知道如何使用会话id使会话无效 我想要的是:invalidate(SessionId) 有可能吗?我认为可以使用一个过滤器并在每次请求时检查数据库,但是否有另一种方法不需要在每次httprequest上检查数据库 谢谢D我想我看到了一个使用Spring安全基础架构的解决方案,该类 您必须在web.xml中注册HttpSessionEventPub
谢谢D我想我看到了一个使用Spring安全基础架构的解决方案,该类 您必须在
web.xml
中注册HttpSessionEventPublisher
:
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
org.springframework.security.web.session.HttpSessionEventPublisher
在Spring配置中,声明SessionRegistry
<bean id="sessionRegistry"
class="org.springframework.security.core.session.SessionRegistryImpl" />
在管理控制台中,您必须使用SessionRegistry
检索用户的SessionInformation
,并调用expireNow
。在用户的下一个请求中,servlet过滤器应该使HttpSession过期。的javadoc对其工作原理有一些解释
让我们知道这是否有帮助。//要结束用户的会话:
// to end a session of a user:
List<SessionInformation> sessions = sessionRegistryImpl.getAllSessions(user, false);
sessionRegistryImpl.getSessionInformation(sessions.get(0).getSessionId()).expireNow();
// note: you can get all users and their corresponding session Ids:
List<Object> users = sessionRegistryImpl.getAllPrincipals();
List<String> sessionIds = new ArrayList<>(users.size());
for (Object user: users) {
List<SessionInformation> sessions = sessionRegistryImpl.getAllSessions(user, false);
sessionIds.add(sessions.get(0).getSessionId());
}
List sessions=sessionRegistryImpl.getAllSessions(用户,false);
sessionRegistryImpl.getSessionInformation(sessions.get(0.getSessionId()).expireNow();
//注意:您可以获取所有用户及其对应的会话ID:
List users=sessionRegistryImpl.getAllPrinciples();
List sessionIds=newarraylist(users.size());
for(对象用户:用户){
List sessions=sessionRegistryImpl.getAllSessions(用户,false);
add(sessions.get(0.getSessionId());
}
除了@LaurentG的建议之外,还需要在spring配置文件中添加以下内容:
<session-management>
<concurrency-control session-registry-alias="sessionRegistry" />
</session-management>
让它工作。还可以使用@zygimantus answer访问会话数据 除了如上所述使用户过期外,您还可能希望将其从注册表中删除:
// expire and remove the user's sessions from Spring session registry
List<Object> principals = sessionRegistry.getAllPrincipals();
for (Object principal : principals) {
List<SessionInformation> sessions = sessionRegistry.getAllSessions(principal, true);
for (SessionInformation sessionInfo : sessions) {
if (authentication.getPrincipal().equals(sessionInfo.getPrincipal())) {
if (!sessionInfo.isExpired()) sessionInfo.expireNow();
sessionRegistry.removeSessionInformation(sessionInfo.getSessionId());
}
}
}
//过期并从Spring会话注册表中删除用户的会话
List principals=sessionRegistry.getAllPrincipals();
for(对象主体:主体){
List sessions=sessionRegistry.getAllSessions(主体,true);
用于(会话信息会话信息:会话){
if(authentication.getPrincipal().equals(sessionInfo.getPrincipal())){
如果(!sessionInfo.isExpired())sessionInfo.expireNow();
removeSessionInformation(sessionInfo.getSessionId());
}
}
}
如果使用xml配置连接您的Spring会话注册表,它可能如下所示:
<beans:bean id="sessionRegistry"
class="org.springframework.security.core.session.SessionRegistryImpl" />
我遇到问题,我注册了调用sessionRegistry.registerNewSession(String session,Object principal)的会话。当我删除用户时,我正确获取了会话,并在控制器中调用sessionInformation.expireSession(),但它不起作用。它仍在工作的用户会话我再次阅读了,我认为您还必须将
添加到Spring安全配置的
部分。我希望这会有所帮助。为什么不在答案中加上这个呢?我一定要像LaurentG写的那样吗?@Alex78191,我们的答案基本相同。在这两种情况下,您都需要调用expireNow
方法。我是否应该在web.xml
中注册HttpSessionEventPublishe
,并声明SessionRegistry
?记不清了。我使用的不是web.xml,而是java config。我相信您可能希望如上所述终止用户会话,但也要删除它们(removeSessionInformation(sessionid)。
与