Keycloak會話管理-refreshToken


Keycloak會話管理中,獲取到accessToken和refreshToken后,基於accessToken交換用戶數據或者參與KeycloakAPI的請求,當accessToken過期的時候,可使用refreshToken去交換新的accessToken和refreshToken。

我們可能會遇到這樣一個情況:當refreshToken在請求的時候也過期了,這個時候,需要回到登錄頁面。如果按照這樣的流程走,將帶來較差的體驗,需要用戶重新登錄,尤其在較多模塊中,這種方案極不可取。

我們先從正常的角度去請求token、基於refreshToken交換token、基於offlineToken交換token

基於Oauth2-password獲取token
curl
-X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=password&client_id=ROOMIS&username=123&password=123' "http://keycloak域名IP/auth/realms/{realms名字}/protocol/openid-connect/token" | jq

基於refreshToken獲取accessToken和refreshToken

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=refresh_token&client_id=ROOMIS&refresh_token={refreshToken}' "http://keycloak域名IP/auth/realms/{realms名字}/protocol/openid-connect/token" | jq

 請求后的結果如上圖所示。

  

備注
1
基於上述獲取的refreshToken是有失效期的,當剛剛好refreshToken失效的時候,將不再獲取accessToken,只能重新登錄。 2 官方的解決辦法是獲取離線token,請參照:https://www.keycloak.org/docs/3.2/server_admin/topics/sessions/offline.html

   解決方法是:在請求token的時候,假如scope=offline_access


免責聲明!

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



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