提交修改
This commit is contained in:
parent
1cec00eee0
commit
b664c0c1a2
@ -0,0 +1,80 @@
|
||||
package com.heyu.api.alibaba;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class AliyunCDNAuthA {
|
||||
|
||||
|
||||
/**
|
||||
* 生成阿里云CDN鉴权方式A的加密URL
|
||||
*
|
||||
* @param filePath 文件路径,以/开头,如 "/video/test.mp4"
|
||||
* @param expiresInSeconds 过期时间(从当前算起的秒数)
|
||||
* @return 完整的加密URL
|
||||
*/
|
||||
public static String generateAuthUrl(String privateKey,String domainName,String filePath, long expiresInSeconds) {
|
||||
try {
|
||||
// 1. 生成时间戳(当前时间 + 过期时间)
|
||||
long timestamp = (System.currentTimeMillis() / 1000) + expiresInSeconds;
|
||||
|
||||
// 2. 生成随机数(去掉UUID中的短横线)
|
||||
String rand = UUID.randomUUID().toString().replace("-", "");
|
||||
|
||||
// 3. 设置uid为0
|
||||
String uid = "0";
|
||||
|
||||
// 4. 计算md5hash
|
||||
String md5hash = calculateMd5Hash(privateKey,filePath, timestamp, rand, uid);
|
||||
|
||||
// 5. 构建鉴权URL
|
||||
return "http://" + domainName + filePath +
|
||||
"?auth_key=" + timestamp + "-" + rand + "-" + uid + "-" + md5hash;
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("生成鉴权URL失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算MD5哈希值
|
||||
*/
|
||||
private static String calculateMd5Hash(String privateKey,String filePath, long timestamp, String rand, String uid)
|
||||
throws NoSuchAlgorithmException {
|
||||
// 构造签名字符串
|
||||
String signString = filePath + "-" + timestamp + "-" + rand + "-" + uid + "-" + privateKey;
|
||||
|
||||
// 计算MD5
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] digest = md.digest(signString.getBytes());
|
||||
|
||||
// 转换为十六进制字符串
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : digest) {
|
||||
String hex = Integer.toHexString(0xff & b);
|
||||
if (hex.length() == 1) {
|
||||
hexString.append('0');
|
||||
}
|
||||
hexString.append(hex);
|
||||
}
|
||||
|
||||
return hexString.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用示例
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// 初始化鉴权对象
|
||||
String privateKey = "heyuquyixiao123456"; // 替换为你的密钥
|
||||
String domainName = "heyuimage.ihzhy.com"; // 替换为你的CDN域名
|
||||
|
||||
|
||||
// 生成鉴权URL,有效时间30分钟(1800秒)
|
||||
String filePath = "/ccc.jpeg";
|
||||
String authUrl = AliyunCDNAuthA.generateAuthUrl(privateKey,domainName,filePath, 1800);
|
||||
|
||||
System.out.println("生成的鉴权URL: " + authUrl);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package com.heyu.api.controller.vv;
|
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class AppOssAuthController {
|
||||
|
||||
|
||||
@RequestMapping("/auth")
|
||||
public HttpServletResponse auth(HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
String uri = request.getRequestURI();
|
||||
|
||||
log.info("AppOssAuthController uri:{}",uri);
|
||||
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user