Commit ab97d384 by yangyang

凯亚接口处理(海南)

parent f8893b63
package com.founder.commonutils.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.java.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.*;
/**
* @ClassName: KyTokenUtils
* @Auther: yangyang
* @Description: java类作用描述
* @CreateDate: 2024-03-28
* @Version: 1.0
*/
@Component
@Log
public class KyTokenUtils {
private static StringRedisTemplate redisTemplate;
@Autowired
public void setRestTemplate(StringRedisTemplate redisTemplate) {
KyTokenUtils.redisTemplate = redisTemplate;
}
/**
* 判断并获取新的token(海南凯亚)
* @return
*/
public static String getHnKyToken (){
String result_token = null;
try{
// 1 先比较已有token是否过期或空
long nowtime = new Date().getTime();
// 获取redis tokenKy
Map<Object,Object> redisMap = redisTemplate.opsForHash().entries("tokenKy");
String token = String.valueOf(redisMap.get("token"));
String expiration = String.valueOf(redisMap.get("expiration"));
System.out.println("得到的token: "+token);
System.out.println("得到的时间是: "+expiration);
if("null" == token || "null" == expiration){
// 2 如果为空获取新的token值
result_token = readeWriteHnKyToken(nowtime);
}else{
// 3 如果过期获取新的token值
if(nowtime > Long.valueOf(expiration)){
result_token= readeWriteHnKyToken(nowtime);
}else{
result_token = token;
}
}
}catch (Exception e){
e.printStackTrace();
log.info(e.getMessage());
}
return result_token;
}
// 读和写海南凯亚的token值
public static String readeWriteHnKyToken(long nowtime){
String result_token="";
String token_url = "http://74.10.30.174:18093";
String username = "xingjdj";
//如果过期先获取新的token值
Map headerMap=new HashMap();
headerMap.put("login-channel","API");
String clientUid = getClientUid(token_url);// 随机码
String password =RsaUtil.sign(clientUid+"."+username);// 随机码+“.”+用户名 RSA私钥进行SHA1WithRSA数字签名
JSONObject object = new JSONObject();
object.put("username", username);
object.put("password", password);
object.put("clientUid", clientUid);
try {
String resultString = com.founder.commonutils.util.HttpClient.doPostJson(token_url+"/auth/login",headerMap,JSONObject.toJSONString(object));
if (!StringUtils.isEmpty(resultString)) {
JSONObject result=JSONObject.parseObject(resultString);
if(result.getInteger("code")==200){
JSONObject resultData=result.getJSONObject("data");
result_token = resultData.getString("authorization");
String expires_in = resultData.getString("expireTime");//有效时间段 单位为秒 1800秒
String result_expiration = nowtime+Integer.parseInt(expires_in)*1000+"";
// 写入新的token值和本次token的过期时间
Map<String, Object> map = new HashMap<>();
map.put("token", result_token);
map.put("expiration", result_expiration);
redisTemplate.opsForHash().putAll("tokenKy", map);
}else{
return result.getString("msg");
}
}
}catch (Exception e){
e.printStackTrace();
log.info("获取token错误"+e.getMessage());
}
return result_token;
}
// 获取海南凯亚的clientUid
public static String getClientUid(String token_url){
JSONObject jsonObject = JSONObject.parseObject(HttpUtil.doGet(token_url+"/auth/getClientUid"));
return jsonObject.getString("clientUid");
}
}
......@@ -4,6 +4,7 @@ import com.founder.commonutils.carEntity.CarKkXxParams;
import com.founder.commonutils.peopleEntity.CameraFaceTask;
import com.founder.commonutils.peopleEntity.FaceSearchParam;
import com.founder.commonutils.publicEntity.MapRestResult;
import com.founder.commonutils.util.KyTokenUtils;
import com.founder.commonutils.util.TokenUtils;
import com.founder.publicapi.service.ImageService;
import com.founder.servicebase.logs.OperLog;
......@@ -37,10 +38,10 @@ public class ImageController {
@OperLog(message = "token获取",operation = OperationType.QUERY)
@ApiOperation(value = "token获取")
@PostMapping("/token")
@GetMapping("/token")
public MapRestResult getToken() {
//获取token
String accessToken = TokenUtils.getHnKyToken();
String accessToken = KyTokenUtils.getHnKyToken();
return MapRestResult.build(200,"成功获取token信息","1",accessToken);
}
......
......@@ -4,10 +4,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.founder.commonutils.peopleEntity.*;
import com.founder.commonutils.publicEntity.MapRestResult;
import com.founder.commonutils.util.DataUtil;
import com.founder.commonutils.util.HttpClient;
import com.founder.commonutils.util.HttpUtil;
import com.founder.commonutils.util.TokenUtils;
import com.founder.commonutils.util.*;
import com.founder.publicapi.oracleXzxtMapper.CameraInfoMapper;
import com.founder.publicapi.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -358,7 +355,7 @@ public class ImageServiceImpl implements ImageService {
}
// token获取
public Map getToken(){
String token= TokenUtils.getHnKyToken();
String token= KyTokenUtils.getHnKyToken();
Map herders=new HashMap();
//设置请求的header参数
herders.put("Authorization",token);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment