FastDFS集羣環境搭建(六)FastDFS防盜鏈

FastDFS從4.05版本開始去除了內置HTTP,估計也是爲了安全考慮,通過查看GitHub上的HISTORY可以看到最後一點“* remove embed HTTP support”

Version 4.05  2012-12-30
* client/fdfs_upload_file.c can specify storage ip port and store path index
* add connection pool
* client load storage ids config
* common/ini_file_reader.c does NOT call chdir
* keep the mtime of file same
* use g_current_time instead of call time function
* remove embed HTTP support

但是我們爲了能夠集羣訪問,配置了Nginx,爲了防止外泄文件信息,就需要添加防盜鏈

修改配置sudo vim /etc/fdfs/http.conf

# if use token to anti-steal
# default value is false (0)
# 開啓防盜鏈
http.anti_steal.check_token=true

# token TTL (time to live), seconds
# default value is 600
# 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
# secret密碼
http.anti_steal.secret_key=your_own_secret_key

# return the content of the file when check token fail
# default value is empty (no file sepecified)
# 防盜鏈圖片地址
http.anti_steal.token_check_fail=/home/feige/fastdfs/fastdfs-5.11/conf/anti-steal.jpg

修改之後再去訪問試試,發現404了

要想再次訪問需要拼接帶Token和時間戳的URL,Java代碼如下:

private static String getSourceUrl(String remoteFilename, String httpHost,String secretKey) throws Exception {
    int lts = (int)(System.currentTimeMillis() / 1000);
    // 初始化secret_key
    String token = ProtoCommon.getToken(remoteFilename, lts, secretKey);
    return httpHost + "/group1/" + remoteFilename + "?token=" + token + "&ts=" + lts;
}

public static void main(String[] args) {
    try {
        String fileName = "M00/00/00/wKivgl1CrdqAMZb_AADeqP7ACwc568.png";
        String host = "http://192.168.175.129:8888";
        String secretKey = "your_own_secret_key";
        String sourceUrl = getSourceUrl(fileName, host, secretKey);
        System.out.println(sourceUrl);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章