【发布时间】:2018-08-31 17:44:06
【问题描述】:
我正在尝试在 JWT 令牌中添加新字段,它实际上是 access_token,它是由 grant_type=password 生成的。如果授权类型仅为password,我想添加更多字段。
如果我实现了自定义令牌增强器,它会在 oauth login api 的响应正文中添加新字段。但我只需要access_token JWT 中的那些新字段。
例如:
解码access_token时,Object应该来自
{
"user_name": "uuid",
"scope": [
"trust"
],
"exp": 1522008499,
"authorities": [
"USER"
],
"jti": "9d827f63-99ba-4fc1-a838-bc74331cf660",
"client_id": "myClient"
}
到
{
"user_name": "uuid",
"newField": [
{
"newFieldChild": "1",
},
{
"newFieldChild": "2",
}
],
"scope": [
"trust"
],
"exp": 1522008499,
"authorities": [
"USER"
],
"jti": "9d827f63-99ba-4fc1-a838-bc74331cf660",
"client_id": "myClient"
}
实现CustomTokenEnhancer在登录的响应体中添加newField列表:
{
"access_token": "jwt-access_token",
"token_type": "bearer",
"refresh_token": "jwt-refresh_token",
"expires_in": 299999,
"scope": "trust",
"jti": "b23affb3-39d3-408a-bedb-132g6de15d7",
"newField": [
{
"newFieldChild": "1",
},
{
"newFieldChild": "2",
}
]
}
CustomTokenEnhancer:
public class CustomTokenEnhancer implements TokenEnhancer {
@Override
public OAuth2AccessToken enhance(
OAuth2AccessToken accessToken,
OAuth2Authentication authentication) {
Map<String, Object> additionalInfo = new HashMap<>();
Map<String, String> newFields = ....;
additionalInfo.put("newField", newFields);
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
}
}
如果grant_type 是password,是否可以修改access_token JWT?
【问题讨论】:
标签: spring-boot oauth-2.0 spring-security-oauth2