目錄:html
設置響應消息頭:refresh,實現5秒後 自動跳轉 index.htmljava
設置響應消息頭狀態碼302,實現請求 重定向web
判斷請求消息頭,referer,實現防盜鏈post
利用URL對象,僞造referer消息頭,破解防盜鏈機制ui
*注:Servlet在web.xml文件中有配置,注意查看url
設置響應消息頭:refresh,實現5秒後 自動跳轉 index.htmlspa
package cn.itcast.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 5秒後 自動跳轉 index.html * @author seawind * */ public class RefreshServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置refresh response.setHeader("refresh", "5;url=index.html"); // 顯示提示信息 response.setContentType("text/html;charset=utf-8"); response.getWriter().println("網頁會在5秒後 跳轉 index.html"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
設置響應消息頭狀態碼302,實現請求 重定向.net
package cn.itcast.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 完成請求 重定向 * @author seawind * */ public class RedirectServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置狀態碼 302 response.setStatus(302); // 指定 重定向頁面地址 response.setHeader("Location", "img.html"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
判斷請求消息頭,referer,實現防盜鏈3d
htmlcode
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="referer">特價商品</a> </body> </html>
Servlet
package cn.itcast.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 經過/referer 訪問程序 * @author seawind * */ public class RefererServlet extends HttpServlet { // 處理get方式請求 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 判斷請求中referer是否存在,有效 --- 防止盜鏈 String referer = request.getHeader("referer"); if(referer!=null && referer.equals("http://localhost/day4/index.html")){ // 有效 response.setContentType("text/html;charset=gbk"); response.getWriter().println("筆記本1000元"); }else{ // 無效 response.setContentType("text/html;charset=gbk"); response.getWriter().println("盜鏈真無恥!"); } } // 處理post方式請求 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
利用URL對象,僞造referer消息頭,破解防盜鏈機制
package cn.itcast.client; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class URLClient { public static void main(String[] args) throws IOException { // 創建訪問目標URL對象 URL url = new URL("http://localhost/day4/referer"); // 創建目標URL鏈接 URLConnection urlConnection = url.openConnection(); // 僞造referer urlConnection.addRequestProperty("referer", "http://localhost/day4/index.html"); // 抓取響應內容 byte[] buf = new byte[8192]; int len = urlConnection.getInputStream().read(buf); // 輸出內容 System.out.println(new String(buf,0,len)); } }