基本的な盗難防止チェーンラベルを実現

12345 ワード

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"/>