Spring Security OAuth2 實現(xiàn)登錄互踢的示例代碼
本文主要介紹了Spring Security OAuth2 實現(xiàn)登錄互踢的示例代碼,分享給大家,具體如下:
背景說明
一個賬號只能一處登錄,類似的業(yè)務需求在現(xiàn)有后管類系統(tǒng)是非常常見的。 但在原有的 spring security oauth2 令牌方法流程(所謂的登錄)無法滿足類似的需求。
我們先來看 TokenEndpoint 的方法流程
客戶端 帶參訪問 /oauth/token 接口,最后去調(diào)用 TokenGranter
TokenGranter 根據(jù)不同的授權(quán)類型,獲取用戶認證信息 并去調(diào)用TokenServices 生成令牌
重新 TokenService
重寫發(fā)放邏輯createAccessToken,當用戶管理的令牌存在時則刪除重新創(chuàng)建,這樣會導致之前登陸獲取的token 失效,順理成章的被擠掉。
@Transactional public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException { OAuth2AccessToken existingAccessToken = tokenStore.getAccessToken(authentication); OAuth2RefreshToken refreshToken = null; // 重寫此處,當用戶關(guān)聯(lián)的token 存在時,刪除原有令牌 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 生成邏輯
如上代碼,我們實現(xiàn)用戶單一終端的唯一性登錄,什么是單一終端 我們可以類比 QQ 登錄 移動端和 PC 端可以同時登錄,但 移動端 和移動端不能同時在線。
如何能夠?qū)崿F(xiàn) 在不同客戶端也能夠唯一性登錄呢?
先來看上文源碼 `OAuth2AccessToken existingAccessToken=tokenStore.getAccessToken(authentication);
是如何根據(jù)用戶信息判斷 token 存在的呢?
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) { String key = authenticationKeyGenerator.extractKey(authentication); // redis 查詢邏輯,根據(jù) key return accessToken; }
AuthenticationKeyGenerator key值生成器 默認情況下根據(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);}
若想實現(xiàn),多終端的唯一性登錄,只需要使得同一個用戶在多個終端生成的 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 實現(xiàn)登錄互踢的示例代碼的文章就介紹到這了,更多相關(guān)Spring Security OAuth2 登錄互踢內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)2. ASP.NET MVC遍歷驗證ModelState的錯誤信息3. jsp網(wǎng)頁實現(xiàn)貪吃蛇小游戲4. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向5. CSS hack用法案例詳解6. asp中response.write("中文")或者js中文亂碼問題7. 將properties文件的配置設(shè)置為整個Web應用的全局變量實現(xiàn)方法8. PHP設(shè)計模式中工廠模式深入詳解9. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明10. ASP實現(xiàn)加法驗證碼
