1.首先要在server端修改配置参数
http.conf中防盗链相关的几个参数如下:
# cd /etc/fdfs
# vim /etc/fdfs/http.conf
# if use token to anti-steal
# default value is false (0)
# 是否做token检查,缺省值为false。
http.anti_steal.check_token=true
# token TTL (time to live), seconds
# default value is 600
# TTL,即生成token的有效时长(秒)
http.anti_steal.token_ttl=900
# secret key to generate anti-steal token
# this parameter must be set when http.anti_steal.check_token set to true
# the length of the secret key should not exceed 128 bytes
# 生成token的密钥,尽量设置得长一些,千万不要泄露出去
http.anti_steal.secret_key=FastDFS1234567890
# return the content of the file when check token fail
# default value is empty (no file sepecified)
# 检查失败,返回的文件内容,需指定本地文件名
http.anti_steal.token_check_fail=/etc/fdfs/error.png
参数意思很明了,跟一般的加密方式类似,有兴趣的可以去看源码的加密方式
秘钥加上时间戳。
依次重启 tracker、storage 和 nginx
2.修改客户端,此处以java为例
本人安装的是最新版的fasfdfs6.0.6
// 封装文件完整URL地址
ProtoCommon类找不到的话,需要新增jar
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
private String getResAccessUrl(String fid) {
//不需要group的名称,所以截取掉
String substring = fid.substring(fid.indexOf("/")+1);
//unix时间戳 以秒为单位
int ts = (int) (System.currentTimeMillis() / 1000);
String secret_key = "FastDFS1234567890";
String token = new String();
try {
token= ProtoCommon.getToken(substring, ts, secret_key);
} catch (Exception e) {
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
sb.append(realPath);
sb.append(fid);
sb.append("?token=").append(token);
sb.append("&ts=").append(ts);
return sb.toString();
}
接下来就是测试结果了,一切正常。