1. @RequestMapping(value = "/home", method = RequestMethod.GET)//需要设置的IDP的回调地址redirect_uri
  2. public Object get(@RequestParam("code")String code) throws IOException {
  3. if(StringUtils.isNotBlank(code)){
  4. String accessKeyId = "xxxx";
  5. String secretAccessId = "xxxx";
  6. HttpClient httpClient = NdHttpClientBuilder.getHttpClient(accessKeyId, secretAccessId);//此类由sdk提供
  7. JSONObject obj = new JSONObject();
  8. obj.put("access_key_id", accessKeyId);
  9. obj.put("secret_access_key", secretAccessId);
  10. obj.put("code", code);
  11. obj.put("grant_type", "authorization_code");
  12. HttpPost request = new HttpPost(API_GATEWAY_HOST+"/oauth/access_token");
  13. request.setHeader("Content-Type","application/json");
  14. StringEntity entity = new StringEntity(obj.toString());
  15. request.setHeader("sdp-app-id",SDP_APP_ID);
  16. request.setEntity(entity);
  17. HttpHost httpHost = HttpHost.create(API_GATEWAY_HOST);
  18. HttpResponse httpResponse = httpClient.execute(httpHost,request);
  19. if(httpResponse.getStatusLine().getStatusCode()==200){
  20. String result = EntityUtils.toString(httpResponse.getEntity());
  21. JSONObject token = JSONObject.parseObject(result,JSONObject.class);
  22. return token;
  23. }
  24. return EntityUtils.toString(httpResponse.getEntity());
  25. }
  26. return "code is null";
  27. }
作者:wangtc  创建时间:2023-10-08 10:41
 更新时间:2023-11-14 11:11