spring+springmvc+Interceptor+jwt+redis實現sso單點登入

weixin_33670713發表於2017-10-31

在分散式環境中,如何支援PC、APP(ios、android)等多端的會話共享,這也是所有公司都需要的解決方案,用傳統的session方式來解決,我想已經out了,我們是否可以找一個通用的方案,比如用傳統cas來實現多系統之間的sso單點登入或使用oauth的第三方登入方案? 今天給大家簡單講解一下使用spring攔截器Interceptor機制、jwt認證方式、redis分散式快取實現sso單點登入,閒話少說,直接把步驟記錄下來分享給大家:分散式架構

1. 引入jwt的相關jar包,在專案pom.xml中引入:

spring+springmvc+Interceptor+jwt+redis實現sso單點登入

2. 攔截器配置:

spring+springmvc+Interceptor+jwt+redis實現sso單點登入

我這裡簡單配置了要攔截的url和過濾的url(這個根據自己專案來定)

3. 編寫jwt的加密或者解密工具類:

spring+springmvc+Interceptor+jwt+redis實現sso單點登入

這個加密工具類是我從網上找的,如果各位要修改,可以按照自己業務修改即可。

4. 建立Login.java物件,用來進行jwt的加密或者解密:

spring+springmvc+Interceptor+jwt+redis實現sso單點登入
spring+springmvc+Interceptor+jwt+redis實現sso單點登入

5. 定義RedisLogin物件,用來通過uid往redis進行user物件儲存:

spring+springmvc+Interceptor+jwt+redis實現sso單點登入
spring+springmvc+Interceptor+jwt+redis實現sso單點登入

6. 編寫LoginInterceptor.java攔截器

spring+springmvc+Interceptor+jwt+redis實現sso單點登入
spring+springmvc+Interceptor+jwt+redis實現sso單點登入

7. 定義異常的LoginResponseCode

spring+springmvc+Interceptor+jwt+redis實現sso單點登入

8. 編寫統一sso單點登入介面:

spring+springmvc+Interceptor+jwt+redis實現sso單點登入

9. 測試sso單點登入:

spring+springmvc+Interceptor+jwt+redis實現sso單點登入

返回結果集:

spring+springmvc+Interceptor+jwt+redis實現sso單點登入

願意瞭解框架技術或者原始碼的朋友直接求求交流分享技術:2042849237

分散式的一些解決方案,有願意瞭解的朋友可以找我們團隊探討


相關文章