diff --git a/server/src/main/java/cn/keking/model/FileAttribute.java b/server/src/main/java/cn/keking/model/FileAttribute.java index f07ba2f6..24694ab6 100644 --- a/server/src/main/java/cn/keking/model/FileAttribute.java +++ b/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; + } } diff --git a/server/src/main/java/cn/keking/service/FileHandlerService.java b/server/src/main/java/cn/keking/service/FileHandlerService.java index 8f2fe5e7..28f495a9 100644 --- a/server/src/main/java/cn/keking/service/FileHandlerService.java +++ b/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.getHeader( "kk-proxy-authorization"); + attribute.setKkProxyAuthorization(kkProxyAuthorization); + } return attribute; diff --git a/server/src/main/java/cn/keking/utils/DownloadUtils.java b/server/src/main/java/cn/keking/utils/DownloadUtils.java index a5d93f75..9bd93fcd 100644 --- a/server/src/main/java/cn/keking/utils/DownloadUtils.java +++ b/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 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);