DynamicTokenGet
1 package com.-.it.regioc.utils;
2
3 import com.alibaba.fastjson.JSON;
4 import com.alibaba.fastjson.JSONObject;
5 import com.-.it.regioc.bean.config.HttpConfig;
6 import org.apache.commons.collections.MapUtils;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import org.springframework.beans.factory.annotation.Value;
10 import org.springframework.stereotype.Component;
11
12 import javax.annotation.Resource;
13 import javax.ws.rs.core.HttpHeaders;
14 import javax.ws.rs.core.MediaType;
15 import java.util.HashMap;
16 import java.util.Map;
17
18 @Component
19 public class DynamicTokenGet {
20
21 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicTokenGet.class);
22
23 @Value("${request.application.appId}")
24 private String appId;
25
26 @Value("${request.appSecret}")
27 private String appSecret;
28
29 @Resource
30 private HttpConfig httpConfig;
31
32 /**
33 * 获取动态token
34 */
35 public String getDynamicToken() {
36 String token = "";
37 try {
38 String url = httpConfig.getDynamicToken();
39 // 请求参数 (集合转换为json字符串)
40 Map<String, Object> mapParam = new HashMap<>();
41 mapParam.put("appId",appId);
42 mapParam.put("credential",httpConfig.getHttpToken());
43 Map<String, String> headerMap = buildHeader();
44 String htmlJson = HttpUtils.postRequest(url, JSON.toJSON(mapParam).toString(), headerMap);
45 JSONObject jsonObject = JSONObject.parseObject(htmlJson);
46 token = MapUtils.getString(jsonObject, "result");
47 } catch (Exception e) {
48 e.printStackTrace();
49 LOGGER.error("#getDynamicToken Error is msg:{0}#", e);
50 }
51 return token;
52 }
53
54 private Map<String, String> buildHeader() {
55 Map<String, String> headerMap = new HashMap<>();
56 headerMap.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
57 return headerMap;
58 }
59
60 }