HTTP协议和防盗链
2021-04-20 07:26
标签:二进制 auth https 事务 提交 服务 config request 进制 http 协议: 防盗链: HTTP协议和防盗链 标签:二进制 auth https 事务 提交 服务 config request 进制 原文地址:https://www.cnblogs.com/F017/p/12260719.htmlpackage com.wdksoft;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 过滤图片请求
* @author FLC
*
*/
public class ImageFilter implements Filter{
@Override
public void destroy() {
System.out.println("过滤器销毁");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("dofilter");
//获取到当前请求的连接地址和上一个发送请求的地址
HttpServletRequest httprequest=(HttpServletRequest)request;
HttpServletResponse httpresponse=(HttpServletResponse)response;
//获取上一个发送请求的连接
String referer = httprequest.getHeader("Referer");
String serverName = httprequest.getServerName();
System.out.println(referer+"\t"+serverName);
if(referer==null||!referer.contains(serverName)) {
request.getRequestDispatcher("/img/002.png").forward(request, response);
return;
}
//放行
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("过滤器启动");
}
}