Skip to content

支持反向代理、trusthost支持子域名、无法预览的文件提供下载链接 #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ nbdist/

server/src/main/cache/
server/src/main/file/
### temp files ###
__pycache__/
*.dmp
1 change: 1 addition & 0 deletions server/src/main/config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
server.port = ${KK_SERVER_PORT:8012}
server.servlet.context-path= ${KK_CONTEXT_PATH:/}
server.servlet.encoding.charset = utf-8
server.forward-headers-strategy=NATIVE
#启用GZIP压缩功能
server.compression.enabled = true
#允许压缩的响应缓冲区最小字节数,默认2048
Expand Down
4 changes: 3 additions & 1 deletion server/src/main/java/cn/keking/web/filter/BaseUrlFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
baseUrl = configBaseUrl;
} else {
//3、默认动态拼接 baseUrl
baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
String port = (("https".equals(request.getScheme()) && request.getServerPort() == 443)) || ("http".equals(request.getScheme()) && request.getServerPort() == 80)
? "" : ":" + request.getServerPort();
baseUrl = request.getScheme() + "://" + request.getServerName() + port
+ servletRequest.getContextPath() + "/";
}

Expand Down
19 changes: 14 additions & 5 deletions server/src/main/java/cn/keking/web/filter/TrustHostFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.servlet.ServletResponse;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

Expand Down Expand Up @@ -51,11 +52,19 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}

public boolean isNotTrustHost(String host) {
if (CollectionUtils.isNotEmpty(ConfigConstants.getNotTrustHostSet())) {
return ConfigConstants.getNotTrustHostSet().contains(host);
}
if (CollectionUtils.isNotEmpty(ConfigConstants.getTrustHostSet())) {
return !ConfigConstants.getTrustHostSet().contains(host);
while(StringUtils.isNotBlank(host)){
if (CollectionUtils.isNotEmpty(ConfigConstants.getNotTrustHostSet()) && ConfigConstants.getNotTrustHostSet().contains(host)) {
return true;
}
if (CollectionUtils.isNotEmpty(ConfigConstants.getTrustHostSet()) && ConfigConstants.getTrustHostSet().contains(host)) {
return false;
}
//try sub domain
if (host.contains(".")) {
host = host.substring(host.indexOf(".")+1);
} else {
return false;
}
}
return false;
}
Expand Down
18 changes: 16 additions & 2 deletions server/src/main/resources/web/fileNotSupported.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,26 @@

<body>
<div class="container">
<img src="images/sorry.jpg"/>
<a name="download" title="点击下载文件"><img src="images/sorry.jpg"/></a>
<span>
该(${fileType})文件,系统暂不支持在线预览,具体原因如下:
该(${fileType})<a name="download" title="点击下载文件">文件</a>,系统暂不支持在线预览,具体原因如下:
<p style="color: red;">${msg}</p>
</span>
<span>
你可以&nbsp;<a name="download" title="点击下载文件">下载</a>&nbsp;文件(&nbsp;<a id="file" name="download" title="点击下载文件"></a>&nbsp;)本地查看。
</span>
<p>有任何疑问,请加入kk开源社区知识星球咨询:<a href="https://t.zsxq.com/09ZHSXbsQ">https://t.zsxq.com/09ZHSXbsQ</a><br></p>
</div>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
var downloads = document.querySelectorAll('[name="download"]');
downloads.forEach(function (download) {
download.href = atob(decodeURI(new URL(location.href).searchParams.get('url')));
if(download.id === 'file'){
download.innerText = download.href;
}
});
});
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion server/src/main/resources/web/main/index.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
+ '|' // 允许IP和DOMAIN(域名)
+ '(localhost)|' //匹配localhost
+ '([\\w_!~*\'()-]+\\.)*' // 域名- 至少一个[英文或数字_!~*\'()-]加上.
+ '\\w+\\.' // 一级域名 -英文或数字 加上.
+ '[\\w_-]+\\.' // 一级域名 -_英文或数字 加上.
+ '[a-zA-Z]{1,6})' // 顶级域名- 1-6位英文
+ '(:[0-9]{1,5})?' // 端口- :80 ,1-5位数字
+ '((/?)|' // url无参数结尾 - 斜杆或这没有
Expand Down
10 changes: 5 additions & 5 deletions server/src/main/resources/web/main/integrated.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
<#-- 接入说明 -->
<div class="page-header">
<h1>接入说明</h1>
本文档针对前端项目接入 kkFileView 的说明,并假设 kkFileView 的服务地址为:http://127.0.0.1:8012
本文档针对前端项目接入 kkFileView 的说明,并假设 kkFileView 的服务地址为:${baseUrl}
</div>
<div class="well">

<div style="font-size: 16px;">
【http/https 资源文件预览】如果你的项目需要接入文件预览项目,达到对docx、excel、ppt、jpg等文件的预览效果,那么通过在你的项目中加入下面的代码就可以成功实现:
<p style="background-color: #2f332a;color: #cccccc;font-size: 14px;padding:10px;margin-top:10px;">
var url = 'http://127.0.0.1:8080/file/test.txt'; //要预览文件的访问地址 <br>
window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(base64Encode(url)));
window.open('${baseUrl}onlinePreview?url='+encodeURIComponent(base64Encode(url)));
</p>
</div>
<br>
Expand All @@ -49,15 +49,15 @@
<p style="background-color: #2f332a;color: #cccccc;font-size: 14px;padding:10px;margin-top:10px;">
var originUrl = 'http://127.0.0.1:8080/filedownload?fileId=1'; //要预览文件的访问地址<br>
var previewUrl = originUrl + '&fullfilename=test.txt'<br>
window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(previewUrl)));
window.open('${baseUrl}onlinePreview?url='+encodeURIComponent(Base64.encode(previewUrl)));
</p>
</div>
<br>
<div style="font-size: 16px;">
【ftp 资源文件预览】如果要预览的FTP url是可以匿名访问的(不需要用户名密码),则可以直接通过下载url预览,示例如下
<p style="background-color: #2f332a;color: #cccccc;font-size: 14px;padding:10px;margin-top:10px;">
var url = 'ftp://127.0.0.1/file/test.txt'; //要预览文件的访问地址<br>
window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(url)));
window.open('${baseUrl}onlinePreview?url='+encodeURIComponent(Base64.encode(url)));
</p>
</div>
<br>
Expand All @@ -66,7 +66,7 @@
<p style="background-color: #2f332a;color: #cccccc;font-size: 14px;padding:10px;margin-top:10px;">
var originUrl = 'ftp://127.0.0.1/file/test.txt'; //要预览文件的访问地址<br>
var previewUrl = originUrl + '?ftp.username=xx&ftp.password=xx&ftp.control.encoding=xx';<br>
window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(previewUrl)));
window.open('${baseUrl}onlinePreview?url='+encodeURIComponent(Base64.encode(previewUrl)));
</p>
</div>
</div>
Expand Down