在实际的项目开发中,是不是迂到这样的问题,输入一个完的地址后,就显示出了相对应的图片。为了预防这类问题,所以使用到fdfs的token;
开启token后,在访问这个地址
一、修改fdfs的http.conf配置文件
cd /etc/fdfs/
vi http.conf
## 开启token
# 服务器配置的秘钥
http.anti_steal.secret_key=Fdfs@0903&!
二、pom.xml
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
三、工具类
import org.csource.common.MyException;
import org.csource.fastdfs.ProtoCommon;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
public class FastDfsUtils {
/**
* 获取访问服务器的token,拼接到地址后面
*
* @param filepath 文件路径 group1/M00/00/00/wKgzgFnkTPyAIAUGAAEoRmXZPp876.jpeg
* @param httpSecretKey 密钥
* @return 返回token,如: token=078d370098b03e9020b82c829c205e1f&ts=1508141521
*/
public static String getToken(String filepath, String httpSecretKey) {
// unix seconds
int ts = (int) Instant.now().getEpochSecond();
// token
String token = "null";
try {
String path = getPath(filepath);
if(StringUtils.isEmpty(path)){
return null;
}
token = ProtoCommon.getToken(path, ts, httpSecretKey);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
sb.append("token=").append(token);
sb.append("&ts=").append(ts);
return sb.toString();
}
public static String getPath(String filepath){
//截取下标
String sub = "/group1/";
int index = filepath.indexOf(sub);
if(index <= 0){
//地址为空,或地址不包含"/group1/"路径
return null;
}
// 截取字符串
String path = filepath.substring(index + sub.length());
// System.out.println(result); // 输出如:M00/00/0A/wKhDKWDnqD2Aa-qCAAFdD_Pw4vA622.jpg
return path;
}
public static String getUrlWithToken(String filepath, String httpSecretKey){
String token = getToken(filepath, httpSecretKey);
if(StringUtils.isEmpty(token)){
return null;
}
return filepath + "?" + token;
}
public static String getUrlWithoutToken(String urlWithToken){
//截取下标
String sub = "?token=";
int index = urlWithToken.indexOf(sub);
if(index <= 0){
return urlWithToken;
}
// 截取字符串
String path = urlWithToken.substring(0, index);
// System.out.println(path);
return path;
}
public static void main(String[] args) {
String url = "https://xxxxxx/group1/M00/00/0B/wKhDKWE1xpuAHx-hAAy2WlTKUp8445.jpg";
getUrlWithoutToken(url);
System.out.println(getUrlWithToken(url,"Fdfs@0903&!"));
}
}
四、业务层代码
@GetMapping("/info/{id}")
@ApiOperation(value = "根据作品id获取个人作品详细信息", notes = "根据作品id获取个人作品详细信息")
@ApiImplicitParam(name = "id", value = "作品id", required = true, dataType = "Long")
public Result<Object> entInfo(@PathVariable(value = "id") Long id) throws MalformedURLException {
//根据Id到个人作品的具体信息,个人作品信息中有一个字段放到的是 https://xxxx/group1/M00/00/0B/wKhDKWE1xpuAHx-hAAy2WlTKUp8445.jpg,假如这个字段是url
personInfo info= person.getPersonInfoDetail(id);
info.setUrl(FastDfsUtils.getUrlWithToken(detail.getIdUrl,fdfsSecretKey));
System.out.print(info.setUrl());
//https://XXXX/group1/M00/00/0B/wKhDKWE1xpuAHx-hAAy2WlTKUp8445.jpg?token=cb2d9ee1a639b2e8f1f9776a3494b1ce&ts=1693723731
return Result.success(info);
}
五、校验
标签:return,String,filepath,FastDFS,token,path,id,防盗链 From: https://www.cnblogs.com/fwfjava/p/17674975.html