Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼
本文主要介紹了Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼,分享給大家,具體如下:
背景說(shuō)明
一個(gè)賬號(hào)只能一處登錄,類(lèi)似的業(yè)務(wù)需求在現(xiàn)有后管類(lèi)系統(tǒng)是非常常見(jiàn)的。 但在原有的 spring security oauth2 令牌方法流程(所謂的登錄)無(wú)法滿足類(lèi)似的需求。
我們先來(lái)看 TokenEndpoint
的方法流程
客戶(hù)端 帶參訪問(wèn) /oauth/token 接口,最后去調(diào)用 TokenGranter
TokenGranter
根據(jù)不同的授權(quán)類(lèi)型,獲取用戶(hù)認(rèn)證信息 并去調(diào)用TokenServices
生成令牌
重新 TokenService
重寫(xiě)發(fā)放邏輯createAccessToken
,當(dāng)用戶(hù)管理的令牌存在時(shí)則刪除重新創(chuàng)建,這樣會(huì)導(dǎo)致之前登陸獲取的token 失效,順理成章的被擠掉。
@Transactional public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException { OAuth2AccessToken existingAccessToken = tokenStore.getAccessToken(authentication); OAuth2RefreshToken refreshToken = null; // 重寫(xiě)此處,當(dāng)用戶(hù)關(guān)聯(lián)的token 存在時(shí),刪除原有令牌 if (existingAccessToken != null) { tokenStore.removeAccessToken(existingAccessToken); } else if (refreshToken instanceof ExpiringOAuth2RefreshToken) { ExpiringOAuth2RefreshToken expiring = (ExpiringOAuth2RefreshToken) refreshToken; if (System.currentTimeMillis() > expiring.getExpiration().getTime()) { refreshToken = createRefreshToken(authentication); } } OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken); tokenStore.storeAccessToken(accessToken, authentication); // In case it was modified refreshToken = accessToken.getRefreshToken(); if (refreshToken != null) { tokenStore.storeRefreshToken(refreshToken, authentication); } return accessToken; }
重寫(xiě) Token key 生成邏輯
如上代碼,我們實(shí)現(xiàn)用戶(hù)單一終端的唯一性登錄,什么是單一終端 我們可以類(lèi)比 QQ 登錄 移動(dòng)端和 PC 端可以同時(shí)登錄,但 移動(dòng)端 和移動(dòng)端不能同時(shí)在線。
如何能夠?qū)崿F(xiàn) 在不同客戶(hù)端也能夠唯一性登錄呢?
先來(lái)看上文源碼 `OAuth2AccessToken existingAccessToken=tokenStore.getAccessToken(authentication);
是如何根據(jù)用戶(hù)信息判斷 token 存在的呢?
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) { String key = authenticationKeyGenerator.extractKey(authentication); // redis 查詢(xún)邏輯,根據(jù) key return accessToken; }
AuthenticationKeyGenerator key值生成器 默認(rèn)情況下根據(jù) username
/clientId
/scope
參數(shù)組合生成唯一token
public String extractKey(OAuth2Authentication authentication) { Map<String, String> values = new LinkedHashMap<String, String>(); OAuth2Request authorizationRequest = authentication.getOAuth2Request(); if (!authentication.isClientOnly()) { values.put(USERNAME, authentication.getName()); } values.put(CLIENT_ID, authorizationRequest.getClientId()); if (authorizationRequest.getScope() != null) { values.put(SCOPE, OAuth2Utils.formatParameterList(new TreeSet<String>(authorizationRequest.getScope()))); } return generateKey(values); }
若想實(shí)現(xiàn),多終端的唯一性登錄,只需要使得同一個(gè)用戶(hù)在多個(gè)終端生成的 token
一致,加上上文提到的 createToken 修改邏輯,既去掉extractKey
的 clientId 條件,不區(qū)分終端即可
public String extractKey(OAuth2Authentication authentication) { Map<String, String> values = new LinkedHashMap<String, String>(); OAuth2Request authorizationRequest = authentication.getOAuth2Request(); if (!authentication.isClientOnly()) { values.put(USERNAME, authentication.getName()); } if (authorizationRequest.getScope() != null) { values.put(SCOPE, OAuth2Utils.formatParameterList(new TreeSet<String>(authorizationRequest.getScope()))); } return generateKey(values); }
最后在 authserver 中注入新的 TokenService 即可
到此這篇關(guān)于Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼的文章就介紹到這了,更多相關(guān)Spring Security OAuth2 登錄互踢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot + Spring Security 基本使用及個(gè)性化登錄配置詳解
- spring security自定義認(rèn)證登錄的全過(guò)程記錄
- spring-boot集成spring-security的oauth2實(shí)現(xiàn)github登錄網(wǎng)站的示例
- 詳解使用Spring Security進(jìn)行自動(dòng)登錄驗(yàn)證
- Spring Security Oauth2.0 實(shí)現(xiàn)短信驗(yàn)證碼登錄示例
- 詳解Spring Security如何配置JSON登錄
- spring security自定義登錄頁(yè)面
- Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解
- Spring Security登錄添加驗(yàn)證碼的實(shí)現(xiàn)過(guò)程
- Spring Security 表單登錄功能的實(shí)現(xiàn)方法
相關(guān)文章
利用Spring Boot創(chuàng)建docker image的完整步驟
這篇文章主要給大家介紹了關(guān)于如何利用Spring Boot創(chuàng)建docker image的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java FineReport報(bào)表工具導(dǎo)出EXCEL的四種方式
這篇文章主要介紹了Java FineReport報(bào)表工具導(dǎo)出EXCEL的四種方式的相關(guān)資料,需要的朋友可以參考下2016-03-03Spring Cloud Netflix架構(gòu)淺析(小結(jié))
這篇文章主要介紹了Spring Cloud Netflix架構(gòu)淺析(小結(jié)),詳解的介紹了Spring Cloud Netflix的概念和組件等,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01SpringMVC4 + MyBatis3 + SQL Server 2014整合教程(含增刪改查分頁(yè))
這篇文章主要給大家介紹了關(guān)于SpringMVC4 + MyBatis3 + SQL Server 2014整合的相關(guān)資料,文中包括介紹了增刪改查分頁(yè)等相關(guān)內(nèi)容,通過(guò)示例代碼介紹的非常詳細(xì),分享出來(lái)供大家參考學(xué)習(xí),下面來(lái)一起看看吧。2017-06-06Hibernate初體驗(yàn)及簡(jiǎn)單錯(cuò)誤排除代碼詳解
這篇文章主要介紹了Hibernate初體驗(yàn)及簡(jiǎn)單錯(cuò)誤排除代碼詳解,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02idea編譯時(shí)不提示任何錯(cuò)誤信息的問(wèn)題及解決
這篇文章主要介紹了idea編譯時(shí)不提示任何錯(cuò)誤信息的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12SpringSecurity怎樣使用注解控制權(quán)限
這篇文章主要介紹了SpringSecurity怎樣使用注解控制權(quán)限的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06