1.先去查看SpringSecurity官網(wǎng)文檔 SpringSecurity 官網(wǎng)提供操作梅誓,此操作只能解決單機(jī)非redis的
2.再去查看springsSession官網(wǎng)的文檔腿堤。springsession官網(wǎng) 提供文檔
SessionRepository實(shí)現(xiàn)也可以選擇實(shí)現(xiàn)FindByIndexNameSessionRepository
FindByIndexNameSessionRepository提供一種方法胸遇,用于查找具有給定索引名稱和索引值的所有會(huì)話
FindByIndexNameSessionRepository實(shí)現(xiàn)時(shí),可以使用方便的方法查找特定用戶的所有會(huì)話
具體代碼實(shí)現(xiàn)
@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個(gè)用戶登錄該賬號,如果失敗跳轉(zhuǎn)登錄地址
說了這么多 其實(shí)也就這一句
http.sessionManagement().maximumSessions(1).expiredUrl("/login").sessionRegistry(sessionRegistry());