package com.dsh.guns.modular.system.controller.util; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*; /** * 曹操接口请求签名工具包 * @author 吕雪 * @createDate 2020年01月15日 * client_id:f168c651bf86f2c1 * sign_key:6d2c6566598949478bef1b5d91451e9f */ public class SignUtil { /** * 获取签名 * @param params * @return */ public static String sign(Map params){ params.put("sign_key", "6d2c6566598949478bef1b5d91451e9f"); Map needVerify = new HashMap<>(); for (Map.Entry entry : params.entrySet()) { needVerify.put(entry.getKey(), String.valueOf(entry.getValue())); } List> entryList = new ArrayList<>(needVerify.entrySet()); //排序 Collections.sort(entryList, (o1, o2) -> o1.getKey().compareTo(o2.getKey())); StringBuilder buffer = new StringBuilder(); for (Map.Entry entry : entryList) { buffer.append(entry.getKey()).append(entry.getValue()); } System.out.println("sha1 签名参数:"+buffer.toString()); try { return encodeBySHA(buffer.toString(),"utf-8"); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 使用SHA算法加密 * @param str * @param charset * @return * @throws Exception */ public static String encodeBySHA(String str,String charset) throws Exception { MessageDigest messageDigest = null; try { messageDigest = MessageDigest.getInstance("SHA"); messageDigest.reset(); messageDigest.update(str.getBytes(charset)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new Exception("不能调用指定的加密算法。"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); throw new Exception("字符串不支持"+ charset +"的转换。"); } return byteArrayToString(messageDigest.digest()); } /** * 将byte数组转化为16进制字符串 * @param byteArray * @return */ private static String byteArrayToString(byte[] byteArray){ StringBuilder strBuilder = new StringBuilder(); for (int i = 0; i < byteArray.length; i++) { if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) { strBuilder.append("0").append(Integer.toHexString(0xFF & byteArray[i])); } else { strBuilder.append(Integer.toHexString(0xFF & byteArray[i])); } } return strBuilder.toString(); } // public static void main(String[] args) { //Get /*Long time = new Date().getTime(); Map map = new HashMap<>(); map.put("client_id","f168c651bf86f2c1"); map.put("timestamp",time); map.put("longitude","120.10"); map.put("latitude","30.19"); String url = "https://sandbox-cop.caocaokeji.cn/v2/common/queryCity"; String response = PlatformUtil.doGet(url,"client_id=f168c651bf86f2c1×tamp="+time+"&sign="+SignUtil.sign(map)+"&longitude=120.10&latitude=30.19",null,100000); com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response); System.out.println(jsonObject.toJSONString());*/ //Post /* Map map = new HashMap<>(); map.put("client_id","f168c651bf86f2c1"); map.put("timestamp",new Date().getTime()); map.put("order_id",1); map.put("score","3"); map.put("content","测试数据"); map.put("sign",SignUtil.sign(map)); String jsonMsg =JSONObject.toJSONString(map); String url = "/v2/common/evaluateOrder"; TokenUtil.RequestEntity req = new TokenUtil.RequestEntity(); req.setReqMethod(TokenUtil.REQ_METHOD_POST); req.setUri(url); req.setContent(jsonMsg); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); req.setReqTime(dateFormat.format(new Date())); String response = PlatformUtil.doPostJson1("https://sandbox-cop.caocaokeji.cn"+url,req.getContent(),null); System.out.println(JSONObject.parseObject(response));*/ // System.out.println(new Date().getTime()); // // } }