I want to retrieve the currently logged in users by leveraging spring's SessionRegistry
.
I have defined the bean and applied it to the session management as shown below:
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.sessionManagement().maximumSessions(1).sessionRegistry(sessionRegistry());
}
@Bean
public SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}
Then I've created the method to get all users:
@Autowired
private SessionRegistry sessionRegistry;
@Override
public List<String> getUsersFromSessionRegistry() {
return sessionRegistry.getAllPrincipals().stream()
.filter(u -> !sessionRegistry.getAllSessions(u, false).isEmpty())
.map(Object::toString)
.collect(Collectors.toList());
}
I added a method in the controller but it returns always an empty list
@GetMapping("/loggedUsersFromSessionRegistry")
public List<String> getLoggedUsersFromSessionRegistry() {
return userService.getUsersFromSessionRegistry();
}