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();
    }
}

你可能感兴趣的:(FastDFS)