1.配置nginx文件,我這里是使用騰訊云申請(qǐng)的https
server {
#SSL 訪問端口號(hào)為 443
listen 443 ssl;
# listen 80;
#填寫綁定證書的域名
server_name xx;
#證書文件名稱
ssl_certificate /etc/nginx/conf/certificate/你的證書.crt;
#私鑰文件名稱
ssl_certificate_key /etc/nginx/conf/certificate/你的證書.key;
ssl_session_timeout 5m;
#請(qǐng)按照以下協(xié)議配置
ssl_protocols TLSv1.2 TLSv1.3;
#請(qǐng)按照以下套件配置,配置加密套件,寫法遵循 openssl 標(biāo)準(zhǔn)。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location /preview{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 本地運(yùn)行的kkFileView的地址
proxy_pass http://127.0.0.1:8012;
}
}
2.修改kkfileview的application.properties配置
server.servlet.context-path= /preview/
base.url= https://你的域名/preview/
3.重啟,訪問路徑
https://你的路徑/preview/onlinePreview?url=xxx
3.1 如果訪問出錯(cuò),并且報(bào)錯(cuò)信息是下圖
兩種解決方案
第一用我打包好的jar,下載替換就行
代碼是 2021年7月6日,v4.0.0 版本
下載地址:鏈接: https://pan.baidu.com/s/1yqJDa75tokAWQhn_tfCOmA?pwd=ribv 提取碼: ribv
第二你自己在gitee拉取代碼進(jìn)行處理文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-510669.html
如果拉取中報(bào)錯(cuò)
error: RPC failed; curl 18 transfer closed with outstanding read data remaining error: 2406 bytes of
原因:緩存區(qū)溢出curl的postBuffer的默認(rèn)值太小,需要增加緩存文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-510669.html
使用git命令增大緩存(單位是b,524288000B也就500M左右)
git config --global http.postBuffer 524288000
使用git config --list查看是否生效
2.1新增工具類
package cn.keking.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* @Author:
* @Create: 2022/7/29 16:31
*/
public class SslUtils {
private final static Logger logger = LoggerFactory.getLogger(SslUtils.class);
private static void trustAllHttpsCertificates() throws Exception {
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
static class miTM implements TrustManager, X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
}
/**
* 忽略HTTPS請(qǐng)求的SSL證書,必須在openConnection之前調(diào)用
* @throws Exception
*/
public static void ignoreSsl(){
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
try {
trustAllHttpsCertificates();
} catch (Exception e) {
e.printStackTrace();
logger.error("忽略https失敗");
}
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}
2.2打開DownloadUtils找到ReturnResponse并替換
/**
* @param fileAttribute fileAttribute
* @param fileName 文件名
* @return 本地文件絕對(duì)路徑
*/
public static ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) {
String urlStr = fileAttribute.getUrl().replaceAll("\\+", "%20");
ReturnResponse<String> response = new ReturnResponse<>(0, "下載成功!!!", "");
String realPath = DownloadUtils.getRelFilePath(fileName, fileAttribute);
try {
URL url = WebUtils.normalizedURL(urlStr);
SslUtils.ignoreSsl();
if (!fileAttribute.getSkipDownLoad()) {
if (isHttpUrl(url)) {
File realFile = new File(realPath);
FileUtils.copyURLToFile(url, realFile);
} else if (isFtpUrl(url)) {
String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
String ftpControlEncoding = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding);
} else {
response.setCode(1);
response.setMsg("url不能識(shí)別url" + urlStr);
}
}
response.setContent(realPath);
response.setMsg(fileName);
return response;
} catch (IOException | GalimatiasParseException e) {
logger.error("文件下載失敗,url:{}", urlStr, e);
response.setCode(1);
response.setContent(null);
if (e instanceof FileNotFoundException) {
response.setMsg("文件不存在!!!");
} else {
response.setMsg(e.getMessage());
}
return response;
}
}
到了這里,關(guān)于kkviewfile 實(shí)現(xiàn)nginx反向代理+https的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!