diff --git a/ruoyi-modules/ruoyi-file/pom.xml b/ruoyi-modules/ruoyi-file/pom.xml index aef3ed06..3cd5dbc6 100644 --- a/ruoyi-modules/ruoyi-file/pom.xml +++ b/ruoyi-modules/ruoyi-file/pom.xml @@ -16,25 +16,25 @@ - + com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery - + com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config - + com.alibaba.cloud spring-cloud-starter-alibaba-sentinel - + org.springframework.boot @@ -65,7 +65,12 @@ com.github.tobato fastdfs-client - + + net.oschina.zcx7878 + fastdfs-client-java + 1.27.0.0 + + io.minio @@ -161,5 +166,5 @@ - + \ No newline at end of file diff --git a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/config/FastDfsConfig.java b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/config/FastDfsConfig.java index 9ddf178b..2f328449 100644 --- a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/config/FastDfsConfig.java +++ b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/config/FastDfsConfig.java @@ -14,6 +14,7 @@ import org.springframework.context.annotation.Configuration; * FastDFS配置 其他参数见:{@link PooledConnectionFactory} * * 使用: Docker部署FastDFS(附示例代码) https://www.cnblogs.com/cao-lei/p/13470695.html + * github 地址:https://github.com/tobato/FastDFS_Client */ @RefreshScope @Configuration @@ -26,7 +27,13 @@ public class FastDfsConfig { * FastDFS配置 其他参数见:{@link PooledConnectionFactory} * //@Value("${fdfs.domain}") */ - public String domain; + private String domain; + + /** + * 生成防盗链token的加密key;注意保密 + * fastdfs 内置的功能,需要和 FastDFS 【etc/fdfs/http.conf】 【http.anti_steal.secret_key】保持一致 + */ + private String tokenSecretKey; public String getDomain() { return domain; @@ -35,4 +42,12 @@ public class FastDfsConfig { public void setDomain(String domain) { this.domain = domain; } + + public String getTokenSecretKey() { + return tokenSecretKey; + } + + public void setTokenSecretKey(String tokenSecretKey) { + this.tokenSecretKey = tokenSecretKey; + } } diff --git a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsServiceImpl.java b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsServiceImpl.java index c5052ed4..23c00f54 100644 --- a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsServiceImpl.java +++ b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsServiceImpl.java @@ -1,11 +1,11 @@ package com.ruoyi.file.service; -import com.github.tobato.fastdfs.domain.fdfs.MetaData; import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException; import com.ruoyi.common.core.exception.CustomException; import com.ruoyi.file.config.FastDfsConfig; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; +import org.csource.fastdfs.ProtoCommon; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -15,14 +15,13 @@ import org.springframework.web.multipart.MultipartFile; import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.service.FastFileStorageClient; -import java.util.HashSet; -import java.util.Set; /** * FastDFS 文件存储 * @author ruoyi * @see FastDfsConfig */ +@Primary @Service() public class FastDfsServiceImpl implements IDfsService { @@ -51,13 +50,10 @@ public class FastDfsServiceImpl implements IDfsService // fastdsf 这里的 modules 没用 validateModule(file, modules); - Set metaDataSet = new HashSet<>(1); - metaDataSet.add(new MetaData("groupName", "group1")); - StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), - FilenameUtils.getExtension(file.getOriginalFilename()), metaDataSet); + FilenameUtils.getExtension(file.getOriginalFilename()), null); - /// fileUrl = "http://127.0.0.1:22122/" + storePath.getFullPath(); + // 形如: http://47.99.175.191:8888/group1/M00/00/00/rBzzjWD-ec2ADLS9AAJiu1rRenk51.jpeg return fastDfsConfig.getDomain() + "/" + storePath.getFullPath(); } @@ -81,8 +77,31 @@ public class FastDfsServiceImpl implements IDfsService throw new CustomException("fastdfs-获取文件占用空间功能,敬请期待"); } + /** + * FastDFS防盗链 https://www.cnblogs.com/xiaolinstudy/p/9341779.html + * @param fileUrl 文件访问地址,全路径或者不是全路径都可以,eg: http://47.99.175.191:8888/group1/M00/00/00/rBzzjWD_eaqAFbHyAAJiu1rRenk96.jpeg + * 过期时间,需要在 fastdf的/etc/fdfs/http.con 配置,无法通过代码直接配置; + * @return 返回 带有防盗链token的url; eg: http://47.99.175.191:8888/group1/M00/00/00/rBzzjWD_eaqAFbHyAAJiu1rRenk96.jpeg?token=db86f940a963f6a6c10483c55c060a93&ts=1627355677 + */ @Override public String presignedUrl(String fileUrl) { - return fileUrl; + if (StringUtils.isBlank(fastDfsConfig.getTokenSecretKey())) { + throw new CustomException("防盗链生成token的密钥为空,请检查:tokenSecretKey"); + } + String tokenSecretKey = fastDfsConfig.getTokenSecretKey(); + + StorePath storePath = StorePath.parseFromUrl(fileUrl); + String keyPath = storePath.getPath(); + //时间戳 单位为秒 + int ts = (int) (System.currentTimeMillis() / 1000); + + String token; + try { + token = ProtoCommon.getToken(keyPath, ts, tokenSecretKey); + } catch (Exception e) { + throw new CustomException("FastDFS获取token异常"); + } + // 形如: http://47.99.175.191:8888/group1/M00/00/00/rBzzjWD-ec2ADLS9AAJiu1rRenk51.jpeg + return fastDfsConfig.getDomain() + "/" + storePath.getFullPath() + "?token=" + token + "&ts=" + ts; } } diff --git a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/IDfsService.java b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/IDfsService.java index d497b19e..ef7f32cd 100644 --- a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/IDfsService.java +++ b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/IDfsService.java @@ -76,6 +76,7 @@ public interface IDfsService * 3、qiniu ;七牛云存储; 下载凭证(如果Bucket设置成私有,必须要有 下载凭证),路径:【对象存储==》使用指南===》安全机制===》 下载凭证】 https://developer.qiniu.com/kodo/1202/download-token * https://developer.qiniu.com/kodo/5914/s3-compatible-sts * 4、腾讯 临时密钥(临时访问凭证) GetFederationToken 临时密钥生成及使用指引 https://cloud.tencent.com/document/product/436/14048?from=10680 + * 5、fastdfs 防掉链 前提,需要在 fastdfs上面配置 https://www.cnblogs.com/xiaolinstudy/p/9341779.html * @param fileUrl 文件访问地址,全路径或者不是全路径都可以 * @return 返回签名后的url */ diff --git a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/QiniuDfsServiceImpl.java b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/QiniuDfsServiceImpl.java index e42340ee..be466ea4 100644 --- a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/QiniuDfsServiceImpl.java +++ b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/QiniuDfsServiceImpl.java @@ -32,7 +32,7 @@ import java.io.InputStream; * * 构建时间戳防盗链访问链接: https://developer.qiniu.com/kodo/1239/java#fusion-antileech */ -@Primary +//@Primary @Service() public class QiniuDfsServiceImpl implements IDfsService { private static final Logger log = LoggerFactory.getLogger(QiniuDfsServiceImpl.class); diff --git a/ruoyi-modules/ruoyi-file/src/main/resources/bootstrap.yml b/ruoyi-modules/ruoyi-file/src/main/resources/bootstrap.yml index 5bd0f15c..967fa852 100644 --- a/ruoyi-modules/ruoyi-file/src/main/resources/bootstrap.yml +++ b/ruoyi-modules/ruoyi-file/src/main/resources/bootstrap.yml @@ -17,9 +17,11 @@ spring: discovery: # 服务注册地址 server-addr: 127.0.0.1:8848 + namespace: 72b686a1-d9f6-499f-8275-e481b664779e config: # 配置中心地址 server-addr: 127.0.0.1:8848 + namespace: 72b686a1-d9f6-499f-8275-e481b664779e # 配置文件格式 file-extension: yml # 共享配置 @@ -43,10 +45,11 @@ ftp: # 文件服务器之3 FastDFS配置 fdfs: - domain: http://8.129.231.12 soTimeout: 3000 - connectTimeout: 2000 - trackerList: 8.129.231.12:22122 + connectTimeout: 5000 + trackerList: 47.99.175.191:22122 + domain: http://47.99.175.191:8888 + token-secret-key: test@test # 文件服务器之4 Minio配置 minio: