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();
}
接下來就是測試結果了,一切正常。