java WEB Referer防盗链实例化代码

  1. package cn.com;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. public class RequestPreventThreif extends HttpServlet {
  8.     private static final long serialVersionUID = 1L;
  9.    //防盗链:用于保护自己独有的消息,防止被其他的网站直接连接浏览,利于保护自己的信息
  10.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11.         String referer=request.getHeader(“referer”);
  12.         if(referer==null||!referer.startsWith(“http://localhost”))
  13.         {
  14.             response.sendRedirect(“/Request/Index.html”);
  15.             return;
  16.         }
  17.         String data=”haha”;
  18.         response.getWriter().write(data);
  19.     }
  20.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  21.         doGet(request,response);
  22.     }
  23. }

标签