一:
* 第三方授权
* 得到第三方token和Expires(过期时间)
说明:请求第三方数据时,需要授权。第三方会给到appid和secret,请求携带appid和secret获取一个token和expires,又了token就又了操作第三方数据的权限。每次操作第三方数据时就需要携带token。
maven坐标
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
Map<String, Object> params = new HashMap<>();
params.put("appid", authProperties.getAppid());
params.put("secret", authProperties.getSecret());
log.info("AuthUtils.得到配置文件appid和secret");
log.info("appid===:{}", authProperties.getAppid());
log.info("secret===:{}", authProperties.getSecret());
log.info("auth地址===:{}", ipUtil.getOip() + "/auth/token");
String str = HttpRequest.post("183.xxx.xxx.xx:9981" + "/auth/token?appid="+authProperties.getAppid()+"&secret="+authProperties.getSecret())
.timeout(500)//超时,毫秒
.execute().body();
log.info("调用第三方auth传回的json数据{}",str);
JSONObject jsonObject = JSON.parseObject(str);
JSONObject data = jsonObject.getJSONObject("data");
String token = data.get("token").toString();
String expires = data.get("expires").toString();
System.out.println(token);
二:
发送数据给第三方,请求头放入token,才能权限请求。
Map<String, Object> params = new HashMap<>();
params.put("userName","小明");
params.put("userIDcard","123123");
params.put("userSeat","3");
JSONObject data = new JSONObject(params);
String s = HttpRequest.post(ipUtil.getOip() + "/wer/tte/wrt/pickUpData")
.header("token","0d173774b9274ed9883b17xxxxx")
.contentType("application/json")
.body(data.toJSONString())
.timeout(20000)//超时,毫秒
.execute().body();
System.out.println("s"+"===>"+s);
JSONObject jsonObject = JSON.parseObject(s);
log.info("code:{}",jsonObject.get("code"));