简单标签解决防盗链问题源码

 

简单标签解决防盗链问题

一  思路分析

   防盗链就是不让自己的网站通过别的网站访问,而应该是通过自己网站的首页再访问自己网站的东西

    下面创建了两个jsp页面,一个java页面,jsp页面是为了模拟首页和点击首页进去的网页。Java页面是编写标签处理器

二  源码

1.创建标签处理器

类名为RefererTag.java

 

public class RefererTag extends SimpleTagSupport {

    private String site;

    private String page;

 

    public void setSite(String site) {

       this.site = site;

    }

    public void setPage(String page) {

       this.page = page;

    }

 

    @Override

    public void doTag() throws JspException, IOException {

       // 获取request对象

       JspContext jc = this.getJspContext();

       PageContext pc = (PageContext) jc;

       HttpServletRequest request = (HttpServletRequest) pc.getRequest();

 

       HttpServletResponse response = (HttpServletResponse) pc.getResponse();

 

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

 

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

           // 跳转到首页

           //获取地址的完整路径

           if (page.startsWith(request.getContextPath())) {

              response.sendRedirect(page);

           } else if (page.startsWith("/")) {//获取加/的路径

              response.sendRedirect(request.getContextPath() + page);

           } else {

              //最不完整的路径

              response.sendRedirect(request.getContextPath() + "/" + page);

           }

           // 不运行一下的代码

           throw new SkipPageException();

       }

    }

2.部署tld文件为:dhw.tld

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"

                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>

    <tlib-version>1.0</tlib-version>

    <jsp-version>1.2</jsp-version>

    <short-name>dhw</short-name>

    <uri>http://www.dhw.com</uri>

    <tag>

       <name>referer</name>

       <tag-class>com.hbsi.web.tag.project.RefererTag</tag-class>

       <body-content>empty</body-content>

       <attribute>

           <name>site</name>

           <required>true</required>

           <rtexprvalue>true</rtexprvalue>

       </attribute>

       <attribute>

           <name>page</name>

           <required>true</required>

           <rtexprvalue>true</rtexprvalue>

       </attribute>

    </tag>

</taglib>

3. 创建TestReferer.jsp页面和index.jsp页面(首页)

(1)TestReferer.jsp---是应该通过首页访问到的页面,如果没有经过首页就访问,则会跳转到首页,在标签处理器中判断

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib uri="http://www.dhw.com"  prefix="dhw"%>

<!-- 防盗链的网站和跳转的页面 -->

<dhw:referer site="http://localhost" page="/index.jsp"/>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'TestReferer.jsp' starting page</title>

   

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

  <!--  -->

  <body>明星信息----------------

  </body>

</html>

(2)index.jsp----首页

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'index.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

  <body>

    This is my JSP page. <br>

    <a href="/CustomerTagDemo/example/TestReferer.jsp">明星档案</a>

  </body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
防盗链是指为了阻止其他网站或用户在未经授权的情况下盗用自己网站的资而采取的一种措施。在Python中,可以通过设置请求头中的"Referer"字段来进行防盗链。 在给定的代码中,通过设置请求头的"referer"字段来模拟请求时的来页面。这样,被请求的服务器可以根据Referer字段来判断请求的合法性。如果请求的Referer字段与服务器期望的来页面匹配,则服务器会正常返回请求的资。否则,服务器可能会返回错误或者拒绝提供资。 具体来说,在代码中的header字典中设置了"referer"字段的值为"https://www.pearvideo.com/video_1160135",这是一个示例的来页面链接。根据实际情况,可以将该字段的值设置为合法的来页面链接。 在请求中,会根据提供的链接获取视频资的URL,并进行一些拼接替换操作,最终得到视频的真实URL。然后可以使用Python的requests库发送GET请求,获取到视频资的内容,并进行保存。 总结起来,这段Python代码通过设置请求头的Referer字段来模拟合法的请求来,从而实现防盗链的效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python爬虫学习--防盗链](https://blog.csdn.net/weixin_30675247/article/details/95668172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python--爬虫,了解防盗链-->解决防盗链,解决反反反爬](https://blog.csdn.net/qq_57663276/article/details/127474123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值