How to reload authorities on user update with Spring Security

前端 未结 6 1878
情话喂你
情话喂你 2020-12-04 08:44

I\'m doing an application with authentication by OpenID using Spring Security. When user is logged-in, some authorities are loaded in his session.

I have User with f

6条回答
  •  天涯浪人
    2020-12-04 09:43

    Thanks, help me a lot ! With SessionRegistry, I can use getAllPrincipals() to compare the user to modify with the current active users in sessions. If a session exist, I can invalidate his session using : expireNow() (from SessionInformation) to force re-authentication.

    But I don't understand the usefulness of securityContextPersistenceFilter ?

    EDIT :

    // user object = User currently updated
    // invalidate user session
    List loggedUsers = sessionRegistry.getAllPrincipals();
    for (Object principal : loggedUsers) {
        if(principal instanceof User) {
            final User loggedUser = (User) principal;
            if(user.getUsername().equals(loggedUser.getUsername())) {
                List sessionsInfo = sessionRegistry.getAllSessions(principal, false);
                if(null != sessionsInfo && sessionsInfo.size() > 0) {
                    for (SessionInformation sessionInformation : sessionsInfo) {
                        LOGGER.info("Exprire now :" + sessionInformation.getSessionId());
                        sessionInformation.expireNow();
                        sessionRegistry.removeSessionInformation(sessionInformation.getSessionId());
                        // User is not forced to re-logging
                    }
                }
            }
        }
    } 
    
        

    提交回复
    热议问题