防盜鏈

原理:判斷http消息頭中的referer是否爲空或者是否不爲localhosthtml

 
代碼放在:jsp文件引入,由於全部數據都是以jsp頁面的形式展現出來的
<%@page import="com.qipai.referer_module.Request_referer" %> //引入包
<%
Request_referer Rr=new Request_referer();
Rr.Referer(request, response);
%>
 
 
代碼:
 //這個函數屬於Request_referer 類 
  public  void Referer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

                   //設置瀏覽器以什麼編碼打開web

                   response.setCharacterEncoding("utf-8");瀏覽器

                   response.setContentType("text/html;utf-8");jsp

                   PrintWriter out=response.getWriter();函數

                   //獲取請求頭編碼

                   String referer=request.getHeader("Referer");spa

                   //判斷這個請求頭是否爲空,或者這個請求頭是否不爲localhostorm

                   if(referer == null || !referer.startsWith("http://localhost")){htm

                            out.println("請登陸");ip

                            response.sendRedirect("http://localhost:8080/QiPaiProject/Login.jsp");

 

}

         }

相關文章
相關標籤/搜索