1.先去查看SpringSecurity官网文档 SpringSecurity 官网提供操作,此操作只能解决单机非redis的
https://docs.spring.io/spring-security/site/docs/5.2.1.RELEASE/reference/htmlsingle/#ns-concurrent-sessions

2.再去查看springsSession官网的文档。springsession官网 提供文档
https://docs.spring.io/spring-session/docs/2.2.2.RELEASE/reference/html5/#api-findbyindexnamesessionrepository
SessionRepository实现也可以选择实现FindByIndexNameSessionRepository
FindByIndexNameSessionRepository提供一种方法,用于查找具有给定索引名称和索引值的所有会话
FindByIndexNameSessionRepository实现时,可以使用方便的方法查找特定用户的所有会话

具体代码实现
@Configuration
@EnableWebSecurity
public class WebSecurityConfig<S extends Session> extends WebSecurityConfigurerAdapter {
@Autowired
private FindByIndexNameSessionRepository<S> sessionRepository;
@Override
protected void configure(HttpSecurity http) throws Exception {
//单用户登录
http.sessionManagement().maximumSessions(1).expiredUrl("/login").sessionRegistry(sessionRegistry());
}
@Bean
SpringSessionBackedSessionRegistry<S> sessionRegistry() {
return new SpringSessionBackedSessionRegistry<S>(sessionRepository);
}
}
spring session管理器 只允许1个用户登录该账号,如果失败跳转登录地址
说了这么多 实则也就这一句
http.sessionManagement().maximumSessions(1).expiredUrl("/login").sessionRegistry(sessionRegistry());
© 版权声明
文章版权归作者所有,未经允许请勿转载。如内容涉嫌侵权,请在本页底部进入<联系我们>进行举报投诉!
THE END





![[C++探索之旅] 第一部分第十一课:小练习,猜单词 - 鹿快](https://img.lukuai.com/blogimg/20251015/da217e2245754101b3d2ef80869e9de2.jpg)










暂无评论内容