过滤器与监听器专题

防盗链过滤器

2018-08-23  本文已影响0人  神坛下的我

UseFilter.java

@WebFilter("/*")
public class UseFilter implements Filter{
    //private FilterConfig filterConfig;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //this.filterConfig = filterConfig;
    }
    
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain filterChain) throws IOException, ServletException {
            
            HttpServletRequest request=(HttpServletRequest) req;
            HttpServletResponse response=(HttpServletResponse) resp;
            String imurl = request.getHeader("imurl");//连接的来源地址
            if(imurl==null||!imurl.contains(request.getServerName())){//判断访问来源
            request.getRequestDispatcher("/images/a.PNG").forward(request, response);
            }else {
                filterChain.doFilter(request, response);
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
    }
    
    @Override
        public void destroy() {
            
        }
    
}
7.PNG 8.PNG
上一篇下一篇

猜你喜欢

热点阅读