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

pull/235/head
YiwenXia 1 year ago
parent 7e0f7f6608
commit 713edcfb8d

@ -20,6 +20,13 @@ public class FileAttribute {
private Boolean skipDownLoad = false; private Boolean skipDownLoad = false;
private Boolean forceUpdatedCache = false; private Boolean forceUpdatedCache = false;
/**
*
* {username:"test","password":"test"}
* json
*/
private String kkProxyAuthorization;
public FileAttribute() { public FileAttribute() {
} }
@ -124,4 +131,11 @@ public class FileAttribute {
this.forceUpdatedCache = forceUpdatedCache; this.forceUpdatedCache = forceUpdatedCache;
} }
public String getKkProxyAuthorization() {
return kkProxyAuthorization;
}
public void setKkProxyAuthorization(String kkProxyAuthorization) {
this.kkProxyAuthorization = kkProxyAuthorization;
}
} }

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

@ -3,6 +3,7 @@ package cn.keking.utils;
import cn.keking.config.ConfigConstants; import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute; import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.mola.galimatias.GalimatiasParseException; import io.mola.galimatias.GalimatiasParseException;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -12,11 +13,19 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.URL; 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 java.util.UUID;
import static cn.keking.utils.KkFileUtils.isFtpUrl; import static cn.keking.utils.KkFileUtils.isFtpUrl;
import static cn.keking.utils.KkFileUtils.isHttpUrl; 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 * @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_USERNAME = "ftp.username";
private static final String URL_PARAM_FTP_PASSWORD = "ftp.password"; 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 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 * @param fileAttribute fileAttribute
@ -75,7 +87,20 @@ public class DownloadUtils {
if (!fileAttribute.getSkipDownLoad()) { if (!fileAttribute.getSkipDownLoad()) {
if (isHttpUrl(url)) { if (isHttpUrl(url)) {
File realFile = new File(realPath); 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)) { } else if (isFtpUrl(url)) {
String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME); String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD); String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);

Loading…
Cancel
Save