• 实现一个基本防盗链标签


    1. 标签处理类

     1 public class MyReferer extends BodyTagSupport {
     2     private String site;
     3     private String back;
     4     public String getSite() {
     5         return site;
     6     }
     7     public void setSite(String site) {
     8         this.site = site;
     9     }
    10     public String getBack() {
    11         return back;
    12     }
    13     public void setBack(String back) {
    14         this.back = back;
    15     }
    16     public int doEndTag() throws JspException {
    17         // 获取JSP上下文环境对象
    18         PageContext pageContext = this.pageContext;
    19         // 获取到request对象
    20         HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
    21         // 判断
    22         String header = request.getHeader("referer");
    23         if(header != null && header.startsWith(getSite())){
    24             // 执行后续的页面
    25             return Tag.EVAL_PAGE;
    26         }else{
    27             // 页面的重定向
    28             HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
    29             try {
    30                 response.sendRedirect(getBack());
    31             } catch (IOException e) {
    32                 e.printStackTrace();
    33             }
    34             // 不执行
    35             return Tag.SKIP_PAGE;
    36         }
    37     }
    38 }

    2. 描述文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <taglib 
     3     xmlns="http://java.sun.com/xml/ns/javaee"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
     6     version="2.1">
     7   <!-- 2. 编写标签库描述文件 -->  
     8   <tlib-version>1.0</tlib-version>
     9   <short-name>jnb</short-name>
    10    <tag>  
    11     <name>referer</name>
    12     <tag-class>cn.itcast.custom.MyReferer</tag-class>
    13     <body-content>empty</body-content>
    14      <attribute>
    15         <name>site</name>
    16         <required>true</required>
    17         <rtexprvalue>true</rtexprvalue>
    18     </attribute>
    19      <attribute>
    20         <name>back</name>
    21         <required>true</required>
    22         <rtexprvalue>true</rtexprvalue>
    23     </attribute>
    24   </tag>
    25 </taglib>    

    3. 引入和使用

    1 <%@taglib uri="/WEB-INF/referer.tld" prefix="my"%>
    2    <my:referer site=http://localhost:8080/day11/list.jsp
    3  back="/day11/list.jsp"/>
  • 相关阅读:
    paip.erlang 文本文件读写操作attilax总结
    paip.python错误解决20
    paip.python错误解决8
    paip. sip module implements API v10.0 to v10.1 but the PyQt4.QtCore module requires API v9.2
    解读NoSQL数据库的四大家族
    paip.python错误解决9
    paip.python 执行shell 带空格命令行attilax总结
    paip.python错误解决15
    paip.python错误解决24
    paip.python优缺点attilax总结
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3736658.html
Copyright © 2020-2023  润新知