首页 > 其他分享 >实现fastdfs防盗链功能

实现fastdfs防盗链功能

时间:2022-10-09 20:47:03浏览次数:84  
标签:功能 http fastdfs token length steal 防盗链

目录

1、背景

我们可以通过fastdfs实现一个分布式文件系统,如果我们的fastdfs部署在外网,那么任何一个人知道了我们的上传接口,那么它就可以文件的上传和访问。那么我们如何阻止他人访问我们fastdfs服务器上的文件呢?此处就需要使用fastdfs的防盗链功能。

2、实现原理

fastdfs的防盗链是通过token机制来实现的。当我们开启防盗链功能后,需要在url后增加2个额外的参数tokentstokents的生成都是需要在服务端。

2.1 开启防盗链

vim /etc/fdfs/http.conf

# true 表示开启防盗链
http.anti_steal.check_token = true
# token的过期时间,单位为秒
http.anti_steal.token_ttl = 60
# 密钥,不可泄漏,用于生成token
http.anti_steal.secret_key = thisisasecuritykey
# 当图片拒绝访问后,显示的图片,此图片需要可访问,不然可能会出现问题
http.anti_steal.token_check_fail = /data/fastdfs/401.jpg

http.anti_steal.token_check_fail 指定的图片需要可访问,否则可能会出现问题

2.2 重启 nginx

/usr/local/nginx/sbin/nginx -s reload

2.3 Java代码生成token

1、token生成规则

token = md5(文件ID+私钥+时间戳)

文件ID:不能包含group

group1/M00/00/00/wKh5iWNBl7-AKvj1AAAwWD4VeAg577.jpg
`需要替换成`
M00/00/00/wKh5iWNBl7-AKvj1AAAwWD4VeAg577.jpg

私钥:需要和 /etc/fdfs/http.conf 中的 http.anti_steal.secret_key 值一致
时间戳:单位秒

2、java生成token

/**
     * 生成token
     *
     * @param fileId          the filename return by FastDFS server,不能含有组
     * @param timestampSecond 时间戳 单位秒
     * @return token
     * @throws NoSuchAlgorithmException
     */
    private String generatorToken(String fileId, Long timestampSecond) throws NoSuchAlgorithmException {
        // 需要去掉 group
        fileId = fileId.substring(fileId.indexOf("/") + 1);
        byte[] bsFilename = fileId.getBytes(StandardCharsets.UTF_8);
        byte[] bsTimestamp = timestampSecond.toString().getBytes(StandardCharsets.UTF_8);
        // thisisasecuritykey 需要和 /etc/fdfs/http.conf 中的 http.anti_steal.secret_key 值一致
        byte[] bsKey = "thisisasecuritykey".getBytes(StandardCharsets.UTF_8);

        byte[] buff = new byte[bsFilename.length + bsKey.length + bsTimestamp.length];
        System.arraycopy(bsFilename, 0, buff, 0, bsFilename.length);
        System.arraycopy(bsKey, 0, buff, bsFilename.length, bsKey.length);
        System.arraycopy(bsTimestamp, 0, buff, bsFilename.length + bsKey.length, bsTimestamp.length);

        return md5(buff);
    }

    public static String md5(byte[] source) throws NoSuchAlgorithmException {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        md.update(source);
        byte tmp[] = md.digest();
        char str[] = new char[32];
        int k = 0;
        for (int i = 0; i < 16; i++) {
            str[k++] = hexDigits[tmp[i] >>> 4 & 0xf];
            str[k++] = hexDigits[tmp[i] & 0xf];
        }

        return new String(str);
    }

3、测试

3.1 带正确token访问

带正确token访问

3.2 带错误token访问

带错误token访问

这个地方返回的图片是 http.anti_steal.token_check_fail = /data/fastdfs/401.jpg 这个配置中配置的图片。

4、项目代码

https://gitee.com/huan1993/spring-cloud-parent/tree/master/springboot/springboot-fastdfs

5、参考链接

  1. 使用FastDFS的内置防盗链功能官方文章 http://bbs.chinaunix.net/thread-1916999-1-1.html
  2. fastdfs faq http://bbs.chinaunix.net/thread-1920470-1-1.html

标签:功能,http,fastdfs,token,length,steal,防盗链
From: https://www.cnblogs.com/huan1993/p/16773595.html

相关文章

  • 国庆学go,完成了博客基本功能,迫不及待的发布上线了
    大家好,我是沙漠尽头的狼。国庆7天,利用带娃之余的空闲时间学习了go,并做了一个不是很完善的博客前台网站。网站发布地址:​​https://go.dotnet9.com​​源码边做边上传Github,......
  • C++ 实现一个函数,函数完成如下功能: 1.函数的输入为一个数组,数组的成员个数不定(即:可能
    #include<iostream>#include<string>#include<windows.h>usingnamespacestd;boolfind_max_min(inta[],intlen,int*max,int*min){if(len<1)ret......
  • 界面组件DevExpress WinForms v22.1 - 数据可视化功能全新升级
    DevExpressWinForms拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office......
  • C#实现winform拖拽文本类型文件并读取内容功能
    利用winform窗体,或添加有属性AllowDrop的控件,可以实现拖拽文件文本类型文件并读取内容功能,txt、sql、html等类型文件都可以。这里利用Button控件,向窗体添加两个Button控件......
  • 绝对实用的功能,如何收藏网页图片?
    大家在上网浏览网页的过程中有没有收藏过网页上图片的经历呢?而大家是如何保存这些图片的呢?恐怕就是使用浏览器的“保存该图片”的功能了吧。今天给大家介绍一款插件,不需......
  • SpringBoot整合fastdfs
    1、背景在前一节中,我们搭建了一个单机版的fastdfs服务,此处我们将fastdfs与springboot进行整合,实现文件的上传和下载。2、整合步骤2.1、引入依赖<dependency><group......
  • SpringBoot整合fastdfs
    目录1、背景2、整合步骤2.1、引入依赖2.2、引入fastdfs配置2.3编写文件上传和下载接口2.4测试文件上传2.5文件下载3、参考文档1、背景在前一节中,我们搭建了一个单机版......
  • ubuntu开启远程桌面功能
    本文介绍ubuntu自带的xrdp工具进行远程桌面登陆。第一步:安装vnc服务sudoapt-getinstalltightvncserver1第二步:安装xrdp服务sudoapt-getinstallxrdp1PS:VNC与xrdp服......
  • Excel导航功能(Excel技巧集团)
    【视图】选项卡下多了个【导航】功能,可以快速定位工作表中的工作表、连续区域的数据、超级表、自定义名称、数据透视表、图形图片等非单元格对象。 ......
  • 数据链路层功能
    数据链路层的研究思想数据链路层基本概念结点:主机、路由器链路:网络中两个结点之间的物理通道,链路的传输介质主要有双绞线、光纤和微波。分为有线链路、无线链路。数据......