久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼

瀏覽:93日期:2023-09-07 10:39:38

本文主要介紹了Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼,分享給大家,具體如下:

Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼

背景說(shuō)明

一個(gè)賬號(hào)只能一處登錄,類似的業(yè)務(wù)需求在現(xiàn)有后管類系統(tǒng)是非常常見(jiàn)的。 但在原有的 spring security oauth2 令牌方法流程(所謂的登錄)無(wú)法滿足類似的需求。

我們先來(lái)看 TokenEndpoint 的方法流程

客戶端 帶參訪問(wèn) /oauth/token 接口,最后去調(diào)用 TokenGranter

Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼

TokenGranter 根據(jù)不同的授權(quán)類型,獲取用戶認(rèn)證信息 并去調(diào)用TokenServices 生成令牌

Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼

重新 TokenService

重寫發(fā)放邏輯createAccessToken,當(dāng)用戶管理的令牌存在時(shí)則刪除重新創(chuàng)建,這樣會(huì)導(dǎo)致之前登陸獲取的token 失效,順理成章的被擠掉。

@Transactional public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException { OAuth2AccessToken existingAccessToken = tokenStore.getAccessToken(authentication); OAuth2RefreshToken refreshToken = null; // 重寫此處,當(dāng)用戶關(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; }

重寫 Token key 生成邏輯

如上代碼,我們實(shí)現(xiàn)用戶單一終端的唯一性登錄,什么是單一終端 我們可以類比 QQ 登錄 移動(dòng)端和 PC 端可以同時(shí)登錄,但 移動(dòng)端 和移動(dòng)端不能同時(shí)在線。

如何能夠?qū)崿F(xiàn) 在不同客戶端也能夠唯一性登錄呢?

先來(lái)看上文源碼 `OAuth2AccessToken existingAccessToken=tokenStore.getAccessToken(authentication);

是如何根據(jù)用戶信息判斷 token 存在的呢?

public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) { String key = authenticationKeyGenerator.extractKey(authentication); // redis 查詢邏輯,根據(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è)用戶在多個(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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 平乐县| 哈尔滨市| 塘沽区| 会昌县| 萝北县| 达日县| 沈阳市| 三门县| 即墨市| 漯河市| 大田县| 通山县| 新建县| 蒙城县| 保德县| 拉孜县| 云和县| 中江县| 汝阳县| 商都县| 兴安县| 顺义区| 奉新县| 荆门市| 孟连| 神农架林区| 监利县| 林甸县| 望奎县| 淮北市| 眉山市| 中卫市| 通榆县| 广饶县| 霞浦县| 沈阳市| 通江县| 奉节县| 镇平县| 辽阳县| 巩留县|