package com.njcn.web.filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author hongawen
 * @version 1.0.0
 * @createTime 2021年05月20日 16:33
 */
public class XssFilter implements Filter {

    private static final String[]  EXCLUSIONS_URLS = {".js", ".gif", ".jpg", ".png", ".css", ".ico", ".dll", ".so"};


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletRequest req = (HttpServletRequest) request;
        String path = req.getServletPath();
        req.getRequestURL();
        //注解配置的是urlPatterns="/*"(过滤所有请求),所以这里对不需要过滤的静态资源url,作忽略处理(大家可以依照具体需求配置)
        for (String str : EXCLUSIONS_URLS) {
            if (path.endsWith(str)) {
                chain.doFilter(request, response);
                return;
            }
        }
        chain.doFilter(new XssRequestWrapper(httpServletRequest), response);
    }
}
