Browse Source

一个通用的文件服务器认证访问的设计,解决 https://www.gitlink.org.cn/kk-open/kkFileView/issues/3 的问题

pull/235/head
YiwenXia 1 year ago
parent
commit
713edcfb8d
  1. 14
      server/src/main/java/cn/keking/model/FileAttribute.java
  2. 3
      server/src/main/java/cn/keking/service/FileHandlerService.java
  3. 29
      server/src/main/java/cn/keking/utils/DownloadUtils.java

14
server/src/main/java/cn/keking/model/FileAttribute.java

@ -20,6 +20,13 @@ public class FileAttribute {
private Boolean skipDownLoad = false;
private Boolean forceUpdatedCache = false;
/**
* 代理请求到文件服务器的认证请求头格式如下
* {username:"test","password":"test"}
* 请求文件服务器时会将 json 直接塞到请求头里
*/
private String kkProxyAuthorization;
public FileAttribute() {
}
@ -124,4 +131,11 @@ public class FileAttribute {
this.forceUpdatedCache = forceUpdatedCache;
}
public String getKkProxyAuthorization() {
return kkProxyAuthorization;
}
public void setKkProxyAuthorization(String kkProxyAuthorization) {
this.kkProxyAuthorization = kkProxyAuthorization;
}
}

3
server/src/main/java/cn/keking/service/FileHandlerService.java

@ -469,6 +469,9 @@ public class FileHandlerService implements InitializingBean {
if (StringUtils.hasText(userToken)) {
attribute.setUserToken(userToken);
}
String kkProxyAuthorization = req.getParameter( "kk-proxy-authorization");
attribute.setKkProxyAuthorization(kkProxyAuthorization);
}
return attribute;

29
server/src/main/java/cn/keking/utils/DownloadUtils.java

@ -3,6 +3,7 @@ package cn.keking.utils;
import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.mola.galimatias.GalimatiasParseException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
@ -12,11 +13,19 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import static cn.keking.utils.KkFileUtils.isFtpUrl;
import static cn.keking.utils.KkFileUtils.isHttpUrl;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;
/**
* @author yudian-it
*/
@ -27,6 +36,9 @@ public class DownloadUtils {
private static final String URL_PARAM_FTP_USERNAME = "ftp.username";
private static final String URL_PARAM_FTP_PASSWORD = "ftp.password";
private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding";
private static final RestTemplate restTemplate = new RestTemplate();
private static final ObjectMapper mapper = new ObjectMapper();
/**
* @param fileAttribute fileAttribute
@ -75,7 +87,20 @@ public class DownloadUtils {
if (!fileAttribute.getSkipDownLoad()) {
if (isHttpUrl(url)) {
File realFile = new File(realPath);
FileUtils.copyURLToFile(url, realFile);
RequestCallback requestCallback = request -> {
request.getHeaders()
.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
String proxyAuthorization = fileAttribute.getKkProxyAuthorization();
if(StringUtils.hasText(proxyAuthorization)){
Map<String,String> proxyAuthorizationMap = mapper.readValue(proxyAuthorization, Map.class);
proxyAuthorizationMap.entrySet().forEach(entry-> request.getHeaders().set(entry.getKey(), entry.getValue()));
}
};
urlStr = URLDecoder.decode(urlStr, StandardCharsets.UTF_8.name());
restTemplate.execute(urlStr, HttpMethod.GET, requestCallback, fileResponse -> {
FileUtils.copyToFile(fileResponse.getBody(), realFile);
return null;
});
} else if (isFtpUrl(url)) {
String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);

Loading…
Cancel
Save