image.png
image.png
nginx配置
location /tmp {
#这里配置了2个参数一个是md5,一个是expires
secure_link $arg_md5,$arg_expires;
#md5的哈希格式为 time,uri,test
secure_link_md5 "$secure_link_expires$uri test";
#这里我们的md5是我们按照secure_link_md5的方式计算的哈希,secure_link会比对它计算的哈希值是否与我们的md5参数一致
if ($secure_link = "") {
#资源不存在或哈希比对失败
return 403;
}
if ($secure_link = "0") {
#失效超时
return 410;
}
#重命名文件名称
add_header Content-Disposition "attachment;filename=$arg_f";
}
image.png
java代码生成链接代码
pom.xml
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.12</version>
</dependency>
</dependencies>
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
/**
* 生成加密連接
*/
public class Main {
private static String site = "http://172.31.17.110:8081/tmp/";
private static String secret = "test";
public static String createLink(String path,String fileName){
String time = String.valueOf((System.currentTimeMillis() / 1000) + 300); // 5分钟有效
String md5 = Base64.encodeBase64URLSafeString(DigestUtils.md5(time+"/tmp/test.txt "+secret));
String url = site + path + "?md5=" + md5 + "&expires=" + time + "&f="+fileName;
return url;
}
public static void main(String[] args) {
System.out.println(createLink("test.txt","1232134213.txt"));
}
}
网友评论