使用简单标签解决防盗链问题
2011-11-09 21:34
375 查看
使用简单标签解决防盗链问题
一 思路分析
防盗链就是不让自己的网站通过别的网站访问,而应该是通过自己网站的首页再访问自己网站的东西
下面创建了两个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 = (H
4000
ttpServletResponse)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 "-//SunMicrosystems, 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.01Transitional//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.01Transitional//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>
一 思路分析
防盗链就是不让自己的网站通过别的网站访问,而应该是通过自己网站的首页再访问自己网站的东西
下面创建了两个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 = (H
4000
ttpServletResponse)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 "-//SunMicrosystems, 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.01Transitional//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.01Transitional//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>
相关文章推荐
- csshover.htc 以及简单使用方法(解决IE6下 :hover 在其他标签下无效问题)
- 简单标签解决防盗链问题源码
- 在页面中使用WebWork的token标签解决表单重复提交问题
- Flex中如何使用A标签解决Text中给部分文字加连接的问题。
- ASP.Net MVC——DotNetZip简单使用,解决文件压缩问题。
- (C#)使用队列(Queue)解决简单的并发问题
- 简单解决微信文章图片防盗链问题
- 使用Redis模拟简单分布式锁,解决单点故障的问题
- ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题
- spring使用@Value标签读取.properties文件的中文乱码问题的解决
- 解决“允许使用简单密码注册用户”后出现的问题
- 优化 html 标签 为何能用HTML/CSS解决的问题就不要使用JS?
- 用质数解决数据库两表需要中间表的问题如此解决更新用户的标签和统计标签使用数量问题。
- s:action标签不能使用问题解决
- C#使用读写锁三行代码简单解决多线程并发写入文件时线程同步的问题
- 一步一步SharePoint 2007之十八:解决允许使用简单密码注册用户的问题
- 使用jstl标签过程中出现问题的解决
- AIDL的简单使用,涵盖5.0之后系统及魅族手机调用问题的解决
- 分享一个 jsPDF的简单使用以及中文编码问题的解决
- 一步一步SharePoint 2007之十八:解决允许使用简单密码注册用户的问题