Sfoglia il codice sorgente

接口验签加密算法修改

xushengqiang 4 anni fa
parent
commit
7196ad6ab4

+ 5 - 0
pom.xml

@@ -156,6 +156,11 @@
 			<artifactId>jave-native-win64</artifactId>
 			<version>2.4.4</version>
 		</dependency>
+		<dependency>
+			<groupId>commons-codec</groupId>
+			<artifactId>commons-codec</artifactId>
+			<version>1.12</version>
+		</dependency>
 	</dependencies>
 
 	<dependencyManagement>

+ 3 - 6
src/main/java/cn/efunbox/audio/controller/DeviceController.java

@@ -239,7 +239,7 @@ public class DeviceController {
     public void tokenV2(HttpServletRequest request, HttpServletResponse response){
         String idChannel = request.getParameter("idChannel");
         String idDevice = request.getParameter("idDevice");
-        String ts = request.getParameter("ts");
+        String ts = request.getParameter("x-ts");
         String sign = request.getParameter("sign");
 
         if(StringUtils.isBlank(idChannel) || StringUtils.isBlank(idDevice) || StringUtils.isBlank(sign)){
@@ -249,13 +249,10 @@ public class DeviceController {
         Map<String,String> param = new HashMap<>();
         param.put("idChannel",idChannel);
         param.put("idDevice",idDevice);
-        param.put("ts",ts);
+        param.put("x-ts",ts);
         String signStr = Encrypt.createHMACSHA256(param,signKey);
 
-        String decode = new String(Base64.getDecoder().decode(sign), StandardCharsets.UTF_8);
-
-
-        if (!signStr.equalsIgnoreCase(decode)) {
+        if (!signStr.equalsIgnoreCase(sign)) {
             HttpUtil.responseApiCode(request, response, ApiCode.SIGN_FAIL);
             return;
         }

+ 17 - 22
src/main/java/cn/efunbox/audio/utils/Encrypt.java

@@ -2,6 +2,7 @@ package cn.efunbox.audio.utils;
 
 import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.binary.Hex;
 import org.apache.commons.lang.StringUtils;
 
@@ -21,6 +22,13 @@ public class Encrypt {
 
     public static final String SIGN = "sign";
 
+    private static Base64 base64;
+
+    static {
+        base64 = new Base64();
+    }
+
+
     public static String createSHA256Sign(Map<String, String> paramMap, String signKey) {
 
         List<String> sortedKeys = new ArrayList<String>();
@@ -164,36 +172,23 @@ public class Encrypt {
 
         byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
 
-        StringBuilder sb = new StringBuilder();
+        String encodeSign = base64.encodeToString(array);
 
-        for (byte item : array) {
-
-            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
-
-        }
-
-        return sb.toString().toUpperCase();
+        return encodeSign;
 
     }
 
 
     public static void main(String[] args) {
+
+        //idChannel=100103&idDevice=10.31.49.16&ts=159559185715
         Map<String,String> param = new HashMap<>();
-        param.put("idChannel","1000");
-        param.put("idDevice","test");
-        param.put("ts",System.currentTimeMillis() / 1000 + "");
-        System.out.println(JSONObject.toJSONString(param));
-//        String sign = Encrypt.createHMACSHA256(param,"IhOTiTyMLDNNLFuP");
-        String sign = Encrypt.createHMACSHA256(param,"AzaSB2RR0boUz1WQ");
+        param.put("idChannel","100103");
+        param.put("idDevice","10.31.49.16");
+        param.put("x-ts","159559185715");
+        log.info(JSONObject.toJSONString(param));
+        String sign = Encrypt.createHMACSHA256(param,"IhOTiTyMLDNNLFuP");
         log.info(sign);
 
-        String encode = Base64.getEncoder().encodeToString(sign.getBytes(StandardCharsets.UTF_8));
-
-        log.info(encode);
-
-        String decode = new String(Base64.getDecoder().decode(encode), StandardCharsets.UTF_8);
-        log.info(decode);
-
-
     }
 }

+ 2 - 2
src/main/resources/application-dev.properties

@@ -66,8 +66,8 @@ ali.oss.accessKeySecret=YEm1VebbntRIGmV8s8N33LQfOoC2sA
 aliyun.oss.file.prefix=audio/album/
 efunbox.oss.img.url=https://ai-admin-image.ai160.com
 
-#interface.sign.key=IhOTiTyMLDNNLFuP
-interface.sign.key=AzaSB2RR0boUz1WQ
+interface.sign.key=IhOTiTyMLDNNLFuP
+#interface.sign.key=AzaSB2RR0boUz1WQ
 
 hag.cdn.secret.sign.key=7q28cAwmcGQgOsCj
 hag.oss.audio.prefix=https://ai-hw-hag.ai160.com