To access the list of all registered users, you need to enter an instance of SessionRegistry on your bean.
@Autowired @Qualifier("sessionRegistry") private SessionRegistry sessionRegistry;
And then, using the tested SessionRegistry, you can access the list of all participants:
List<Object> principals = sessionRegistry.getAllPrincipals(); List<String> usersNamesList = new ArrayList<String>(); for (Object principal: principals) { if (principal instanceof User) { usersNamesList.add(((User) principal).getUsername()); } }
But before you enter the session registry, you need to define the session management part in spring -security.xml (see the "Session Management" section in Spring's security reference documentation ) and in the concurrency -control section you must set an alias for the session registry object ( session-registry-alias) with which you will enter it.
<security:http access-denied-page="/error403.jsp" use-expressions="true" auto-config="false"> <security:session-management session-fixation-protection="migrateSession" session-authentication-error-url="/login.jsp?authFailed=true"> <security:concurrency-control max-sessions="1" error-if-maximum-exceeded="true" expired-url="/login.html" session-registry-alias="sessionRegistry"/> </security:session-management> ... </security:http>
dimas Jun 30 2018-12-12T00: 00Z
source share