| | |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class XssFilter implements Filter |
| | | { |
| | | public class XssFilter implements Filter { |
| | | /** |
| | | * 排除链接 |
| | | */ |
| | | public List<String> excludes = new ArrayList<>(); |
| | | |
| | | @Override |
| | | public void init(FilterConfig filterConfig) throws ServletException |
| | | { |
| | | public void init(FilterConfig filterConfig) throws ServletException { |
| | | String tempExcludes = filterConfig.getInitParameter("excludes"); |
| | | if (StringUtils.isNotEmpty(tempExcludes)) |
| | | { |
| | | if (StringUtils.isNotEmpty(tempExcludes)) { |
| | | String[] url = tempExcludes.split(","); |
| | | for (int i = 0; url != null && i < url.length; i++) |
| | | { |
| | | for (int i = 0; url != null && i < url.length; i++) { |
| | | excludes.add(url[i]); |
| | | } |
| | | } |
| | |
| | | |
| | | @Override |
| | | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| | | throws IOException, ServletException |
| | | { |
| | | throws IOException, ServletException { |
| | | HttpServletRequest req = (HttpServletRequest) request; |
| | | HttpServletResponse resp = (HttpServletResponse) response; |
| | | if (handleExcludeURL(req, resp)) |
| | | { |
| | | if (handleExcludeURL(req, resp)) { |
| | | chain.doFilter(request, response); |
| | | return; |
| | | } |
| | |
| | | chain.doFilter(xssRequest, response); |
| | | } |
| | | |
| | | private boolean handleExcludeURL(HttpServletRequest request, HttpServletResponse response) |
| | | { |
| | | private boolean handleExcludeURL(HttpServletRequest request, HttpServletResponse response) { |
| | | String url = request.getServletPath(); |
| | | String method = request.getMethod(); |
| | | // GET DELETE 不过滤 |
| | | if (method == null || method.matches("GET") || method.matches("DELETE")) |
| | | { |
| | | if (method == null || method.matches("GET") || method.matches("DELETE")) { |
| | | return true; |
| | | } |
| | | return StringUtils.matches(url, excludes); |
| | | } |
| | | |
| | | @Override |
| | | public void destroy() |
| | | { |
| | | public void destroy() { |
| | | |
| | | } |
| | | } |