fastDFS防盗链踩坑

今天搞一个小东西的时候用到了fastDFS的防盗链功能。遇到了一些坑,过来记录一下。关于fastDFS的配置看之前的内容。

1. 开启fastDFS防盗链功能:(直接贴我的配置文件了)

vim /etc/fdfs/http.conf

# HTTP default content type
http.default_content_type = application/octet-stream

# MIME types mapping filename
# MIME types file format: MIME_type  extensions
# such as:  image/jpeg  jpeg jpg jpe
# you can use apache's MIME file: mime.types
http.mime_types_filename=mime.types

# if use token to anti-steal
# default value is false (0)
http.anti_steal.check_token=true		# 开启防盗链token验证功能,默认是false

# token TTL (time to live), seconds
# default value is 600
http.anti_steal.token_ttl=900			# token验证过期时间

# 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
http.anti_steal.secret_key=sda1_hacker_	# 加密用的密码 -- 不要透漏给别人

# return the content of the file when check token fail
# default value is empty (no file sepecified)
http.anti_steal.token_check_fail=/usr/local/FastDFS/data/error.jpg	# 验证失败的时候访问的内容

配置完毕之后重启一下服务:
	fdfs_trackerd /etc/fdfs/tracker.conf restart
	fdfs_storaged /etc/fdfs/storage.conf restart
	nginx -s stop
	nginx


2. nginx配置: (写正则也可以 -- 如果修改过记得重启nginx)

location /group1/M00/ {
   ngx_fastdfs_module;
}


3. 引入java相关的依赖:(主要是使用ProtoCommon.getToken() 这个函数)

<dependency>
        <groupId>com.github.penggle</groupId>
        <artifactId>fastdfs-client-java</artifactId>
        <version>1.27</version>
</dependency>


4. fdfs_client.conf: 放在resources目录下

connect_timeout = 60
network_timeout = 60
charset = UTF-8
http.anti_steal_token = yes	
http.secret_key = sda1_hacker_			# 加密使用的密码
tracker_server = baseCentos:22122	


5. 工具类:

public static String getATCUrl(String url) {

    String substring = url.substring(url.indexOf("/")+1); // M00/00/00/wKidgV_Xf86AMYSLAAAAB5qB7vI08_big.html
    //unix时间戳 秒
    int ts = (int) (System.currentTimeMillis() / 1000);
    // 加密使用的密码
    String secret_key = "sda1_hacker_";
    String token = new String();
    try {
        token = ProtoCommon.getToken(substring, ts, secret_key);
    } catch (Exception e) {
        e.printStackTrace();
    }

    StringBuilder sb = new StringBuilder("http://baseCentos/group1");
    sb.append(url);
    sb.append("?token=").append(token);
    sb.append("&ts=").append(ts);
    return sb.toString();
}


6. 测试:

@org.junit.Test
public void ATLTest(){

    // 传递的url一定要按照这种形式 -- 不然生成的token不匹配
    String url = "/M00/00/00/wKidgV_YGKeAAUeCAAAACBj3piE484_big.txt";
    String resUrl = AtcSecurityUtils.getATCUrl(url);
    System.out.println(resUrl);
    // http://192.168.157.129/group1/M00/00/00/wKidgV_YGKeAAUeCAAAACBj3piE484_big.txt?token=82da62f06734d762e43fa7330f6ccd54&ts=1608006191
}

参考:
FastDFS防盗链

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值