個性化Token 目的
創新互聯公司服務項目包括大興網站建設、大興網站制作、大興網頁制作以及大興網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,大興網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到大興省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!
默認通過調用 /oauth/token 返回的報文格式包含以下參數
{ "access_token": "e6669cdf-b6cd-43fe-af5c-f91a65041382", "token_type": "bearer", "refresh_token": "da91294d-446c-4a89-bdcf-88aee15a75e8", "expires_in": 43199, "scope": "server" }
并沒包含用戶的業務信息比如用戶信息、租戶信息等。
擴展生成包含業務信息(如下),避免系統多次調用,直接可以通過認證接口獲取到用戶信息等,大大提高系統性能
{ "access_token":"a6f3b6d6-93e6-4eb8-a97d-3ae72240a7b0", "token_type":"bearer", "refresh_token":"710ab162-a482-41cd-8bad-26456af38e4f", "expires_in":42396, "scope":"server", "tenant_id":1, "license":"made by pigx", "dept_id":1, "user_id":1, "username":"admin" }
密碼模式生成Token 源碼解析
主頁參考紅框部分
ResourceOwnerPasswordTokenGranter (密碼模式)根據用戶的請求信息,進行認證得到當前用戶上下文信息
protected OAuth3Authentication getOAuth3Authentication(ClientDetails client, TokenRequest tokenRequest) { Map<String, String> parameters = new LinkedHashMap<String, String>(tokenRequest.getRequestParameters()); String username = parameters.get("username"); String password = parameters.get("password"); // Protect from downstream leaks of password parameters.remove("password"); Authentication userAuth = new UsernamePasswordAuthenticationToken(username, password); ((AbstractAuthenticationToken) userAuth).setDetails(parameters); userAuth = authenticationManager.authenticate(userAuth); OAuth3Request storedOAuth3Request = getRequestFactory().createOAuth3Request(client, tokenRequest); return new OAuth3Authentication(storedOAuth3Request, userAuth); }
然后調用AbstractTokenGranter.getAccessToken() 獲取OAuth3AccessToken
protected OAuth3AccessToken getAccessToken(ClientDetails client, TokenRequest tokenRequest) { return tokenServices.createAccessToken(getOAuth3Authentication(client, tokenRequest)); }
默認使用DefaultTokenServices來獲取token
public OAuth3AccessToken createAccessToken(OAuth3Authentication authentication) throws AuthenticationException { ... 一系列判斷 ,合法性、是否過期等判斷 OAuth3AccessToken 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; }
createAccessToken 核心邏輯
// 默認刷新token 的有效期 private int refreshTokenValiditySeconds = 60 * 60 * 24 * 30; // default 30 days. // 默認token 的有效期 private int accessTokenValiditySeconds = 60 * 60 * 12; // default 12 hours. private OAuth3AccessToken createAccessToken(OAuth3Authentication authentication, OAuth3RefreshToken refreshToken) { DefaultOAuth3AccessToken token = new DefaultOAuth3AccessToken(uuid); token.setExpiration(Date) token.setRefreshToken(refreshToken); token.setScope(authentication.getOAuth3Request().getScope()); return accessTokenEnhancer != null ? accessTokenEnhancer.enhance(token, authentication) : token; }
如上代碼,在拼裝好token對象后會調用認證服務器配置TokenEnhancer( 增強器) 來對默認的token進行增強。
TokenEnhancer.enhance 通過上下文中的用戶信息來個性化Token
public OAuth3AccessToken enhance(OAuth3AccessToken accessToken, OAuth3Authentication authentication) { final Map<String, Object> additionalInfo = new HashMap<>(8); PigxUser pigxUser = (PigxUser) authentication.getUserAuthentication().getPrincipal(); additionalInfo.put("user_id", pigxUser.getId()); additionalInfo.put("username", pigxUser.getUsername()); additionalInfo.put("dept_id", pigxUser.getDeptId()); additionalInfo.put("tenant_id", pigxUser.getTenantId()); additionalInfo.put("license", SecurityConstants.PIGX_LICENSE); ((DefaultOAuth3AccessToken) accessToken).setAdditionalInformation(additionalInfo); return accessToken; }
基于pig 看下最終的實現效果
Pig 基于Spring Cloud、oAuth3.0開發基于Vue前后分離的開發平臺,支持賬號、短信、SSO等多種登錄,提供配套視頻開發教程。
https://gitee.com/log4j/pig
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創新互聯。
文章名稱:SpringSecurityOAuth個性化token的使用
當前路徑:http://www.yijiale78.com/article18/jdohgp.html
成都網站建設公司_創新互聯,為您提供建站公司、品牌網站建設、網站排名、外貿網站建設、外貿建站、全網營銷推廣
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯