本篇介绍如何实现一个用户只允许同时在一个地点登录
基本配置
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().and()
.sessionManagement()
.maximumSessions(1).expiredUrl("/expired");
maximumSessions配置一个用户的最大session数量,默认不限,这里设1。先在一个浏览器如chrome登录,然后在另一个浏览器如firefox登录,此时同时登录数为2超出参数1,之前chrome的session将会过期,再去chrome发出请求会跳转到过期url。
防止再次登录
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().and()
.sessionManagement()
.maximumSessions(1).maxSessionsPreventsLogin(true);
此时不会踢出之前的登录者,而是先登录者建立了唯一session,在他注销或关闭浏览器之前,不允许异地再次登录。