首页 > 解决方案 > 并发会话管理不起作用。我已按照文档进行操作,但我认为我遗漏了一些小东西

问题描述

这是我的安全配置

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/assets/**", "/register/**", "/","/login**")
            .permitAll().antMatchers("/profile/**").hasAuthority("ROLE_1").anyRequest().authenticated()
            .antMatchers("/actuator/**").hasAuthority("ROLE_2").anyRequest().authenticated()
            .and().formLogin().loginPage("/login").permitAll()
            .and().sessionManagement().maximumSessions(1).maxSessionsPreventsLogin(true).expiredUrl("/login?expired")
            .and().and().logout().deleteCookies("JSESSIONID").invalidateHttpSession(true)           
            .and().csrf().disable();
    // .failureUrl("/fail");
}

这是将 HttpSessionEventPublisher 添加到应用程序上下文中

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

请让我知道我错过了什么。我仍然可以使用相同的凭据从两个浏览器登录。

标签: javaspring-bootsessionspring-securityconcurrency

解决方案


我找到了解决方案。它是覆盖我的 User 类的 equals 和 hashCode 方法,该类基于以下解决方案实现 UserDetails。已验证主体的 UserDetails 对象的比较是控制/测量会话并发的方式

http://forum.spring.io/forum/spring-projects/security/99166-maximum-sessions-1-does-not-work

Spring Security maxSession 不起作用


推荐阅读