防盗链的开发

防盗链的开发:
      原理:  获取referer头,判断是否为允许访问的连接,若是,则可以直接访问,
                   若不是,则先看广告,再进连接或提示图片直接停掉。
      注意:  绝对路径和相对路径的不同写法


---------------------------------------------------------------------------------------------------
index.jsp:
      <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
      <%@ taglib uri="http://www.itcast.cn/jsp/tag/example" prefix="eitcast"%>
      <eitcast:referer site="http://localhost:8080/day18" adPage="ad.jsp" />


      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
        <head>
            <title>主页</title> 
            <meta http-equiv="pragma" content="no-cache">
            <meta http-equiv="cache-control" content="no-cache">
            <meta http-equiv="expires" content="0">    
        </head>
        <body>
              艳照门
        </body>
      </html>
---------------------------------------------------------------------------------------------------
ad.jsp:
      <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
        <head>
            <title>广告</title>        
            <meta http-equiv="pragma" content="no-cache">
            <meta http-equiv="cache-control" content="no-cache">
            <meta http-equiv="expires" content="0">    
        </head>
        <body>
              CGS拍照相机不错偶!<br/>
              <a href="http://localhost:8080/day18/example/index.jsp">进入主页</a>    
        </body>
      </html>
---------------------------------------------------------------------------------------------------


public class RefererSimpleTag extends SimpleTagSupport {


      // <eitcast:referer site="http://localhost:8080/day18" adPage="ad.jsp" />
          // 定义属性
        private String site;   // 允许直接访问的网站    
        private String adPage; // 广告页面, 非法盗链者要先看广告
        
        public void setSite(String site) {    
                this.site = site;
        }
        public void setAdPage(String adPage) {
                this.adPage = adPage;
        }


        @Override
        public void doTag() throws JspException, IOException {
                PageContext pc = (PageContext) getJspContext();
                HttpServletRequest request = (HttpServletRequest) pc.getRequest();
                HttpServletResponse response = (HttpServletResponse) pc.getResponse();
                // 得到请求的referer头  需要request对象
                String refererValue = request.getHeader("referer");
                // 如果不是null,并且不是以site开头的,重定向(需要response对象)到广告页面,主页的内容不执行
                if(refererValue!=null){
                        if(!refererValue.startsWith(site)){
                                if(adPage.startsWith("/")){ 
                                        // 绝对路径: <eitcast:referer site="http://localhost:8080/day18" adPage="/example/ad.jsp" />               
                                        response.sendRedirect(request.getContextPath()+adPage);
                                }else{
                                        // 广告页面的相对路径: <eitcast:referer site="http://localhost:8080/day18" adPage="ad.jsp" />
                                        // 获取当前连接的路径URI:  /day18/example/index.jsp
                                        String uri = request.getRequestURI();   
                                        // 找到路径: /day18/example
                                        String path = uri.substring(0,uri.lastIndexOf("/"));   
                                        response.sendRedirect(path + "/" + adPage);
                                }
                        }
                }
         }      
}
---------------------------------------------------------------------------------------------------
<tag>
        <name>referer</name>
        <tag-class>cn.itcast.etag.RefererSimpleTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
              <name>site</name>
              <required>true</required>
              <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
              <name>adPage</name>
              <required>true</required>
              <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

---------------------------------------------------------------------------------------------------









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值