use of com.dtflys.forest.handler.OAuth2DefinitionHandler in project forest by dromara.
the class OAuth2LifeCycle method executeRequestToken.
/**
* 执行实际的网络请求
*
* @param request 当前请求
* @param clientId 客户端ID
* @param body 请求内容
* @return 返回新的 Token 信息
*/
private TokenCache executeRequestToken(ForestRequest request, String clientId, Map<String, Object> body) {
// 加入扩展参数
String[] bodyItems = (String[]) getAttribute(request, "body");
body.putAll(kv2map(bodyItems));
Map<String, Object> queryItems = kv2map((String[]) getAttribute(request, "query"));
Class<? extends OAuth2DefinitionHandler> handlerClass = request.getMethod().getMethod().getAnnotation(OAuth2.class).OAuth2TokenHandler();
ForestResponse<String> response = oAuth2Client.token(getAttributeAsString(request, "tokenUri"), queryItems, body);
OAuth2Token token;
try {
OAuth2DefinitionHandler handler = handlerClass.newInstance();
ForestConfiguration configuration = request.getConfiguration();
Map map = (Map) configuration.getConverter(ForestDataType.AUTO).convertToJavaObject(response.getContent(), Map.class);
token = handler.getOAuth2Token(response, map);
} catch (InstantiationException | IllegalAccessException e) {
throw new ForestRuntimeException("OAuth2 request OAuth2DefinitionHandler error" + e.getMessage());
}
if (token == null) {
throw new ForestRuntimeException("OAuth2 request OAuth2Token is empty");
}
return new TokenCache(clientId, token);
}
Aggregations