How do you log out all logged in users in spring-security?

前端 未结 2 520
失恋的感觉
失恋的感觉 2020-12-29 05:46

I want to be able to log out all logged in users programmatically. How do you force logout all users on some event?

2条回答
  •  醉梦人生
    2020-12-29 06:06

    First define HttpSessionEventPublisher in web.xml

    
        org.springframework.security.web.session.HttpSessionEventPublisher
    
    

    Then define in your spring security.xml file.

    Now, use SessionRegistry in your controller method to invalidate all sessions. Below code retrieves all active sessions.

    List activeSessions = new ArrayList();
        for (Object principal : sessionRegistry.getAllPrincipals()) {
            for (SessionInformation session : sessionRegistry.getAllSessions(principal, false)) {
                activeSessions.add(session);
            }
        }
    

    On Each active session, you can call expireNow() method to expire or invalidate them.

提交回复
热议问题