English
 电子信箱
 加入收藏

  威盾防火墙 >> 新闻中心 >> 业界动态 >> 深入体验JavaWeb开发内幕——使用Request对象设置防盗链

 

深入体验JavaWeb开发内幕——使用Request对象设置防盗链

威盾防火墙 2014-12-22

 

使用Request对象设置页面的防盗链

      所谓的防盗链就是当你以一个非正常渠道去访问某一个Web资源的时候,服务器会将你的请求忽略并且将你的当前请求变为按正常渠道访问时的请求并返回到相应的页面,用户只有通过该页面中的相关操作去访问想要请求的最终资源。

        例如,你有一个访问某资源的网址,但是你事先不知道这个网址是有防盗链的,那么当你输入该网址时你可能会发现,并没有马上跳转到你想要的资源页面而是一些无关的信息页面,但是就是在这些信息页面中你发现有一个超链接或是其他操作可以跳转到你所访问的最终资源页面。


      这就是防盗链技术了,好了来看一个具体应用:

  

  1. Request.java  
  2.   
  3.   
  4. package net.csdn.request;import java.io.IOException;  
  5. import java.io.PrintWriter;import java.util.Enumeration  
  6. import javax.servlet.RequestDispatcher;  
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11. public class Request extends HttpServlet {  
  12. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  13. throws ServletException, IOException {getDoorChain(request, response);}  
  14.   
  15.     private void getDoorChain(HttpServletRequest request,  
  16.             HttpServletResponse response) throws IOException {  
  17.         String referer = request.getHeader("referer");  
  18.         if(referer==null || !referer.endsWith("http://localhost:8080/Request/index.jsp")){  
  19.             response.sendRedirect("http://localhost:8080/Request/index.jsp");  
  20.             return;  
  21.         }  
  22.         response.setCharacterEncoding("utf-8");  
  23.         response.setContentType("text/html;charset =utf-8");  
  24.         PrintWriter pw = response.getWriter();  
  25.         pw.write("喜剧片《东成西就》");  
  26.     }  
  27. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  28.             throws ServletException, IOException {  
  29.         doGet(request, response);  
  30.     }  
  31.   
  32. }  



  1. index.jsp  
  2.   
  3. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"  
  7. +request.getServerPort()+path+"/";  
  8. %>  
  9.   
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  11. <html>  
  12.   <head>  
  13.     <base href="<%=basePath%>">  
  14.       
  15.     <title>My JSP 'index.jsp' starting page</title>  
  16.     <meta http-equiv="pragma" content="no-cache">  
  17.     <meta http-equiv="cache-control" content="no-cache">  
  18.     <meta http-equiv="expires" content="0">      
  19.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  20.     <meta http-equiv="description" content="This is my page">  
  21.     <!-- 
  22.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  23.     -->  
  24.   </head>  
  25.     
  26.   <body>  
  27.   这里是防盗链技术的应用检测! <br>  
  28.   <a href ="/Request/Request" >喜剧片 </a>  
  29.     
  30.   </body>  
  31. </html>  

效果如图:

例如我最终想要通过http://lcoalhost:8080/Request/Request这个网址获取到我想要的《东成西就》
的资源可是当我真正的输入这个网址时,却转到了:
http://localhost:8080/Request/index.jsp这个页面



只有当你点击“喜剧片”这个超链接时才会真正的得到你想要的资源页面即:

 


好了赶快自己动手试一试吧!







  1. <span style="font-size:18px;"><strong>  
  2. </strong></span><span style="font-size:18px;"></span><pre></pre>  
  3. <pre></pre> 

相关内容: 最新内容:
纠正网上 .htaccess 图片防盗链方法的错误[2014-12-22]
IIS下图片文件防盗链的办法[2014-12-22]
如何在ASP.NET中实现防盗链[转][2014-12-22]
如何破解对方网站的图片防盗链[2014-12-22]
泛解析域名 iis7.5 配置web.config防盗链[2014-12-22]
关于IIS防盗链[2014-12-22]
纠正网上 .htaccess 图片防盗链方法的错误[2014-12-22]
IIS 6.0所需要的默认ACLs权限[即NTFS的硬盘权限][2014-12-22]
IIS下搭建PHP5运行环境[2014-12-22]
IIS修复批处理[2014-12-22]
IIS下图片文件防盗链的办法[2014-12-22]
如何在ASP.NET中实现防盗链[转][2014-12-22]