欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java request 防止盗链

程序员文章站 2022-06-24 18:14:11
...
package cn.com;  
  
import java.io.IOException;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
public class RequestPreventThreif extends HttpServlet {  
    private static final long serialVersionUID = 1L;  
         
   //防盗链:用于保护自己独有的消息,防止被其他的网站直接连接浏览,利于保护自己的信息  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        String referer=request.getHeader("referer");  
        if(referer==null||!referer.startsWith("http://localhost"))  
        {  
            response.sendRedirect("/Request/Index.html");  
            return;  
        }  
        String data="haha";  
        response.getWriter().write(data);  
    }  
  
      
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        doGet(request,response);  
    }  
  
}