feat: 新增预览文件 host 黑名单机制

This commit is contained in:
kl
2024-03-06 19:49:59 +08:00
parent 787e9fe615
commit d1d8ffef7a
7 changed files with 134 additions and 56 deletions

View File

@@ -43,10 +43,10 @@ public class AttributeSetFilter implements Filter {
request.setAttribute("beian", ConfigConstants.getBeian());
request.setAttribute("size", ConfigConstants.maxSize());
request.setAttribute("deleteCaptcha", ConfigConstants.getDeleteCaptcha());
request.setAttribute("homePpageNumber", ConfigConstants.gethomePpageNumber());
request.setAttribute("homePagination", ConfigConstants.gethomePagination());
request.setAttribute("homePageSize", ConfigConstants.gethomePageSize());
request.setAttribute("homeSearch", ConfigConstants.gethomeSearch());
request.setAttribute("homePageNumber", ConfigConstants.getHomePageNumber());
request.setAttribute("homePagination", ConfigConstants.getHomePagination());
request.setAttribute("homePageSize", ConfigConstants.getHomePageSize());
request.setAttribute("homeSearch", ConfigConstants.getHomeSearch());
}
/**

View File

@@ -43,7 +43,7 @@ public class BaseUrlFilter implements Filter {
final String urlInHeader = servletRequest.getHeader("X-Base-Url");
if (StringUtils.isNotEmpty(urlInHeader)) {
baseUrl = urlInHeader;
} else if (configBaseUrl != null && !ConfigConstants.DEFAULT_BASE_URL.equalsIgnoreCase(configBaseUrl)) {
} else if (configBaseUrl != null && !ConfigConstants.DEFAULT_VALUE.equalsIgnoreCase(configBaseUrl)) {
//2、如果配置文件中配置了 baseUrl 且不为 default 则以配置文件为准
baseUrl = configBaseUrl;
} else {

View File

@@ -19,7 +19,7 @@ import org.springframework.util.FileCopyUtils;
*/
public class TrustHostFilter implements Filter {
private String notTrustHost;
private String notTrustHostHtmlView;
@Override
public void init(FilterConfig filterConfig) {
@@ -27,7 +27,7 @@ public class TrustHostFilter implements Filter {
try {
classPathResource.getInputStream();
byte[] bytes = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
this.notTrustHost = new String(bytes, StandardCharsets.UTF_8);
this.notTrustHostHtmlView = new String(bytes, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
@@ -37,16 +37,24 @@ public class TrustHostFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String url = WebUtils.getSourceUrl(request);
String host = WebUtils.getHost(url);
if (host != null &&!ConfigConstants.getTrustHostSet().isEmpty() && !ConfigConstants.getTrustHostSet().contains(host)) {
String html = this.notTrustHost.replace("${current_host}", host);
assert host != null;
if (!isTrustHost(host) || isNotTrustHost(host)) {
String html = this.notTrustHostHtmlView.replace("${current_host}", host);
response.getWriter().write(html);
response.getWriter().close();
}
else {
} else {
chain.doFilter(request, response);
}
}
public boolean isTrustHost(String host) {
return !ConfigConstants.getTrustHostSet().isEmpty() && ConfigConstants.getTrustHostSet().contains(host);
}
public boolean isNotTrustHost(String host) {
return !ConfigConstants.getNotTrustHostSet().isEmpty() && ConfigConstants.getNotTrustHostSet().contains(host);
}
@Override
public void destroy() {