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

來源:互聯網
上載者:User

標籤:spring   springmvc   spring mvc   web開發   java分布式架構   shiro   

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

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

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-f0234f4d36258d9b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

2. 攔截器配置:

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-bf6a3b13b4532349.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

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

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

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-d87bf17a9e1cb63d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

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

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

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-9afde7c6681e4b6d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-7819e4c34a2ee2f1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

5. 定義RedisLogin對象,用來通過uid往redis進行userObject Storage Service:

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-a1ab77ecc0d61caa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-0eda71eba8ddb107.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

6. 編寫LoginInterceptor.java攔截器

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-8373c2d2a2ce74eb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-d8476325ad18274c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

7. 定義異常的LoginResponseCode

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-734bcb227979fd3a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

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

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-8fac33f6fbb34fc6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

9. 測試sso單點登入:

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-b09d0f196964ade4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

返回結果集:

650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/5712789-dc5525098b87e708.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" style="height:auto;vertical-align:middle;border:0px;" alt="1240" />

願意瞭解架構技術或者源碼的朋友直接求求交流分享技術:貳零四貳八四九貳三柒

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

更多詳細源碼參考來源



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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.