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

后端 未结 7 2329
独厮守ぢ
独厮守ぢ 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:41

    Please correct me if I'm wrong too.

    I think @Adam's and @elysch`s answer is incomplete. I noticed that there are needed to add listener:

     servletContext.addListener(HttpSessionEventPublisher.class);
    

    to

    public class AppInitializer implements WebApplicationInitializer {
    
    @Override
    public void onStartup(ServletContext servletContext) {
      ...
    servletContext.addListener(HttpSessionEventPublisher.class);
    }
    

    with security conf:

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(final HttpSecurity http) throws Exception {
            // ...
            http.sessionManagement().maximumSessions(1).sessionRegistry(sessionRegistry());
        }
    
        @Bean
        public SessionRegistry sessionRegistry() {
            return new SessionRegistryImpl();
        }
    
        @Bean
        public HttpSessionEventPublisher httpSessionEventPublisher() {
            return new HttpSessionEventPublisher();
        }
    }
    

    And then you will get current online users!

提交回复
热议问题