feature: 新增模板filter以及过滤器异常捕获
This commit is contained in:
parent
d2455401e6
commit
e00a98764c
@ -291,6 +291,8 @@ public enum ErrorCode implements ErrorCodeInterface {
|
||||
|
||||
GET_CACHE_FAILED(Module.COMMON, 4, "获取缓存失败"),
|
||||
|
||||
INTERNAL_ERROR(Module.COMMON, 5, "系统内部错误:{}"),
|
||||
|
||||
LOGIN_CAPTCHA_GENERATE_FAIL(Module.LOGIN, 1, "验证码生成失败"),
|
||||
|
||||
INVALID_TOKEN(Module.PERMISSION, 1, "token异常"),
|
||||
|
||||
@ -1,32 +1,35 @@
|
||||
package com.agileboot.infrastructure.config;
|
||||
|
||||
import com.agileboot.infrastructure.filter.RepeatableFilter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import com.agileboot.infrastructure.filter.GlobalExceptionFilter;
|
||||
import com.agileboot.infrastructure.filter.TestFilter;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Filter配置
|
||||
* @author ruoyi TODO 需整改
|
||||
* @author valarchie
|
||||
*/
|
||||
@Configuration
|
||||
public class FilterConfig {
|
||||
|
||||
@Value("${xss.excludes}")
|
||||
private String excludes;
|
||||
|
||||
@Value("${xss.urlPatterns}")
|
||||
private String urlPatterns;
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@Bean
|
||||
public FilterRegistrationBean someFilterRegistration() {
|
||||
FilterRegistrationBean registration = new FilterRegistrationBean();
|
||||
registration.setFilter(new RepeatableFilter());
|
||||
public FilterRegistrationBean<TestFilter> testFilterRegistrationBean() {
|
||||
FilterRegistrationBean<TestFilter> registration = new FilterRegistrationBean<>();
|
||||
registration.setFilter(new TestFilter());
|
||||
registration.addUrlPatterns("/*");
|
||||
registration.setName("repeatableFilter");
|
||||
registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE);
|
||||
registration.setName("testFilter");
|
||||
registration.setOrder(2);
|
||||
return registration;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<GlobalExceptionFilter> exceptionFilterRegistrationBean() {
|
||||
FilterRegistrationBean<GlobalExceptionFilter> registration = new FilterRegistrationBean<>();
|
||||
registration.setFilter(new GlobalExceptionFilter());
|
||||
registration.addUrlPatterns("/*");
|
||||
registration.setName("exceptionFilter");
|
||||
registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
|
||||
return registration;
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,49 @@
|
||||
package com.agileboot.infrastructure.filter;
|
||||
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.agileboot.common.core.dto.ResponseDTO;
|
||||
import com.agileboot.common.exception.error.ErrorCode.Internal;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
/**
|
||||
* 异常过滤器,因为配置的全局异常捕获器只能捕获MVC框架的异常
|
||||
* 不能捕获filter的异常
|
||||
* @author valarchie
|
||||
*/
|
||||
@Slf4j
|
||||
@WebFilter(filterName = "ExceptionFilter", urlPatterns = "/*")
|
||||
public class GlobalExceptionFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException {
|
||||
try {
|
||||
chain.doFilter(request, response);
|
||||
} catch (Exception e) {
|
||||
log.error("global filter exceptions", e);
|
||||
e.printStackTrace();
|
||||
String resultJson = JSONUtil.toJsonStr(ResponseDTO.fail(Internal.INTERNAL_ERROR, e));
|
||||
response.setContentType("application/json");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.getWriter().write(resultJson);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,46 +0,0 @@
|
||||
package com.agileboot.infrastructure.filter;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
/**
|
||||
* Repeatable 过滤器
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class RepeatableFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
|
||||
ServletRequest requestWrapper = null;
|
||||
if (request instanceof HttpServletRequest
|
||||
&& StrUtil.startWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE)) {
|
||||
requestWrapper = new RepeatedlyRequestWrapper((HttpServletRequest) request, response);
|
||||
}
|
||||
if (null == requestWrapper) {
|
||||
chain.doFilter(request, response);
|
||||
} else {
|
||||
chain.doFilter(requestWrapper, response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package com.agileboot.infrastructure.filter;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
|
||||
/**
|
||||
* 过滤器模板代码
|
||||
* @author valarchie
|
||||
*/
|
||||
public class TestFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
// 实现你的逻辑
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -117,13 +117,5 @@ swagger:
|
||||
# 请求前缀
|
||||
pathMapping: /dev-api
|
||||
|
||||
# 防止XSS攻击
|
||||
xss:
|
||||
# 过滤开关
|
||||
enabled: true
|
||||
# 排除链接(多个用逗号分隔)
|
||||
excludes: /system/notice
|
||||
# 匹配链接
|
||||
urlPatterns: /system/*,/monitor/*,/tool/*
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user