|
@@ -0,0 +1,146 @@
|
|
|
+package com.jkcredit.utils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.jkcredit.entity.Car;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+
|
|
|
+ * @description: 构建签名辅助类
|
|
|
+ * @author: sunzhaoning
|
|
|
+ * @create: 2019-05-14 11:06
|
|
|
+ * @version: V1.0
|
|
|
+ **/
|
|
|
+@Slf4j
|
|
|
+public class SignUtils {
|
|
|
+
|
|
|
+
|
|
|
+ * 构建参数
|
|
|
+ * @param car 入参实体
|
|
|
+ * @param apiName api 名称
|
|
|
+ * @param ip ip地址
|
|
|
+ * @param appKey appKey
|
|
|
+ * @param secret secret
|
|
|
+ * @return Map
|
|
|
+ */
|
|
|
+ public static Map<String, Object> getSign(Car car,String apiName,String ip,String appKey,String secret) throws UnsupportedEncodingException {
|
|
|
+ String sign;
|
|
|
+
|
|
|
+ Map<String, String> jsonMap = new HashMap<>(5);
|
|
|
+ Map<String, Object> param = new HashMap<>(12);
|
|
|
+
|
|
|
+ String carInfo = "carInfo";
|
|
|
+ String stolenCar ="stolenCar";
|
|
|
+ String isMortgage ="mortgage";
|
|
|
+
|
|
|
+ if((carInfo).equals(apiName)){
|
|
|
+ jsonMap.put("FDJH", car.getEngineNumber());
|
|
|
+ jsonMap.put("CLSBDH", car.getVehicleIdentificationNumber());
|
|
|
+ jsonMap.put("CJH", car.getVin());
|
|
|
+ jsonMap.put("HPHM", car.getPlateNumber());
|
|
|
+ param.put("name", "gab_jdcxxcx");
|
|
|
+ param.put("returnColumns", "HPZL,CCDJRQ,CLPP1,FZJG,DYBJ");
|
|
|
+ }else if((stolenCar).equals(apiName)){
|
|
|
+ jsonMap.put("FDJH", car.getEngineNumber());
|
|
|
+ jsonMap.put("CJH", car.getVin());
|
|
|
+ jsonMap.put("HPHM", car.getPlateNumber());
|
|
|
+ jsonMap.put("CLSBDH", car.getVehicleIdentificationNumber());
|
|
|
+ param.put("name", "gab_bdqc");
|
|
|
+ param.put("returnColumns", "FDJH,CJH,HPHM");
|
|
|
+ }else if((isMortgage).equals(apiName)){
|
|
|
+ jsonMap.put("CLSBDH",car.getVehicleIdentificationNumber()); jsonMap.put("FDJH",car.getEngineNumber());
|
|
|
+ jsonMap.put("HPHM", car.getPlateNumber());
|
|
|
+ param.put("name", "gab_jdcsfdy");
|
|
|
+ param.put("returnColumns", "DYBJ");
|
|
|
+ }else{
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, String> map = sortMapByKey(jsonMap);
|
|
|
+ String json = JSON.toJSONString(map);
|
|
|
+
|
|
|
+ Map<String,String> userInfoMap = new HashMap<>(2);
|
|
|
+ userInfoMap.put("ip",ip);
|
|
|
+ userInfoMap.put("mac","00-23-24-8B-C4-81");
|
|
|
+
|
|
|
+ String userInfo = JSON.toJSONString(userInfoMap);
|
|
|
+
|
|
|
+
|
|
|
+ param.put("app_key", appKey);
|
|
|
+ param.put("data", json);
|
|
|
+
|
|
|
+ param.put("timestamp", getTime());
|
|
|
+ param.put("userInfo", userInfo);
|
|
|
+ param.put("version", "1.0");
|
|
|
+ param.put("format", "json");
|
|
|
+
|
|
|
+
|
|
|
+ sign = SignUtils.buildSign(param, secret);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ param.put("sign", sign);
|
|
|
+
|
|
|
+ return param;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 构建签名
|
|
|
+ * @param paramsMap 参数
|
|
|
+ * @param secret 密钥
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String buildSign(Map<String, ?> paramsMap, String secret) {
|
|
|
+ Set<String> keySet = paramsMap.keySet();
|
|
|
+ List<String> paramNames = new ArrayList<>(keySet);
|
|
|
+
|
|
|
+ Collections.sort(paramNames);
|
|
|
+
|
|
|
+ StringBuilder paramNameValue = new StringBuilder();
|
|
|
+
|
|
|
+ for (String paramName : paramNames) {
|
|
|
+ paramNameValue.append(paramName).append(paramsMap.get(paramName));
|
|
|
+ }
|
|
|
+ String source = secret + paramNameValue.toString() + secret;
|
|
|
+
|
|
|
+ return Md5Utils.md5(source);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取当前时间 格式yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getTime() {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 使用 Map按key进行排序
|
|
|
+ * @param map
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<String, String> sortMapByKey(Map<String, String> map) {
|
|
|
+ if (map == null || map.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, String> sortMap = new TreeMap<>(
|
|
|
+ new MapKeyComparator());
|
|
|
+
|
|
|
+ sortMap.putAll(map);
|
|
|
+
|
|
|
+ return sortMap;
|
|
|
+ }
|
|
|
+ static class MapKeyComparator implements Comparator<String>{
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int compare(String str1, String str2) {
|
|
|
+
|
|
|
+ return str1.compareTo(str2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|