3 回答
TA贡献1789条经验 获得超10个赞
Bhanuprakash,我也有类似的问题,但我暂时这样做了:
在“loadUserByUsername”方法中写下:
HttpServletRequest 请求 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); System.out.println("compnayId:" + request.getParameter("companyId"));
我认为有一个更优雅的解决方案,但我暂时使用了这个。
谢谢。
TA贡献1887条经验 获得超5个赞
如果您想要访问令牌的其他信息,您可以使用 TokenEnhancer 类来做到这一点。
CustomTokenEnhancer.java
public class CustomTokenEnhancer implements TokenEnhancer {
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
User user = (User) authentication.getPrincipal();
final Map<String, Object> additionalInfo = new HashMap<>();
additionalInfo.put("id", user.getCompanyId());
additionalInfo.put("authorities", user.getAuthorities());
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
}
}
然后使用此类的实例来 void configure(AuthorizationServerEndpointsConfigurer endpoints) 像这样的方法
AuthorizationServerConfig.java
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
.tokenEnhancer(new CustomTokenEnhancer());
}
TA贡献1874条经验 获得超12个赞
这条评论对我来说非常有用!
HttpServletRequest 请求 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); System.out.println("compnayId:" + request.getParameter("companyId"));
添加回答
举报