public class ImgFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器初始化...");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("doFilter...");
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String referer = req.getHeader("referer");
// 请求服务名称
String serverName = req.getServerName();
System.out.println("referer:" + referer + ",serverName:" + serverName);
if (referer == null || !(referer.contains(serverName))) {
//
req.getRequestDispatcher("error.png").forward(req, res);
return ;
}
//放行
chain.doFilter(req, res);
}
public void destroy() {
}
}
简单的防盗图片过滤器代码
原文地址:https://www.cnblogs.com/liclBlog/p/9419865.html