在春天销毁另一个用户的会话

Jos*_*ema 8 session spring spring-security sessionid

在我的应用程序中,我有一个可以删除用户的管理员.因此,当我从管理会话中删除用户时,我希望删除的用户应该自动注销.我知道我删除了用户的会话ID,但我不知道如何使用会话ID使会话无效.

我想要像:invalidate(SessionId);

有可能吗?我认为可以使用过滤器并根据请求检查每个数据库但是有另一种方法我不需要在每个httprequest上检查数据库吗?

谢谢.:d

Lau*_*ntG 6

我想我看到了一个使用Spring Security基础架构和SessionRegistry类的解决方案.

你必须注册HttpSessionEventPublisherweb.xml:

<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

在Spring配置中,声明SessionRegistry.

<bean id="sessionRegistry"
     class="org.springframework.security.core.session.SessionRegistryImpl" />
Run Code Online (Sandbox Code Playgroud)

在管理控制台中,您必须使用它SessionRegistry来检索SessionInformation用户和呼叫expireNow.在用户的下一个请求中,servlet过滤器应该使HttpSession过期.SessionInformation的javadoc 有一些关于它是如何工作的解释.

如果有帮助,请告诉我们.

  • 我再次阅读[docs](http://static.springsource.org/spring-security/site/docs/3.0.x/reference/session-mgmt.html),我想你还要添加`<session -management ...>`到Spring安全配置的`<http>`部分.我希望它会有所帮助. (2认同)

zyg*_*tus 5

// 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());
}
Run Code Online (Sandbox Code Playgroud)