FASTDFS防盗链

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

接下来就是测试结果了,一切正常。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值