Commit 1060bdd0 authored by 陈精华's avatar 陈精华 Committed by kl

新功能点:支持base url配置(主要用于nginx反向代理等)

parent 8c2fb2bd
...@@ -31,6 +31,9 @@ spring.http.multipart.max-file-size=100MB ...@@ -31,6 +31,9 @@ spring.http.multipart.max-file-size=100MB
cache.clean = true cache.clean = true
#######################################可在运行时动态配置####################################### #######################################可在运行时动态配置#######################################
#提供预览服务的地址,默认从请求url读,如果使用nginx等反向代理,需要手动设置
#base.url = https://file.keking.cn
#是否启用缓存 #是否启用缓存
cache.enabled = true cache.enabled = true
......
...@@ -23,6 +23,7 @@ public class ConfigConstants { ...@@ -23,6 +23,7 @@ public class ConfigConstants {
private static String ftpPassword; private static String ftpPassword;
private static String ftpControlEncoding; private static String ftpControlEncoding;
private static String fileDir = OfficeUtils.getHomePath() + File.separator + "file" + File.separator; private static String fileDir = OfficeUtils.getHomePath() + File.separator + "file" + File.separator;
private static String baseUrl;
public static Boolean isCacheEnabled() { public static Boolean isCacheEnabled() {
return cacheEnabled; return cacheEnabled;
...@@ -92,6 +93,15 @@ public class ConfigConstants { ...@@ -92,6 +93,15 @@ public class ConfigConstants {
return fileDir; return fileDir;
} }
public static String getBaseUrl() {
return baseUrl;
}
public static void setBaseUrl(String baseUrl) {
// 不以'/'结尾的,加上'/'
ConfigConstants.baseUrl = baseUrl.concat("/");
}
@Value("${file.dir:default}") @Value("${file.dir:default}")
public void setFileDir(String fileDir) { public void setFileDir(String fileDir) {
if (!"default".equals(fileDir)) { if (!"default".equals(fileDir)) {
......
...@@ -5,6 +5,7 @@ import org.artofsolving.jodconverter.office.OfficeUtils; ...@@ -5,6 +5,7 @@ import org.artofsolving.jodconverter.office.OfficeUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.io.BufferedReader; import java.io.BufferedReader;
...@@ -29,6 +30,7 @@ public class ConfigRefreshComponent { ...@@ -29,6 +30,7 @@ public class ConfigRefreshComponent {
public static final String DEFAULT_FTP_USERNAME = null; public static final String DEFAULT_FTP_USERNAME = null;
public static final String DEFAULT_FTP_PASSWORD = null; public static final String DEFAULT_FTP_PASSWORD = null;
public static final String DEFAULT_FTP_CONTROL_ENCODING = "UTF-8"; public static final String DEFAULT_FTP_CONTROL_ENCODING = "UTF-8";
public static final String BASE_URL = null;
@PostConstruct @PostConstruct
...@@ -53,6 +55,7 @@ public class ConfigRefreshComponent { ...@@ -53,6 +55,7 @@ public class ConfigRefreshComponent {
String ftpPassword; String ftpPassword;
String ftpControlEncoding; String ftpControlEncoding;
String configFilePath = OfficeUtils.getCustomizedConfigPath(); String configFilePath = OfficeUtils.getCustomizedConfigPath();
String baseUlr;
while (true) { while (true) {
FileReader fileReader = new FileReader(configFilePath); FileReader fileReader = new FileReader(configFilePath);
BufferedReader bufferedReader = new BufferedReader(fileReader); BufferedReader bufferedReader = new BufferedReader(fileReader);
...@@ -67,6 +70,7 @@ public class ConfigRefreshComponent { ...@@ -67,6 +70,7 @@ public class ConfigRefreshComponent {
ftpControlEncoding = properties.getProperty("ftp.control.encoding", DEFAULT_FTP_CONTROL_ENCODING); ftpControlEncoding = properties.getProperty("ftp.control.encoding", DEFAULT_FTP_CONTROL_ENCODING);
textArray = text.split(","); textArray = text.split(",");
mediaArray = media.split(","); mediaArray = media.split(",");
baseUlr = properties.getProperty("base.url", null);
ConfigConstants.setCacheEnabled(cacheEnabled); ConfigConstants.setCacheEnabled(cacheEnabled);
ConfigConstants.setSimText(textArray); ConfigConstants.setSimText(textArray);
ConfigConstants.setMedia(mediaArray); ConfigConstants.setMedia(mediaArray);
...@@ -75,6 +79,9 @@ public class ConfigRefreshComponent { ...@@ -75,6 +79,9 @@ public class ConfigRefreshComponent {
ConfigConstants.setFtpUsername(ftpUsername); ConfigConstants.setFtpUsername(ftpUsername);
ConfigConstants.setFtpPassword(ftpPassword); ConfigConstants.setFtpPassword(ftpPassword);
ConfigConstants.setFtpControlEncoding(ftpControlEncoding); ConfigConstants.setFtpControlEncoding(ftpControlEncoding);
if (baseUlr != null && !StringUtils.isEmpty(baseUlr)) {
ConfigConstants.setBaseUrl(baseUlr);
}
bufferedReader.close(); bufferedReader.close();
fileReader.close(); fileReader.close();
Thread.sleep(1000L); Thread.sleep(1000L);
......
package cn.keking.filters; package cn.keking.filters;
import cn.keking.config.ConfigConstants;
import org.springframework.util.StringUtils;
import javax.servlet.*; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
...@@ -19,10 +22,16 @@ public class ChinesePathFilter implements Filter { ...@@ -19,10 +22,16 @@ public class ChinesePathFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
StringBuilder pathBuilder = new StringBuilder(); String baseUrl;
pathBuilder.append(request.getScheme()).append("://").append(request.getServerName()).append(":") if (ConfigConstants.getBaseUrl() != null) {
.append(request.getServerPort()).append(((HttpServletRequest)request).getContextPath()).append("/"); baseUrl = ConfigConstants.getBaseUrl();
request.setAttribute("baseUrl", pathBuilder.toString()); } else {
StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append(request.getScheme()).append("://").append(request.getServerName()).append(":")
.append(request.getServerPort()).append(((HttpServletRequest) request).getContextPath()).append("/");
baseUrl = pathBuilder.toString();
}
request.setAttribute("baseUrl", baseUrl);
chain.doFilter(request, response); chain.doFilter(request, response);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment