官方给的示例虽说很详细,但是对于我这个第一次对接三方平台的人来说还是很懵逼的,所以只能通过不断的测试摸索最后才得到了这个签名结果,作为记录以及为后来的人做参考。
认证方式:
一:AK\SK摘要认证
public static void test034(){ try { /*请求数据信息*/ // 请求地址:http://www.example.com/artemis/api/example?qa=a&qb=B // Http Method:POST // appKey:29666671 // appSecret:empsl21ds3 // X-Ca-Timestamp:1479968678000 /* Headers*/ // Accept:*/* // Accept-Encoding:gzip, deflate, sdch // Accept-Language:zh-CN,zh;q=0.8 // Connection:keep-alive // Content-Length:0 // Content-Type:text/plain;charset=UTF-8 // Cookie:JSESSIONID=D9C4A515CACAC31211D1612039D062B7 // header-A:A // header-B:b // X-Ca-Key:29666671 // X-Ca-Signature-Headers:header-a,header-b,x-ca-key,x-ca-timestamp // X-Ca-Timestamp:1479968678000 // X-Requested-With:XMLHttpRequest /*Query*/ // qa:a // qb:B /*BodyForm*/ // a-body:a // x-body:x /*签名字符串*/ String stringToSign = "POST\n" + "*/*\n" + "text/plain;charset=UTF-8\n" + "header-a:A\n" + "header-b:b\n" + "x-ca-key:29666671\n" + "x-ca-timestamp:1479968678000\n" + "/artemis/api/example?a-body=a&qa=a&qb=B&x-body=x"; // 请求数据中的 “appSecret” String secret = "empsl21ds3"; //javax.crypto.Mac 加密解密(消息认证码) Mac hmacSHA256 = javax.crypto.Mac.getInstance("HmacSHA256"); byte[] keyBytes = secret.getBytes("UTF-8"); hmacSHA256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA256")); // java.util.Base64 方式 String sign1 = Base64.getEncoder().encodeToString(hmacSHA256.doFinal(stringToSign.getBytes("UTF-8"))); // org.apache.commons.codec.binary.Base64 方式 byte[] bytes = org.apache.commons.codec.binary.Base64.encodeBase64(hmacSHA256.doFinal(stringToSign.getBytes("UTF-8"))); String sing2 = new String(bytes, "UTF-8"); /** * 官方签名结果:JRpUpk1ETjzr5gsbo4qoEA9EiQPejvNz12B837xV5HI= */ System.out.println(sign1); // JRpUpk1ETjzr5gsbo4qoEA9EiQPejvNz12B837xV5HI= System.out.println(sing2); // JRpUpk1ETjzr5gsbo4qoEA9EiQPejvNz12B837xV5HI= } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
二:access_token认证
(未看,待续)
标签:body,UTF,String,威视,ca,安防,海康,header,Ca From: https://www.cnblogs.com/lucky-jun/p/16600576.html