下载安装包:
wget http://mod-auth-token.googlecode.com/files/mod_auth_token-1.0.6-beta.tar.gz
tar zxvf mod_auth_token-1.0.6-beta.tar.gz
cd mod_auth_token
rm -f configure
autoreconf -fi
automake -f
./configure --prefix=/usr && make && make install
编辑apache的配置文件httpd.conf
AuthTokenSecret "secret string"
AuthTokenPrefix /downloads/
AuthTokenTimeout 60
AuthTokenLimitByIp on
php文件
Python:
#!/usr/bin/env python
import os, time, hashlib
secret = "secret string" # Same as AuthTokenSecret
protectedPath = "/downloads/" # Same as AuthTokenPrefix
ipLimitation = False # Same as AuthTokenLimitByIp
hexTime = "{0:x}".format(int(time.time())) # Time in Hexadecimal
fileName = "/file_to_protect.txt" # The file to access
# Let's generate the token depending if we set AuthTokenLimitByIp
if ipLimitation:
token = hashlib.md5(''.join([secret, fileName, hexTime, os.environ["REMOTE_ADDR"]])).hexdigest()
else:
token = hashlib.md5(''.join([secret, fileName, hexTime])).hexdigest()
# We build the url
url = ''.join([protectedPath, token, "/", hexTime, fileName])
print url
Perl:
#!/usr/bin/perl
use strict;
use warnings;
print protectedPath('SecretWord', '/downloads/', 1, 'file.zip' );
sub protectedPath {
use Digest::MD5 qw/md5_hex/;
my ( $secret, $protectedPath, $ipLimitation, $fileName ) = @_;
my $hexTime = sprintf("%x", time() );
my $token = md5_hex($secret . $fileName. $hexTime);
$token = md5_hex($secret . $fileName . $hexTime . $ENV{'REMOTE_ADDR'}) if $ipLimitation;
return $protectedPath . $token. '/' . $hexTime . '/' . $fileName;
}
JAVA:
String secret="secret string"; // Same as AuthTokenSecret
String protectedPath="/vod/"; // Same as AuthTokenPrefix
//boolean ipLimitation=false; // Same as AuthTokenLimitByIp
long time= (new Date()).getTime(); // Time in decimal
time=time/1000; // timestamp of java is longer than PHP
String hexTime =Long.toHexString(time); // hexTime in Hexadecimal
String token =getMD5( (secret+ filePathName + hexTime).getBytes());
return protectedPath +token+"/"+hexTime+ filePathName;
public String getMD5(byte[] source) {
String s = null;
char hexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
try {
java.security.MessageDigest md = java.security.MessageDigest
.getInstance("MD5");
md.update(source);
byte tmp[] = md.digest();
char str[] = new char[16 * 2];
int k = 0;
for (int i = 0; i < 16; i++) {
byte byte0 = tmp[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
s = new String(str);
} catch (Exception e) {
e.printStackTrace();
}
return s;
}