1.先去查看SpringSecurity官網文檔 SpringSecurity 官網提供操作,此操作只能解決單機非redis的
2.再去查看springsSession官網的文檔。springsession官網 提供文檔
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());