chenkailing
3 years ago
committed by
kl
7 changed files with 233 additions and 135 deletions
@ -1,65 +0,0 @@
|
||||
package cn.keking.web.filter; |
||||
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
|
||||
|
||||
/** |
||||
* |
||||
* @author yudian-it |
||||
* @date 2017/11/30 |
||||
*/ |
||||
@Configuration |
||||
public class FilterConfiguration { |
||||
|
||||
|
||||
@Bean |
||||
public FilterRegistrationBean getChinesePathFilter() { |
||||
ChinesePathFilter filter = new ChinesePathFilter(); |
||||
FilterRegistrationBean registrationBean = new FilterRegistrationBean(); |
||||
registrationBean.setFilter(filter); |
||||
return registrationBean; |
||||
} |
||||
|
||||
@Bean |
||||
public FilterRegistrationBean getTrustHostFilter() { |
||||
Set<String> filterUri = new HashSet<>(); |
||||
filterUri.add("/onlinePreview"); |
||||
filterUri.add("/picturesPreview"); |
||||
TrustHostFilter filter = new TrustHostFilter(); |
||||
FilterRegistrationBean registrationBean = new FilterRegistrationBean(); |
||||
registrationBean.setFilter(filter); |
||||
registrationBean.setUrlPatterns(filterUri); |
||||
return registrationBean; |
||||
} |
||||
|
||||
@Bean |
||||
public FilterRegistrationBean getBaseUrlFilter() { |
||||
Set<String> filterUri = new HashSet<>(); |
||||
filterUri.add("/index"); |
||||
filterUri.add("/onlinePreview"); |
||||
filterUri.add("/picturesPreview"); |
||||
BaseUrlFilter filter = new BaseUrlFilter(); |
||||
FilterRegistrationBean registrationBean = new FilterRegistrationBean(); |
||||
registrationBean.setFilter(filter); |
||||
registrationBean.setUrlPatterns(filterUri); |
||||
return registrationBean; |
||||
} |
||||
|
||||
@Bean |
||||
public FilterRegistrationBean getWatermarkConfigFilter() { |
||||
Set<String> filterUri = new HashSet<>(); |
||||
filterUri.add("/index"); |
||||
filterUri.add("/onlinePreview"); |
||||
filterUri.add("/picturesPreview"); |
||||
AttributeSetFilter filter = new AttributeSetFilter(); |
||||
FilterRegistrationBean registrationBean = new FilterRegistrationBean(); |
||||
registrationBean.setFilter(filter); |
||||
registrationBean.setUrlPatterns(filterUri); |
||||
return registrationBean; |
||||
} |
||||
} |
@ -0,0 +1,73 @@
|
||||
package cn.keking.web.filter; |
||||
|
||||
import cn.keking.config.ConfigConstants; |
||||
import cn.keking.utils.WebUtils; |
||||
import io.mola.galimatias.GalimatiasParseException; |
||||
import org.artofsolving.jodconverter.util.PlatformUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.core.io.ClassPathResource; |
||||
import org.springframework.util.FileCopyUtils; |
||||
|
||||
import javax.servlet.*; |
||||
import java.io.IOException; |
||||
import java.net.URL; |
||||
import java.net.URLDecoder; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Locale; |
||||
|
||||
/** |
||||
* @author : kl (http://kailing.pub)
|
||||
* @since : 2022-05-25 17:45 |
||||
*/ |
||||
public class TrustDirFilter implements Filter { |
||||
|
||||
private String notTrustDirView; |
||||
private final Logger logger = LoggerFactory.getLogger(TrustDirFilter.class); |
||||
|
||||
|
||||
@Override |
||||
public void init(FilterConfig filterConfig) { |
||||
ClassPathResource classPathResource = new ClassPathResource("web/notTrustDir.html"); |
||||
try { |
||||
classPathResource.getInputStream(); |
||||
byte[] bytes = FileCopyUtils.copyToByteArray(classPathResource.getInputStream()); |
||||
this.notTrustDirView = new String(bytes, StandardCharsets.UTF_8); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
||||
String url = WebUtils.getSourceUrl(request); |
||||
if (!allowPreview(url)) { |
||||
response.getWriter().write(this.notTrustDirView); |
||||
response.getWriter().close(); |
||||
} else { |
||||
chain.doFilter(request, response); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void destroy() { |
||||
|
||||
} |
||||
|
||||
private boolean allowPreview(String urlPath) { |
||||
try { |
||||
URL url = WebUtils.normalizedURL(urlPath); |
||||
if ("file".equals(url.getProtocol().toLowerCase(Locale.ROOT))) { |
||||
String filePath = URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8.name()); |
||||
if (PlatformUtils.isWindows()) { |
||||
filePath = filePath.replaceAll("/", "\\\\"); |
||||
} |
||||
return filePath.startsWith(ConfigConstants.getFileDir()) || filePath.startsWith(ConfigConstants.getLocalPreviewDir()); |
||||
} |
||||
return true; |
||||
} catch (IOException | GalimatiasParseException e) { |
||||
logger.error("解析URL异常,url:{}", urlPath, e); |
||||
return false; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,41 @@
|
||||
<!DOCTYPE html> |
||||
|
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="utf-8" /> |
||||
<style type="text/css"> |
||||
body { |
||||
margin: 0 auto; |
||||
width: 900px; |
||||
background-color: #CCB; |
||||
} |
||||
.container { |
||||
width: 700px; |
||||
height: 700px; |
||||
margin: 0 auto; |
||||
} |
||||
img { |
||||
width: auto; |
||||
height: auto; |
||||
max-width: 100%; |
||||
max-height: 100%; |
||||
padding-bottom: 36px; |
||||
} |
||||
p { |
||||
display: block; |
||||
font-size: 20px; |
||||
color: blue; |
||||
} |
||||
</style> |
||||
</head> |
||||
<body> |
||||
<div class="container"> |
||||
<img src="images/sorry.jpg" /> |
||||
<p> |
||||
预览源文件来自未授信的目录 ,请停止访问 <br> |
||||
有任何疑问,请加 <a href="https://jq.qq.com/?_wv=1027&k=5c0UAtu">官方QQ群:613025121</a> 咨询 |
||||
</p> |
||||
</div> |
||||
</body> |
||||
|
||||
</html> |
Loading…
Reference in new issue