Destroy a session of another user in spring

前端 未结 4 1443
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 16:45

In my application, I have an admin that can delete users. so when I delete a user from the admin session I want that the deleted user should get logged out automatically. I

4条回答
  •  被撕碎了的回忆
    2020-12-11 17:10

    In addition to expiring the user as described, you may also want to remove them from the registry:

    // expire and remove the user's sessions from Spring session registry
    List principals = sessionRegistry.getAllPrincipals();
    for (Object principal : principals) {
        List sessions = sessionRegistry.getAllSessions(principal, true);
        for (SessionInformation sessionInfo : sessions) {
            if (authentication.getPrincipal().equals(sessionInfo.getPrincipal())) {
               if (!sessionInfo.isExpired()) sessionInfo.expireNow();
               sessionRegistry.removeSessionInformation(sessionInfo.getSessionId());
            }
        }
    }
    
    
    

    And if using xml config to wire up your Spring Session Registry, it may look something like this:

    
    

    提交回复
    热议问题