How can I have list of all users logged in (via spring security) my web application

后端 未结 7 2328
独厮守ぢ
独厮守ぢ 2020-11-27 12:27

I\'m using spring security in my web application, and now I want to have a list of all users who are logged in my program.

How can I have access to that list? Aren\'

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 12:27

    You need to inject SessionRegistry (as mentioned eariler) and then you can do it in one pipeline like this:

    public List findAllLoggedInUsers() {
        return sessionRegistry.getAllPrincipals()
                .stream()
                .filter(principal -> principal instanceof UserDetails)
                .map(UserDetails.class::cast)
                .collect(Collectors.toList());
    }
    

提交回复
热议问题