mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-03-18 23:23:46 +08:00
新功能点:支持全局水印
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package cn.keking.web.filter;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author chenjh
|
||||
* @since 2020/5/13 18:27
|
||||
*/
|
||||
public class BaseUrlFilter implements Filter {
|
||||
|
||||
private static String BASE_URL;
|
||||
|
||||
public static String getBaseUrl() {
|
||||
String baseUrl;
|
||||
try {
|
||||
baseUrl = (String) RequestContextHolder.currentRequestAttributes().getAttribute("baseUrl",0);
|
||||
} catch (Exception e) {
|
||||
baseUrl = BASE_URL;
|
||||
}
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
|
||||
String baseUrl;
|
||||
StringBuilder pathBuilder = new StringBuilder();
|
||||
pathBuilder.append(request.getScheme()).append("://").append(request.getServerName()).append(":")
|
||||
.append(request.getServerPort()).append(((HttpServletRequest) request).getContextPath()).append("/");
|
||||
String baseUrlTmp = ConfigConstants.getBaseUrl();
|
||||
if (baseUrlTmp != null && !ConfigConstants.DEFAULT_BASE_URL.equals(baseUrlTmp.toLowerCase())) {
|
||||
if (!baseUrlTmp.endsWith("/")) {
|
||||
baseUrlTmp = baseUrlTmp.concat("/");
|
||||
}
|
||||
baseUrl = baseUrlTmp;
|
||||
} else {
|
||||
baseUrl = pathBuilder.toString();
|
||||
}
|
||||
BASE_URL = baseUrl;
|
||||
request.setAttribute("baseUrl", baseUrl);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -38,4 +38,29 @@ public class FilterConfiguration {
|
||||
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("/onlinePreview");
|
||||
filterUri.add("/picturesPreview");
|
||||
WatermarkConfigFilter filter = new WatermarkConfigFilter();
|
||||
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
|
||||
registrationBean.setFilter(filter);
|
||||
registrationBean.setUrlPatterns(filterUri);
|
||||
return registrationBean;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package cn.keking.web.filter;
|
||||
|
||||
import cn.keking.config.WatermarkConfigConstants;
|
||||
|
||||
import javax.servlet.*;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author chenjh
|
||||
* @since 2020/5/13 18:34
|
||||
*/
|
||||
public class WatermarkConfigFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
|
||||
String watermarkTxt = request.getParameter("watermarkTxt");
|
||||
request.setAttribute("watermarkTxt", watermarkTxt != null ? watermarkTxt : WatermarkConfigConstants.getWatermarkTxt());
|
||||
request.setAttribute("watermarkXSpace", WatermarkConfigConstants.getWatermarkXSpace());
|
||||
request.setAttribute("watermarkYSpace", WatermarkConfigConstants.getWatermarkYSpace());
|
||||
request.setAttribute("watermarkFont", WatermarkConfigConstants.getWatermarkFont());
|
||||
request.setAttribute("watermarkFontsize", WatermarkConfigConstants.getWatermarkFontsize());
|
||||
request.setAttribute("watermarkColor", WatermarkConfigConstants.getWatermarkColor());
|
||||
request.setAttribute("watermarkAlpha", WatermarkConfigConstants.getWatermarkAlpha());
|
||||
request.setAttribute("watermarkWidth", WatermarkConfigConstants.getWatermarkWidth());
|
||||
request.setAttribute("watermarkHeight", WatermarkConfigConstants.getWatermarkHeight());
|
||||
request.setAttribute("watermarkAngle", WatermarkConfigConstants.getWatermarkAngle());
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user