nginx添加key访问限制并且对链接加超时失效策略

实现key访问限制,nginx通过accessKey来实现。
具体参考:

实现链接的超时失效策略,nginx通过Secure Link来实现。
具体参考:

由于网上说的实现代码都是基于PHP的,所以在这我用JAVA实现了下。

<?php
$secret = 'password'; # 密钥
$path = '/download/she.flv'; # 下载文件
$ipkey= md5("password".$_SERVER['REMOTE_ADDR']); #加密IP
# 下载到期时间,time是当前时间,300表示300秒,也就是说从现在到300秒之内文件不过期
$expire = time()+300;
# 用文件路径、密钥、过期时间生成加密串
$md5 = base64_encode(md5($secret . $path . $expire, true));
$md5 = strtr($md5, '+/', '-_');
$md5 = str_replace('=', '', $md5);
# 加密后的下载地址
echo '<a href=http://s1.xsdou.com/download/she.flv?key='.$ipkey.'&st='.$md5.'&e='.$expire.'>she.flv</a>';
echo '<br>http://s1.xsdou.com/download/she.flv?key='.$ipkey.'&st='.$md5.'&e='.$expire;
?>
public static void main(String[] args) {
String secret = "password";
String path = "/mp4files/1162000000062C6B/221.194.64.19/2/45a594b6938698f63b5771c12ff5b912.mp4";
String expire = String.valueOf(System.currentTimeMillis() / 1000 + 600);
String remoteIp = "192.168.1.145";String ipkey = DigestUtils.md5Hex(secret + remoteIp);
String st = new sun.misc.BASE64Encoder().encode(DigestUtils.md5(secret + path + expire)).replaceAll("\\+/", "-_").replaceAll("=", "");

System.out.println("ipkey: " + ipkey);
System.out.println("st: " + st);
System.out.println("e: " + expire);
}

发表评论