【问题标题】:Limit Maximum Concurrent Session per user限制每个用户的最大并发会话
【发布时间】:2021-05-04 12:49:25
【问题描述】:

您好团队我正在尝试设置每个用户的并发会话限制。我正在尝试使用会话管理来限制它,但它没有按预期工作。我尝试将最大会话数设置为 1,但我可以拥有超过 1 个活动会话。 请找配置

http.csrf().disable()

            // take all the default security headers
            .headers()

            .contentTypeOptions().and()

            .xssProtection().and()

            .cacheControl().disable().addHeaderWriter(new StaticAndDynamicCacheControlHeaderWriter(cacheTimeDays))

            .httpStrictTransportSecurity().and()

            // add X-Frame-Options: SAMEORIGIN
            .frameOptions().sameOrigin().and()

            // Un-authenticated calls
            .authorizeRequests()
            .antMatchers("/api")
            .permitAll()

            .antMatchers("/api")
            .access("authenticated or hasIpAddress('localhost')").and()

            .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).accessDeniedHandler(accessDeniedHandler).and()

            // Login configuration - the default action here is POST.
            .formLogin().permitAll().loginProcessingUrl(LOGIN_LOGOUT_URI).successHandler(authLoginSuccessHandler).failureHandler(authLoginFailureHandler)
            .and()

            // Logout configuration
            .logout().permitAll().logoutRequestMatcher(new AntPathRequestMatcher(LOGIN_LOGOUT_URI, "DELETE")).logoutSuccessHandler(authLogoutSuccessHandler)
            .and()

            
            .addFilter(getJsonUsernamePasswordAuthenticationFilter())

            .authorizeRequests().antMatchers("/api/**").authenticated().and()

            .authorizeRequests().anyRequest().permitAll().and()

            .sessionManagement().maximumSessions(1).maxSessionsPreventsLogin(true).sessionRegistry(sessionRegistry());

【问题讨论】:

    标签: java spring spring-security spring-session


    【解决方案1】:

    除了配置之外,您的应用程序似乎还有更多内容,因此如果您有一个可重复的小示例可能会有所帮助。但是在一个简单的应用程序中,我注意到除非您注册HttpSessionEventPublisher,否则会话管理不会完全起作用。将以下内容添加到您的@Configuration

            @Bean
            public HttpSessionEventPublisher httpSessionEventPublisher() {
                return new HttpSessionEventPublisher();
            }
    

    有关并发会话的更多信息,请参阅docs

    【讨论】:

    • 您能否发布一个可重现的小示例来说明该问题?此外,可能不相关,但您是否有理由在此配置中有多个 .authorizeRequests() 部分?我相信这些不会很好地结合在一起,所以我不确定这是否与您面临的问题有关。
    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 2015-08-11
    • 2011-01-07
    • 2015-05-18
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多