防盗链的开发:
原理: 获取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>
原理: 获取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>
---------------------------------------------------------------------------------------------------