3 回答

TA貢獻1789條經(jīng)驗 獲得超10個贊
Bhanuprakash,我也有類似的問題,但我暫時這樣做了:
在“l(fā)oadUserByUsername”方法中寫下:
HttpServletRequest 請求 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); System.out.println("compnayId:" + request.getParameter("companyId"));
我認為有一個更優(yōu)雅的解決方案,但我暫時使用了這個。
謝謝。

TA貢獻1887條經(jīng)驗 獲得超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條經(jīng)驗 獲得超12個贊
這條評論對我來說非常有用!
HttpServletRequest 請求 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); System.out.println("compnayId:" + request.getParameter("companyId"));
添加回答
舉報