Get all logged users in Spring Security

后端 未结 1 1034
小蘑菇
小蘑菇 2021-02-10 00:58

I would like to get list of all users which are currently logged in my application. I know, that I should inject SessionRegistry in my code to call getAllPrin

相关标签:
1条回答
  • 2021-02-10 01:52

    I think you are almost there. The only thing you've probably missed is the use of session-registry-alias. By using that attribute on the concurrency-control element you expose the session registry, so that it can be injected to your own beans. See the reference doc.

    So what you need is:

    <http auto-config="true">
    ...
        <session-management>
            <concurrency-control max-sessions="1" session-registry-alias="sessionRegistry"/>
        </session-management>
    </http>
    

    Now you have a reference to the session registry that will be populated by the ConcurrentSessionControlStrategy which is set up implicitly by the above configuration. To use it you would just inject it to your bean as normal:

    <bean class="YourOwnSessionRegistryAwareBean">
        <property sessionRegistry="sessionRegistry"/>
    </bean>
    

    Please note that the above configuration will also restrict the number of concurrent sessions a user may have. If you don't want this restriction, you will have to forgo the convenience of the namespace configuration, because the namespace schema doesn't allow you to set the max-sessions attribute to -1. If you need help on how to wire up the necessary beans manually, the reference doc gives detailed instructions on that.

    0 讨论(0)
提交回复
热议问题