Springboot2.0 + OAuth2.0之授權碼模式


綜述:上兩回講了密碼模式,應該是淺顯易懂。如有補充,不足或者是有誤之處還請多多指出。現在重頭戲,授權碼模式開始。

一、具體操作流程

- 用戶訪問客戶端,后者將前者導向認證服務器,認證服務器返回認證頁面(賬號密碼或者其他認證方式)

- 用戶選擇是否給予客戶端授權。

- 假設用戶給予授權,認證服務器將用戶導向客戶端事先指定的"重定向URI"(redirection URI),同時附上一個授權碼。

- 客戶端收到授權碼,附上早先的"重定向URI",向認證服務器申請令牌。這一步是在客戶端的后台的服務器上完成的,對用戶不可見。

- 認證服務器核對了授權碼和重定向URI,確認無誤后,向客戶端發送訪問令牌(access token)和更新令牌(refresh token)。

二、實踐測試

同樣是使用上回使用的認證服務器。記得將資源配置注釋掉

(1)在瀏覽器訪問認證服務器獲取 code:http://localhost:8001/oauth/authorize?client_id=client&response_type=code&scope=all&redirect_uri=http://www.baidu.com (get), 結果如下:

認證服務器會重定向到 login 頁面讓用戶進行登錄授權。此時用戶輸入用戶名和密碼進行授權。成功之后認證服務器重定向到指定的 url (這里為 www.baidu.com):

(2)在重定向的 url 后面會帶上 code,此時客戶端則可以拿這個 code 去換取 token,換取連接:http://localhost:8001/oauth/token?client_id=client&client_secret=123456&grant_type=authorization_code&redirect_uri=http://www.baidu.com&code=AKAQUe (post)

 

(3)跟上回說的那樣,拿到 token 即可訪問資源了


 

問題總結:

(1)Java.io.NotSerializableException Problem (UserDetails的實現類沒有實現Serializable接口導致  )

(2)User account is locked   (isAccountNonLocked 屬性設置為了false,應該設置為true)

(3)解決Spring Security OAuth在訪問/oauth/token時候報401 authentication is required(解決參考:https://blog.csdn.net/u012040869/article/details/80140515)


 

參考資料:

(1)四中模式概述:https://blog.csdn.net/weixin_42033269/article/details/80086422

(2)userDetailsService:https://www.journaldev.com/2736/spring-security-example-userdetailsservice

(3)spring security Oauth 2.0 authentication server example: https://javadeveloperzone.com/spring-boot/spring-security-oauth-2-0-authentication-server/#26_Demo

(4)OAuth2 tables: https://stackoverflow.com/questions/34170281/spring-boot-oauth2-with-jdbc-token-store-gives-oauth-access-token-relation-doesn

(5)授權碼模式:https://blog.csdn.net/qq_27828675/article/details/82466599

(6)Spring Boot OAuth2.0密碼模式:https://blog.csdn.net/qq_34873338/article/details/80218212


 

后續:由此史上最簡潔Springboot2.0 + OAuth2.0 暫時告一段落。。。

 


免責聲明!

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



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