fastdfs增加防掉链的url 方法【presignedUrl】
This commit is contained in:
parent
407b7e526f
commit
a88298057f
|
|
@ -65,6 +65,11 @@
|
||||||
<groupId>com.github.tobato</groupId>
|
<groupId>com.github.tobato</groupId>
|
||||||
<artifactId>fastdfs-client</artifactId>
|
<artifactId>fastdfs-client</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.oschina.zcx7878</groupId>
|
||||||
|
<artifactId>fastdfs-client-java</artifactId>
|
||||||
|
<version>1.27.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 文件服务器2:Minio -->
|
<!-- 文件服务器2:Minio -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ import org.springframework.context.annotation.Configuration;
|
||||||
* FastDFS配置 其他参数见:{@link PooledConnectionFactory}
|
* FastDFS配置 其他参数见:{@link PooledConnectionFactory}
|
||||||
*
|
*
|
||||||
* 使用: Docker部署FastDFS(附示例代码) https://www.cnblogs.com/cao-lei/p/13470695.html
|
* 使用: Docker部署FastDFS(附示例代码) https://www.cnblogs.com/cao-lei/p/13470695.html
|
||||||
|
* github 地址:https://github.com/tobato/FastDFS_Client
|
||||||
*/
|
*/
|
||||||
@RefreshScope
|
@RefreshScope
|
||||||
@Configuration
|
@Configuration
|
||||||
|
|
@ -26,7 +27,13 @@ public class FastDfsConfig {
|
||||||
* FastDFS配置 其他参数见:{@link PooledConnectionFactory}
|
* FastDFS配置 其他参数见:{@link PooledConnectionFactory}
|
||||||
* //@Value("${fdfs.domain}")
|
* //@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() {
|
public String getDomain() {
|
||||||
return domain;
|
return domain;
|
||||||
|
|
@ -35,4 +42,12 @@ public class FastDfsConfig {
|
||||||
public void setDomain(String domain) {
|
public void setDomain(String domain) {
|
||||||
this.domain = domain;
|
this.domain = domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTokenSecretKey() {
|
||||||
|
return tokenSecretKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTokenSecretKey(String tokenSecretKey) {
|
||||||
|
this.tokenSecretKey = tokenSecretKey;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
package com.ruoyi.file.service;
|
package com.ruoyi.file.service;
|
||||||
|
|
||||||
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
|
|
||||||
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
|
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
|
||||||
import com.ruoyi.common.core.exception.CustomException;
|
import com.ruoyi.common.core.exception.CustomException;
|
||||||
import com.ruoyi.file.config.FastDfsConfig;
|
import com.ruoyi.file.config.FastDfsConfig;
|
||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.csource.fastdfs.ProtoCommon;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.domain.fdfs.StorePath;
|
||||||
import com.github.tobato.fastdfs.service.FastFileStorageClient;
|
import com.github.tobato.fastdfs.service.FastFileStorageClient;
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FastDFS 文件存储
|
* FastDFS 文件存储
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
* @see FastDfsConfig
|
* @see FastDfsConfig
|
||||||
*/
|
*/
|
||||||
|
@Primary
|
||||||
@Service()
|
@Service()
|
||||||
public class FastDfsServiceImpl implements IDfsService
|
public class FastDfsServiceImpl implements IDfsService
|
||||||
{
|
{
|
||||||
|
|
@ -51,13 +50,10 @@ public class FastDfsServiceImpl implements IDfsService
|
||||||
// fastdsf 这里的 modules 没用
|
// fastdsf 这里的 modules 没用
|
||||||
validateModule(file, modules);
|
validateModule(file, modules);
|
||||||
|
|
||||||
Set<MetaData> metaDataSet = new HashSet<>(1);
|
|
||||||
metaDataSet.add(new MetaData("groupName", "group1"));
|
|
||||||
|
|
||||||
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
|
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();
|
return fastDfsConfig.getDomain() + "/" + storePath.getFullPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,8 +77,31 @@ public class FastDfsServiceImpl implements IDfsService
|
||||||
throw new CustomException("fastdfs-获取文件占用空间功能,敬请期待");
|
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
|
@Override
|
||||||
public String presignedUrl(String fileUrl) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,7 @@ public interface IDfsService
|
||||||
* 3、qiniu ;七牛云存储; 下载凭证(如果Bucket设置成私有,必须要有 下载凭证),路径:【对象存储==》使用指南===》安全机制===》 下载凭证】 https://developer.qiniu.com/kodo/1202/download-token
|
* 3、qiniu ;七牛云存储; 下载凭证(如果Bucket设置成私有,必须要有 下载凭证),路径:【对象存储==》使用指南===》安全机制===》 下载凭证】 https://developer.qiniu.com/kodo/1202/download-token
|
||||||
* https://developer.qiniu.com/kodo/5914/s3-compatible-sts
|
* https://developer.qiniu.com/kodo/5914/s3-compatible-sts
|
||||||
* 4、腾讯 临时密钥(临时访问凭证) GetFederationToken 临时密钥生成及使用指引 https://cloud.tencent.com/document/product/436/14048?from=10680
|
* 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 文件访问地址,全路径或者不是全路径都可以
|
* @param fileUrl 文件访问地址,全路径或者不是全路径都可以
|
||||||
* @return 返回签名后的url
|
* @return 返回签名后的url
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ import java.io.InputStream;
|
||||||
*
|
*
|
||||||
* 构建时间戳防盗链访问链接: https://developer.qiniu.com/kodo/1239/java#fusion-antileech
|
* 构建时间戳防盗链访问链接: https://developer.qiniu.com/kodo/1239/java#fusion-antileech
|
||||||
*/
|
*/
|
||||||
@Primary
|
//@Primary
|
||||||
@Service()
|
@Service()
|
||||||
public class QiniuDfsServiceImpl implements IDfsService {
|
public class QiniuDfsServiceImpl implements IDfsService {
|
||||||
private static final Logger log = LoggerFactory.getLogger(QiniuDfsServiceImpl.class);
|
private static final Logger log = LoggerFactory.getLogger(QiniuDfsServiceImpl.class);
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,11 @@ spring:
|
||||||
discovery:
|
discovery:
|
||||||
# 服务注册地址
|
# 服务注册地址
|
||||||
server-addr: 127.0.0.1:8848
|
server-addr: 127.0.0.1:8848
|
||||||
|
namespace: 72b686a1-d9f6-499f-8275-e481b664779e
|
||||||
config:
|
config:
|
||||||
# 配置中心地址
|
# 配置中心地址
|
||||||
server-addr: 127.0.0.1:8848
|
server-addr: 127.0.0.1:8848
|
||||||
|
namespace: 72b686a1-d9f6-499f-8275-e481b664779e
|
||||||
# 配置文件格式
|
# 配置文件格式
|
||||||
file-extension: yml
|
file-extension: yml
|
||||||
# 共享配置
|
# 共享配置
|
||||||
|
|
@ -43,10 +45,11 @@ ftp:
|
||||||
|
|
||||||
# 文件服务器之3 FastDFS配置
|
# 文件服务器之3 FastDFS配置
|
||||||
fdfs:
|
fdfs:
|
||||||
domain: http://8.129.231.12
|
|
||||||
soTimeout: 3000
|
soTimeout: 3000
|
||||||
connectTimeout: 2000
|
connectTimeout: 5000
|
||||||
trackerList: 8.129.231.12:22122
|
trackerList: 47.99.175.191:22122
|
||||||
|
domain: http://47.99.175.191:8888
|
||||||
|
token-secret-key: test@test
|
||||||
|
|
||||||
# 文件服务器之4 Minio配置
|
# 文件服务器之4 Minio配置
|
||||||
minio:
|
minio:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue