2016-07-05 140 views
0

春季安全:
我想用所谓的牵引方式注销:发生了会话超时或注销自身用户...
反正在这些方面,在HttpSessionEventPublisherSessionRegistry称为destroyedSession时从sessionIds列表中删除SessionInformation ...


当我使用下面的方法强制注销特定用户时,此方法仅在“过期”SessionInformationSessionRegistry。现在,当我从SessionRegistry获得所有在线用户“getAllPrincipals()”时,会话过期的用户就在列表中!注销特定会话ID

@Override 
public boolean forceLogOut(int userId){ 
    for (Object username: sessionRegistry.getAllPrincipals()) { 
     User temp = (User) username; 
     if(temp.getId().equals(userId)){ 
      for (SessionInformation session : sessionRegistry.getAllSessions(username, false)) { 
       session.expireNow(); 
      } 
     } 
    } 
    return true; 
} 

我怎么能注销“特定用户”或“的sessionId”该会话对象从“Web服务器”和“会议注册表”中删除?
我在网上搜索,发现HttpSessionContext在Servlet API中可以从特定sessionId获取HttpSession。然后使会话无效。但我认为这种方法并不完全有用!
(注意,这个类已弃用!)

什么是最好的方法?我是否错了?

+0

sessionRegistry.getSessionInformation(的sessionId).expireNow(); Spring将标记此会话过期。来自此会话的任何进一步请求都会调用注销。 –

+0

这是真的,但是如果用户不发送任何请求,并且会话超时最大不超过1小时(超时不是超时),那么当使用'getAllPrincipals()'时,此方法返回所有原则,包括在线会话和到期会话...这是我的问题...! –

回答

0

尝试这样的:

@RequestMapping(value="/logout", method = RequestMethod.GET) 
    public String logoutPage (HttpServletRequest request, HttpServletResponse response){ 
     Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
     if (auth != null){  
      //new SecurityContextLogoutHandler().logout(request, response, auth); 
      persistentTokenBasedRememberMeServices.logout(request, response, auth); 
      SecurityContextHolder.getContext().setAuthentication(null); 
     } 
     return "redirect:/login?logout"; 
    } 

要注销特定的会话ID检查链接: how to log a user out programmatically using spring security

+0

感谢您的回复...我想强制注销一个用户与另一个用户,然后立即从'SessionRegistry'和我的Web服务器中删除用户的会话对象..我认为这种方法注销用户与自己。 –

+0

是的,你是对的,它自己注销用户.. – FuSsA

+0

你有一个想法,我该怎么办?我想管理在线用户和强制注销后一个用户看到在线用户。当我使用'getAllPrincipals()',过期的会话在列表中...! –