浏览代码

'新增云点播签名接口'

Althars123 4 年之前
父节点
当前提交
f8441a8638

+ 3 - 0
zzjs-admin/src/main/java/com/miaxis/pc/controller/teachingVideo/TeachingVideoInfoController.java

@@ -123,4 +123,7 @@ public class TeachingVideoInfoController extends BaseController {
     }
 
 
+
+
+
 }

+ 18 - 2
zzjs-admin/src/main/java/com/miaxis/system/controller/common/CommonController.java

@@ -9,6 +9,7 @@ import com.miaxis.common.enums.FileUploadTypeEnum;
 import com.miaxis.common.exception.CustomException;
 import com.miaxis.common.utils.EnumUtils;
 import com.miaxis.common.utils.StringUtils;
+import com.miaxis.common.utils.VodSignature;
 import com.miaxis.common.utils.file.FileUtils;
 import com.miaxis.file.domain.FileInfo;
 import com.miaxis.file.service.IFileInfoService;
@@ -27,6 +28,7 @@ import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
@@ -46,8 +48,10 @@ public class CommonController
     @Autowired
     private IFileInfoService fileInfoService;
 
-//    @Resource
-//    private VodUploadClient vodUploadClient;
+
+    @Resource
+    VodSignature vodSignature;
+
 
     @Value("${cos.bucketName}")
     private String bucketName;
@@ -118,6 +122,18 @@ public class CommonController
         return Response.success(fileInfo);
     }
 
+
+
+    /**
+     * 获取云点播签名
+     */
+    @GetMapping(Constants.OPEN_PREFIX+"/common/vodSignature")
+    @ApiOperation("获取云点播签名")
+    public Response<String> getVodSignature () throws Exception {
+
+        return  Response.success(vodSignature.getUploadSignature());
+
+    }
     /**
      * 本地资源通用下载
      */

+ 40 - 0
zzjs-admin/src/test/java/com/miaxis/test/VodTest.java

@@ -0,0 +1,40 @@
+package com.miaxis.test;
+
+import com.alibaba.fastjson.JSONObject;
+import com.miaxis.ZzjsApplication;
+import com.miaxis.common.utils.VodSignature;
+import com.miaxis.feign.dto.WxSend;
+import com.miaxis.feign.service.IWxSendService;
+import com.miaxis.wx.service.IWxXcxMessageService;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.annotation.Resource;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+@SpringBootTest(classes = ZzjsApplication.class)
+@RunWith(SpringRunner.class)
+public class VodTest {
+
+    @Resource
+    VodSignature vodSignature;
+
+    /**
+     */
+    @Test
+    public void get() throws Exception {
+
+        String uploadSignature = vodSignature.getUploadSignature();
+        System.out.println(uploadSignature);
+    }
+
+
+
+
+
+}

+ 54 - 0
zzjs-common/src/main/java/com/miaxis/common/utils/VodSignature.java

@@ -0,0 +1,54 @@
+package com.miaxis.common.utils;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import sun.misc.BASE64Encoder;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import java.util.Random;
+
+@Component
+public class VodSignature {
+    @Value("${cos.secretId}")
+    private String secretId;
+    @Value("${cos.secretKey}")
+    private String secretKey;
+    private long effectTime =3600 * 24 * 2;
+    private static final String HMAC_ALGORITHM = "HmacSHA1"; //签名算法
+    private static final String CONTENT_CHARSET = "UTF-8";
+    public static byte[] byteMerger(byte[] byte1, byte[] byte2) {
+        byte[] byte3 = new byte[byte1.length + byte2.length];
+        System.arraycopy(byte1, 0, byte3, 0, byte1.length);
+        System.arraycopy(byte2, 0, byte3, byte1.length, byte2.length);
+        return byte3;
+    }
+    // 获取签名
+    public String getUploadSignature() throws Exception {
+        String strSign = "";
+        String contextStr = "";
+        // 生成原始参数字符串
+        long endTime = (System.currentTimeMillis() / 1000 + effectTime);// 签名有效期:2天
+        contextStr += "secretId=" + java.net.URLEncoder.encode(secretId, "utf8");
+        contextStr += "&currentTimeStamp=" + System.currentTimeMillis() / 1000;
+        contextStr += "&expireTime=" + endTime;
+        contextStr += "&random=" + new Random().nextInt(java.lang.Integer.MAX_VALUE);
+        try {
+            Mac mac = Mac.getInstance(HMAC_ALGORITHM);
+            SecretKeySpec secretKey = new SecretKeySpec(this.secretKey.getBytes(CONTENT_CHARSET), mac.getAlgorithm());
+            mac.init(secretKey);
+            byte[] hash = mac.doFinal(contextStr.getBytes(CONTENT_CHARSET));
+            byte[] sigBuf = byteMerger(hash, contextStr.getBytes("utf8"));
+            strSign = base64Encode(sigBuf);
+            strSign = strSign.replace(" ", "").replace("\n", "").replace("\r", "");
+        } catch (Exception e) {
+            throw e;
+        }
+        return strSign;
+    }
+    private String base64Encode(byte[] buffer) {
+        BASE64Encoder encoder = new BASE64Encoder();
+        return encoder.encode(buffer);
+    }
+
+}