spring-boot - 在 Spring Boot Oauth2 响应中添加新字段
问题描述
我被装箱了一个 Spring Boot Oauth2 身份验证并且工作正常。我需要使用 Oauth2 响应添加用户类型字段。
我的代码如下。
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
static final String CLIEN_ID = "client";
//static final String CLIENT_SECRET = "devglan";
static final String CLIENT_SECRET = "$2a$04$e/c1/RfsWuTh/vj/BfG";
static final String GRANT_TYPE = "password";
static final String AUTHORIZATION_CODE = "authorization_code";
static final String REFRESH_TOKEN = "refresh_token";
static final String IMPLICIT = "implicit";
static final String SCOPE_READ = "read";
static final String SCOPE_WRITE = "write";
static final String TRUST = "trust";
static final int ACCESS_TOKEN_VALIDITY_SECONDS = 50*60*60;
static final int FREFRESH_TOKEN_VALIDITY_SECONDS = 66*60*60;
@Autowired
private TokenStore tokenStore;
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
configurer
.inMemory()
.withClient(CLIEN_ID)
.secret(CLIENT_SECRET)
.authorizedGrantTypes(GRANT_TYPE, AUTHORIZATION_CODE, REFRESH_TOKEN, IMPLICIT )
.scopes(SCOPE_READ, SCOPE_WRITE, TRUST)
.accessTokenValiditySeconds(ACCESS_TOKEN_VALIDITY_SECONDS).
refreshTokenValiditySeconds(FREFRESH_TOKEN_VALIDITY_SECONDS);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore)
.authenticationManager(authenticationManager);
}
}
认证后的响应如下
{"access_token":"b3336423-ed9d-4d91-a308-2a5d16dbc037","token_type":"bearer","refresh_token":"135f6f95-8f5b-404a-83fc-11e12bf772be","expires_in":179999,"scope":"read write trust"}
我需要在上面的响应中添加用户类型字段。
解决方案
我得到了这个 Create CustomTokenConverter类的解决方案
@Component
public class CustomTokenConverter extends JwtAccessTokenConverter {
@Autowired
private UserRepository userRepository;
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
final Map<String, Object> additionalInfo = new HashMap<>();
User user = userRepository.findByUsername(authentication.getName());
additionalInfo.put("usertype", user.getTypeOfUser());
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return super.enhance(accessToken, authentication);
}
}
并更新AuthorizationServerConfig类
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore)
.tokenEnhancer(customTokenEnhancer())
.authenticationManager(authenticationManager);
}
@Bean
public CustomTokenConverter customTokenEnhancer() {
return new CustomTokenConverter();
}
现在我得到了这样的回应
{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbInJlYWQiLCJ3cml0ZSIsInRydXN0Il0sInVzZXJ0eXBlIjoiQWRtaW4iLCJleHAiOjE1NzcyODA3ODYsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiZDk4MTkxOWYtZDMzOC00YTE2LTk4NTEtYWFjODUzZWYyOGE4IiwiY2xpZW50X2lkIjoiZGV2Z2xhbi1jbGllbnQifQ.BuuVK6HFajOM9vryciwBi6-aMSMOrV5E0YiPyPmZ0Uw","token_type":"bearer","refresh_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbInJlYWQiLCJ3cml0ZSIsInRydXN0Il0sImF0aSI6ImQ5ODE5MTlmLWQzMzgtNGExNi05ODUxLWFhYzg1M2VmMjhhOCIsInVzZXJ0eXBlIjoiQWRtaW4iLCJleHAiOjE1NzczMzgzODYsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiZGNjYTEyZDUtNzY1Ny00N2I5LThkYmMtNTkzOWQzZDk3MWYzIiwiY2xpZW50X2lkIjoiZGV2Z2xhbi1jbGllbnQifQ.L_vguBCDOeAGNlq-L-OiPO6TW2gRXNBv562JnyR3uSE","expires_in":179999,"scope":"read write trust","usertype":"Admin","jti":"d981919f-d338-4a16-9851-aac853ef28a8"}
谢谢你
推荐阅读
- swift - 如何在现实世界中随机放置 AR 对象?
- node.js - 如何使用 Google 计算 node.js api 创建具有公共 IP 地址的接口?
- mysql - 从 Dash 框架中的输入字段中检索值
- javascript - React.js 用户对话 UI 设计
- sql-server - 在 SQL Server 中使用条件读取节点值
- typescript - TypeScript 中的另一个多态性问题
- python - Pandas 基于单个列进行聚合,同时保留其他列
- git - 从 master 获取最新更新到我的分支
- php - 在 PHP 中使用无限时间的 Oracle 查询
- python - Seaborn 点图中的分离点和误差条问题