SpringSecurity整合SpringSession-Redis 限制用戶登錄,SpringSecurity單用戶登錄


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());


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM