簡單的防盜鏈技術(過濾器原理)
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() {
}
}
簡單的防盜圖片過濾器代碼
簡單的防盜鏈技術(過濾器原理)