fastdfs增加防掉链的url 方法【presignedUrl】

pull/84/head
duandazhi 4 years ago
parent 407b7e526f
commit a88298057f

@ -65,6 +65,11 @@
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
</dependency>
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
<!-- 文件服务器2Minio -->
<dependency>

@ -14,6 +14,7 @@ import org.springframework.context.annotation.Configuration;
* FastDFS {@link PooledConnectionFactory}
*
* 使: DockerFastDFS 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;
/**
* tokenkey
* 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;
}
}

@ -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<MetaData> 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 tokenurl; 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;
}
}

@ -76,6 +76,7 @@ public interface IDfsService
* 3qiniu (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
* 5fastdfs fastdfs https://www.cnblogs.com/xiaolinstudy/p/9341779.html
* @param fileUrl 访,
* @return url
*/

@ -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);

@ -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:

Loading…
Cancel
Save