国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring Security OAuth 個性化token的使用

Spring Security OAuth 個性化token的使用

2021-07-16 14:45冷冷 Java教程

這篇文章主要介紹了Spring Security OAuth 個性化token的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

個性化token 目的

默認(rèn)通過調(diào)用 /oauth/token 返回的報文格式包含以下參數(shù)

?
1
2
3
4
5
6
7
{
 "access_token": "e6669cdf-b6cd-43fe-af5c-f91a65041382",
 "token_type": "bearer",
 "refresh_token": "da91294d-446c-4a89-bdcf-88aee15a75e8",
 "expires_in": 43199,
 "scope": "server"
}

并沒包含用戶的業(yè)務(wù)信息比如用戶信息、租戶信息等。

擴(kuò)展生成包含業(yè)務(wù)信息(如下),避免系統(tǒng)多次調(diào)用,直接可以通過認(rèn)證接口獲取到用戶信息等,大大提高系統(tǒng)性能

?
1
2
3
4
5
6
7
8
9
10
11
12
{
 "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 源碼解析

Spring Security OAuth 個性化token的使用

? 主頁參考紅框部分

resourceownerpasswordtokengranter (密碼模式)根據(jù)用戶的請求信息,進(jìn)行認(rèn)證得到當(dāng)前用戶上下文信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
protected oauth2authentication getoauth2authentication(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);
 
 oauth2request storedoauth2request = getrequestfactory().createoauth2request(client, tokenrequest); 
  return new oauth2authentication(storedoauth2request, userauth);
}

然后調(diào)用abstracttokengranter.getaccesstoken() 獲取oauth2accesstoken

?
1
2
3
protected oauth2accesstoken getaccesstoken(clientdetails client, tokenrequest tokenrequest) {
 return tokenservices.createaccesstoken(getoauth2authentication(client, tokenrequest));
}

默認(rèn)使用defaulttokenservices來獲取token

?
1
2
3
4
5
6
7
8
9
10
11
12
public oauth2accesstoken createaccesstoken(oauth2authentication authentication) throws authenticationexception {
 
 ... 一系列判斷 ,合法性、是否過期等判斷
 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;
}

createaccesstoken 核心邏輯

?
1
2
3
4
5
6
7
8
9
10
11
12
// 默認(rèn)刷新token 的有效期
private int refreshtokenvalidityseconds = 60 * 60 * 24 * 30; // default 30 days.
// 默認(rèn)token 的有效期
private int accesstokenvalidityseconds = 60 * 60 * 12; // default 12 hours.
 
private oauth2accesstoken createaccesstoken(oauth2authentication authentication, oauth2refreshtoken refreshtoken) {
 defaultoauth2accesstoken token = new defaultoauth2accesstoken(uuid);
 token.setexpiration(date)
 token.setrefreshtoken(refreshtoken);
 token.setscope(authentication.getoauth2request().getscope());
 return accesstokenenhancer != null ? accesstokenenhancer.enhance(token, authentication) : token;
}

如上代碼,在拼裝好token對象后會調(diào)用認(rèn)證服務(wù)器配置tokenenhancer( 增強(qiáng)器) 來對默認(rèn)的token進(jìn)行增強(qiáng)。

tokenenhancer.enhance 通過上下文中的用戶信息來個性化token

?
1
2
3
4
5
6
7
8
9
10
11
public oauth2accesstoken enhance(oauth2accesstoken accesstoken, oauth2authentication 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);
 ((defaultoauth2accesstoken) accesstoken).setadditionalinformation(additionalinfo);
 return accesstoken;
}

基于pig 看下最終的實(shí)現(xiàn)效果

pig 基于spring cloud、oauth2.0開發(fā)基于vue前后分離的開發(fā)平臺,支持賬號、短信、sso等多種登錄,提供配套視頻開發(fā)教程。

https://gitee.com/log4j/pig

Spring Security OAuth 個性化token的使用

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://segmentfault.com/a/1190000018187384

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美福利在线观看 | 成人免费一区二区三区视频网站 | 国产精品女同一区二区免费站 | 米奇777超碰欧美日韩亚洲 | 中文字幕在线精品 | 免费看a| 欧美片网站免费 | 久久国内精品 | 国产精品99久久久久久动医院 | 影音先锋中文字幕一区 | 夜夜操导航 | 夜久久 | 一级特色黄大片 | 日韩高清一区二区 | 转生成为史莱姆这档事第四季在线观看 | www.久久久| 黄一区| 国产伦精品一区二区三区照片91 | 天天射影院 | 爱操av | 久久亚洲综合 | 欧美大黄大色一级毛片 | 日韩中文字幕在线播放 | 久久久久久久av | 国产精品自拍视频 | 毛片免费视频 | 狠狠影院 | 激情欧美日韩一区二区 | av一区二区在线观看 | 国产一级片| 久草青青草 | 日韩欧美一级片在线观看 | 亚洲狠狠爱 | 久久久精品日本 | 性农村人freesex | 久久伊99综合婷婷久久伊 | 国产成人精品一区二区三区四区 | 久久国产精品电影 | 91人人爽人人爽人人精88v | 亚洲综合av在线播放 | 精品国产乱码久久久久久久软件 |