Skip to content

Commit

Permalink
修改时间戳防盗链接口,老接口保留
Browse files Browse the repository at this point in the history
  • Loading branch information
sxci committed Mar 14, 2017
1 parent 34bb968 commit 3cb044a
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 34 deletions.
76 changes: 44 additions & 32 deletions src/main/java/com/qiniu/cdn/CdnManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
import com.qiniu.util.StringMap;
import com.qiniu.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -201,6 +198,30 @@ public CdnResult.LogListResult getCdnLogList(String[] domains, String logDate) t
return response.jsonToObject(CdnResult.LogListResult.class);
}

public static String createTimestampAntiLeechUrl(URL oUrl, String encryptKey, long deadline) throws QiniuException {
try {
String urlencodedPath = URLEncoder.encode(oUrl.getPath(), "UTF-8").replaceAll("%2F", "/");
String query = oUrl.getQuery();
String file = (query == null) ? urlencodedPath : (urlencodedPath + "?" + query);
URL url = new URL(oUrl.getProtocol(), oUrl.getHost(), oUrl.getPort(), file);
String expireHex = Long.toHexString(deadline);

String toSignStr = String.format("%s%s%s", encryptKey, urlencodedPath, expireHex);
String signedStr = StringUtils.md5Lower(toSignStr);

String signedUrl;
if (url.getQuery() != null) {
signedUrl = String.format("%s&sign=%s&t=%s", url, signedStr, expireHex);
} else {
signedUrl = String.format("%s?sign=%s&t=%s", url, signedStr, expireHex);
}

return signedUrl;
} catch (Exception e) {
throw new QiniuException(e, "timestamp anti leech failed");
}
}

/**
* 构建七牛标准的基于时间戳的防盗链
* 参考文档:<a href="https://support.qiniu.com/question/195128">时间戳防盗链</a>
Expand All @@ -214,37 +235,28 @@ public CdnResult.LogListResult getCdnLogList(String[] domains, String logDate) t
*/
public static String createTimestampAntiLeechUrl(
String host, String fileName, final StringMap queryStringMap, String encryptKey, long deadline)
throws UnsupportedEncodingException, MalformedURLException, NoSuchAlgorithmException {
String urlToSign;
String encodedFileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
if (queryStringMap != null && queryStringMap.size() > 0) {
List<String> queryStrings = new ArrayList<String>();
for (Map.Entry<String, Object> entry : queryStringMap.map().entrySet()) {
StringBuilder queryStringBuilder = new StringBuilder();
queryStringBuilder.append(URLEncoder.encode(entry.getKey(), "utf-8"));
queryStringBuilder.append("=");
queryStringBuilder.append(URLEncoder.encode(entry.getValue().toString(), "utf-8"));
queryStrings.add(queryStringBuilder.toString());
throws QiniuException {
URL urlObj = null;
try {
String urlToSign = null;
if (queryStringMap != null && queryStringMap.size() > 0) {
List<String> queryStrings = new ArrayList<String>();
for (Map.Entry<String, Object> entry : queryStringMap.map().entrySet()) {
StringBuilder queryStringBuilder = new StringBuilder();
queryStringBuilder.append(URLEncoder.encode(entry.getKey(), "utf-8"));
queryStringBuilder.append("=");
queryStringBuilder.append(URLEncoder.encode(entry.getValue().toString(), "utf-8"));
queryStrings.add(queryStringBuilder.toString());
}
urlToSign = String.format("%s/%s?%s", host, fileName, StringUtils.join(queryStrings, "&"));
} else {
urlToSign = String.format("%s/%s", host, fileName);
}
urlToSign = String.format("%s/%s?%s", host, encodedFileName, StringUtils.join(queryStrings, "&"));
} else {
urlToSign = String.format("%s/%s", host, encodedFileName);
}

URL urlObj = new URL(urlToSign);
String path = urlObj.getPath();
String expireHex = Long.toHexString(deadline);

String toSignStr = String.format("%s%s%s", encryptKey, path, expireHex);
String signedStr = StringUtils.md5Lower(toSignStr);

String signedUrl;
if (urlObj.getQuery() != null) {
signedUrl = String.format("%s&sign=%s&t=%s", urlToSign, signedStr, expireHex);
} else {
signedUrl = String.format("%s?sign=%s&t=%s", urlToSign, signedStr, expireHex);
urlObj = new URL(urlToSign);
} catch (Exception e) {
throw new QiniuException(e, "timestamp anti leech failed");
}

return signedUrl;
return createTimestampAntiLeechUrl(urlObj, encryptKey, deadline);
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/qiniu/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import qiniu.happydns.util.Hex;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
Expand Down Expand Up @@ -138,9 +139,9 @@ public static String utf8String(byte[] data) {
return new String(data, Constants.UTF_8);
}

public static String md5Lower(String src) throws UnsupportedEncodingException, NoSuchAlgorithmException {
public static String md5Lower(String src) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(src.getBytes("utf-8"));
digest.update(src.getBytes(Charset.forName("UTF-8")));
byte[] md5Bytes = digest.digest();
return Hex.encodeHexString(md5Bytes);
}
Expand Down
93 changes: 93 additions & 0 deletions src/test/java/com/qiniu/CdnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.junit.Assert;
import org.junit.Test;

import java.net.MalformedURLException;
import java.net.URL;

/**
* Created by bailong on 16/9/21.
*/
Expand Down Expand Up @@ -164,4 +167,94 @@ public void testCreateTimestampAntiLeechUrlWithQueryString2() {
Assert.fail();
}
}

@Test
public void testCreateTimestampAntiLeechUrlSimple3() throws MalformedURLException {
String host = "http://xxx.yyy.com";
String fileName = "DIR1/dir2/vodfile.mp4";
StringMap queryStringMap = new StringMap();
queryStringMap.put("name", "七牛");
queryStringMap.put("year", 2017);
queryStringMap.put("年龄", 28);
long deadline = 1438358400;
String encryptKey = "12345678";
String signedUrl;
try {
signedUrl = CdnManager.createTimestampAntiLeechUrl(host, fileName,
queryStringMap, encryptKey, deadline);
Assert.assertTrue(signedUrl, signedUrl.indexOf("19eb212771e87cc3d478b9f32d6c7bf9&t=55bb9b80") > -1);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail();
}
}

@Test
public void testCreateTimestampAntiLeechUrlSimple4() throws MalformedURLException {
String host = "http://xxx.yyy.com";
String fileName = "DIR1/中文/vodfile.mp4";
StringMap queryStringMap = new StringMap();
queryStringMap.put("name", "七牛");
queryStringMap.put("year", 2017);
queryStringMap.put("年龄", 28);
long deadline = 1438358400;
String encryptKey = "12345678";
String signedUrl;
try {
signedUrl = CdnManager.createTimestampAntiLeechUrl(host, fileName,
queryStringMap, encryptKey, deadline);
Assert.assertTrue(signedUrl, signedUrl.indexOf("sign=6356bca0d2aecf7211003e468861f5ea&t=55bb9b80") > -1);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail();
}
}

@Test
public void testCreateTimestampAntiLeechUrlSimple5() throws MalformedURLException {
URL url = new URL("http://xxx.yyy.com/DIR1/dir2/vodfile.mp4");
long deadline = 1438358400;
String encryptKey = "12345678";
String signedUrl;
String ret = "http://xxx.yyy.com/DIR1/dir2/vodfile.mp4?sign=19eb212771e87cc3d478b9f32d6c7bf9&t=55bb9b80";
try {
signedUrl = CdnManager.createTimestampAntiLeechUrl(url, encryptKey, deadline);
Assert.assertEquals(ret, signedUrl);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail();
}
}

@Test
public void testCreateTimestampAntiLeechUrlSimple6() throws MalformedURLException {
URL url = new URL("http://xxx.yyy.com/DIR1/dir2/vodfile.mp4?v=1.1");
long deadline = 1438358400;
String encryptKey = "12345678";
String signedUrl;
String ret = "http://xxx.yyy.com/DIR1/dir2/vodfile.mp4?v=1.1&sign=19eb212771e87cc3d478b9f32d6c7bf9&t=55bb9b80";
try {
signedUrl = CdnManager.createTimestampAntiLeechUrl(url, encryptKey, deadline);
Assert.assertEquals(ret, signedUrl);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail();
}
}

@Test
public void testCreateTimestampAntiLeechUrlSimple7() throws MalformedURLException {
URL url = new URL("http://xxx.yyy.com/DIR1/中文/vodfile.mp4?v=1.2");
long deadline = 1438358400;
String encryptKey = "12345678";
String signedUrl;
String ret = "http://xxx.yyy.com/DIR1/%E4%B8%AD%E6%96%87/vodfile.mp4?v=1.2&sign=6356bca0d2aecf7211003e468861f5ea&t=55bb9b80";
try {
signedUrl = CdnManager.createTimestampAntiLeechUrl(url, encryptKey, deadline);
Assert.assertEquals(ret, signedUrl);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail();
}
}
}

0 comments on commit 3cb044a

Please sign in to comment.