FastDFS扩展模块内置了通过token来实现防盗链的功能。开启防盗链后,访问文件是需要在url中加两个参数:token和ts。ts为时间戳,token为系统根据时间戳和密码生成的信物。为了系统的安全,下面一起来开启防盗链吧!

1. 配置http访问

1.1 开启防盗链检查

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 # 修改1,开启防盗链检查 # 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=123456 # 修改2,防盗链密码 # return the content of the file when check token fail
# default value is empty (no file sepecified)
http.anti_steal.token_check_fail=/root/error.jpg # 修改3,配置拒绝访问后显示的图片,需要是个有效可访问的图片 # if support multi regions for HTTP Range
# default value is true
http.multi_range.enabed = true

1.2 重启nginx

service nginx restart
# 或
nginx -s reload

1.3 验证

  1. 没有开启防盗链,文件可以正常访问:

  2. 成功开启防盗链后,访问文件时携带了错误的token,文件不能访问并且显示访问出错的图片

  3. 携带正确的token,效果已经达到,只要保证密码不被泄露,我们的文件就是相对安全的

2. 开发服务端代码修改

2.1 fdfs_client.conf配置

http.anti_steal_token = true  # 启动防盗链
http.secret_key = 123456 # 防盗链密码 tracker_server=192.168.56.10:22122
tracker_server=192.168.56.11:22122

 

2.2 服务器端

服务器端为文件访问生成token
remoteFilename:不能加group1(group name)

package com.aixin.tuna.fdfs;

import org.csource.common.MyException;
import org.csource.fastdfs.ProtoCommon; import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException; /**
* Created by dailin on 2018/6/12.
*/
public class FdfsFDL {
public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, MyException {
String fileName = "M00/00/00/wKg4C1tFmTWAFPKBAADdeFFxlXA240.png";
String host = "http://192.168.56.10:8888";
String secretKey = "";
String sourceUrl = getSourceUrl(fileName, host, secretKey);
System.out.println(sourceUrl);
} /**
* 生成防盗链token
* @param remoteFilename 文件路径,不带group:M00/00/00/wKg4C1tFmTWAFPKBAADdeFFxlXA240.png
* @param httpHost 文件服务器web访问地址
* @param secretKey 密码
* @return
* @throws UnsupportedEncodingException
* @throws NoSuchAlgorithmException
* @throws MyException
*/
public static String getSourceUrl(String remoteFilename, String httpHost,String secretKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, MyException {
int lts = (int)(System.currentTimeMillis() / 1000);
String token = ProtoCommon.getToken(remoteFilename, lts, secretKey); //初始化secret_key
return httpHost + "/" + remoteFilename + "?token=" + token + "&ts=" + lts;
}
}

得到

http://192.168.56.10:8888/M00/00/00/wKg4C1tFmTWAFPKBAADdeFFxlXA240.png?token=2fd428c6acc14126239e3a7d7d1d872b&ts=153

FastDFS防盗链的更多相关文章

  1. 实现fastdfs防盗链功能

    目录 1.背景 2.实现原理 2.1 开启防盗链 2.2 重启 nginx 2.3 Java代码生成token 1.token生成规则 2.java生成token 3.测试 3.1 带正确token访 ...

  2. SpringBoot集成FastDFS+Nginx整合基于Token的防盗链

    为什么要用SpringBoot? SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...

  3. JAVA防盗链在报表中的应用实例

    今天我们来聊聊Java防盗链,多说无用,直接上应用案例. 这里所用的工具是报表软件FineReport,搭配有决策系统(一个web前端展示系统,主要用于权限控制),可以采用java防盗链的方式来实现页 ...

  4. nginx secure_link下载防盗链

    下载服务器上有众多的软件资源, 可是很多来源不是本站,是迅雷.flashget, 源源不断的带宽,防盗链绝对是当务之急. 使用来源判断根本不靠谱,只能防止一些小白站点的盗链,迅雷之类的下载工具完全无效 ...

  5. Nginx中防盗链(下载防盗链和图片防盗链)操作记录

    日常运维工作中,设置防盗链的需求会经常碰到,这也是优化网站的一个必要措施.今天在此介绍Nginx中设置下载防盗链和图片防盗链的操作~ 一.Nginx中下载防盗链的操作记录对于一些站点上的下载操作,有很 ...

  6. Code笔记 之:防盗链(图片)

    图片防盗链   参考:http://bbs.csdn.net/topics/330080045    应该是”10种图片防盗的方法“,而不是”10种图片防盗链的方法“,不过看搜索防盗链的人要多一点,所 ...

  7. nginx配置图片防盗链

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${ expires 30d; access_log off; valid_referers none blocked ...

  8. php中防盗链使用.htaccess

    下面开始讲解:比如你的图片都在img目录下,那就在该目录下放一个名为 .htaccess 的文件,内容如下: php代码: 以下为引用的内容:RewriteEngine onRewriteCond % ...

  9. 使用Apache的.htaccess就可以防盗链

    Apache的.htaccess可以实现很多功能,如密码保护.禁止显示目录列表.阻止/允许特定的IP地址.实现网址的301 重定向等等.本文就来说说使用Apache的.htaccess如何防盗链. 当 ...

随机推荐

  1. 微信小程序隐藏滚动条

    全局wxss中添加以下样式,可以隐藏所有的滚动条: 包括使用scroll-view组件或者使用overflow-y:scroll;而出现的滚动条: 无论竖向横向滚动条都可隐藏: ::-webkit-s ...

  2. 基于bootstrap table配置的二次封装

    准备 jQuery js css 引用完毕 开始 如果对bootstrap table 的方法与事件不熟悉: Bootstrap table方法,Bootstrap table事件 <table ...

  3. 六、Python-字符串编码

      最早的编码为ASCII码(包含0-9.A-Z.a-z.符号(空格.制表符等)),最多支持256个符号(每个符号占1字节) GBK/GB2312:我国制定的中文编码标准,一个字节表示因为字母,两个字 ...

  4. 【学习】python文件读写,用with open as的好处,非常好【转载】

    原文链接:http://www.cnblogs.com/ymjyqsx/p/6554817.html 备注:博主还有很多值得学习的笔记,遇到问题可以拜读,非常感谢博主的总结 读写文件是最常见的IO操作 ...

  5. Android App专项测试(压力测试)

    转载https://blog.csdn.net/qq_29794757/article/details/64160303 转载https://blog.csdn.net/xuejiaodream/ar ...

  6. for循环,while循环,do while循环

    for循环: for循环格式: for(初始化语句;判断条件语句;控制条件语句) { 循环体语句; } 例子:取五位数各个位数的练习 public static void main(String[] ...

  7. 小白安装openvas总结-原创20181213

    先对该工具进行熟悉: OpenVAS 介绍 1.关于OpenVAS OpenVAS(Open Vulnerability Assessment System)是一套开源的漏洞扫描系统,早期Nessus ...

  8. C# 打印 长字符串自动换行

    主要代码如下: StringFormat fmt = new StringFormat(); fmt.LineAlignment = StringAlignment.Near;//左对齐 fmt.Fo ...

  9. Day02 - Ruby比一比:Module模块与Class类别

    前情提要 在第一天里,我很激昂地用Ruby的类别.物件.方法,写了宣言! class TingIsIronman def initialize @message =“I'm going to writ ...

  10. 基于WebGL架构的3D可视化平台ThingJS-搭建设备管理系统

    国内高层建筑不断兴建,它的特点是高度高.层数多.体量大.面积可达几万平方米到几十万平方米.这些建筑都是一个个庞然大物,高高的耸立在地面上,这是它的外观,而随之带来的内部的建筑设备也是大量的.为了提高设 ...