防盗链的解决方法.docx

上传人:b****8 文档编号:10341088 上传时间:2023-02-10 格式:DOCX 页数:15 大小:19.36KB
下载 相关 举报
防盗链的解决方法.docx_第1页
第1页 / 共15页
防盗链的解决方法.docx_第2页
第2页 / 共15页
防盗链的解决方法.docx_第3页
第3页 / 共15页
防盗链的解决方法.docx_第4页
第4页 / 共15页
防盗链的解决方法.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

防盗链的解决方法.docx

《防盗链的解决方法.docx》由会员分享,可在线阅读,更多相关《防盗链的解决方法.docx(15页珍藏版)》请在冰豆网上搜索。

防盗链的解决方法.docx

防盗链的解决方法

防盗链的解决方法:

(一)创建一个继承了System.Web.IHttpHandler接口的类

在System.Web.IHttpHandler接口有两个成员ProcessRequest()方法和IsReusable()属性。

ProcessRequest(System.Web.HttpContext context) 方法,除了用户自定义中被要求处理的特殊的http请求。

其中的参数 System.Web.HttpContext 类的实例装入了一个http请求中http协议中要求的所有信息。

其中System.Web.HttpContext 类中包含有属性 Request 使得从客户端发送过来的http请求信息的值可以被方便地读取;属性Response 它封装了需要返回给客户端的信息和操作。

IsReusable()属性要置为true.

(二) 创建一个资源图片的方法Real()

1.若是盗链服务器给其错误图片。

2.若不是盗链服务器给其正常的图片。

在Real()方法中用到了FileInfo 这个类去取得图片的扩展名并用来判断图片的类型.

(三)  注意页面的缓存(response.Expires=1;获取和设置浏览器上缓存的页过期之前的分钟数.如果用户在页过期之前返回同一页,则显示缓存的内容)

(四)配置应用程序扩展名映射

因为IIS在接收到aspx页面请求时,会为其自动加载一个C:

\WINDOWS\Micosoft.NET\Framework\v1.1.4322

\aspnet_isapi.dll文件。

虚拟目录和文件夹的属性里面的”执行许可”改为“纯脚本”。

若是有多个Web.config那么只要在总目录下有就可以了,其它地方的均可删除。

(五)在 Web.config这个网络应用程序配置文件中加入注册信息

        

\.ppm\.rgb\.xbm\.xpm\.xwd\.did\.ico\.emf"type="HandlerExample.HttphandlerTest,HandlerExample"/>

.

.

 

源码:

ASP.NET 防盗链源码

/* 

* 防盗链IHttpHandler 

* 增加了对文件关键字的选择(即仅对文件名存在某些关键字或不存在某些关键字进行过滤) 

* 设置web.config中节以下值 

* string eWebapp_NoLink 如果文件名符合该正确表态式将进行过滤(不设置对所有进行过滤) 

* string eWebapp_AllowLink 如果文件名符合该正确表态式将不进行过滤(优先权高于AllowLink,不设置则服从AllowLink) 

* bool eWebapp_ AllowOnlyFile 如果为False,(默认true)则不允许用户直接对该文件进行访问建议为true 

* :

)以下设置均可省略,设置只是为了增加灵活性与体验 

* eWebapp_NoLink_Message 错误信息提示:

默认为Link From:

域名 

* eWebapp_Error_Width 错误信息提示图片宽 

* eWebapp_Error_Height 错误信息提示图片高 

* 垃圾猪 2005-9-11 创建 

*/ 

using System; 

using System.Web; 

using System.Drawing; 

using System.Drawing.Imaging; 

using System.IO; 

using System.Configuration; 

using System.Text.RegularExpressions; 

namespace eWebapp 

/// 

 

/// 防盗链IHttpHandler 

/// 垃圾猪 2005-9-12 修正 

/// 

 

public class NoLink :

 IHttpHandler 

private string eWebapp_NoLink = string.Empty; 

private string eWebapp_AllowLink = string.Empty; 

private bool eWebapp_AllowOnlyFile = true; 

private string eWebapp_NoLink_Message = string.Empty; 

private bool error = false; 

public NoLink() 

// 

// TODO:

 在此处添加构造函数逻辑 

// 

public void ProcessRequest(HttpContext context) 

eWebapp_NoLink_Message = ConfigurationSettings.AppSettings["eWebapp_NoLink_Message"]; 

string myDomain = string.Empty; 

error = errorLink(context,out myDomain); 

if(Empty(eWebapp_NoLink_Message)) 

eWebapp_NoLink_Message = "Link from :

" + myDomain; 

if(error) 

//Jpg(context.Response,eWebapp_NoLink_Message); 

Jpg(context.Response,eWebapp_NoLink_Message); 

else 

Real(context.Response,context.Request); 

public bool IsReusable 

get 

return true; 

/// 

 

/// 输出错误信息 

/// 

 

///  

///  

private void Jpg(HttpResponse Response,string _word) 

int myErrorWidth = _word.Length*15; 

int myErrorHeight = 16; 

try 

int _myErrorWidth = Convert.ToInt32(ConfigurationSettings.AppSettings["eWebapp_Error_Width"]); 

if(_myErrorWidth > 0 ) 

myErrorWidth = _myErrorWidth; 

catch 

try 

int _myErrorHeight = Convert.ToInt32(ConfigurationSettings.AppSettings["eWebapp_Error_Height"]); 

if(_myErrorHeight > 0 ) 

myErrorHeight = _myErrorHeight; 

catch 

Bitmap Img=null; 

Graphics g=null; 

MemoryStream ms=null; 

Img=new Bitmap(myErrorWidth,myErrorHeight); 

g=Graphics.FromImage(Img); 

g.Clear(Color.White); 

Font f=new Font("Arial",9); 

SolidBrush s=new SolidBrush(Color.Red); 

g.DrawString(_word,f,s,3,3); 

ms=new MemoryStream(); 

Img.Save(ms,ImageFormat.Jpeg); 

Response.ClearContent(); 

Response.ContentType="image/Gif"; 

Response.BinaryWrite(ms.ToArray()); 

g.Dispose(); 

Img.Dispose(); 

Response.End(); 

/// 

 

/// 输出真实文件 

/// 

 

///  

///  

private void Real(HttpResponse response,HttpRequest request) 

FileInfo file = new System.IO.FileInfo(request.PhysicalPath); 

response.Clear(); 

response.AddHeader("Content-Disposition", "filename=" + file.Name); 

response.AddHeader("Content-Length", file.Length.ToString()); 

string fileExtension = file.Extension.ToLower(); 

//这里选择输出的文件格式 

switch (fileExtension) 

case "mp3":

 

response.ContentType = "audio/mpeg3"; 

break; 

case "mpeg":

 

response.ContentType = "video/mpeg"; 

break; 

case "jpg":

 

response.ContentType = "image/jpeg"; 

break; 

case "bmp":

 

response.ContentType = "image/bmp"; 

break; 

case "gif":

 

response.ContentType = "image/gif"; 

break; 

case "doc":

 

response.ContentType = "application/msword"; 

break; 

case "css":

 

response.ContentType = "text/css"; 

break; 

default:

 

response.ContentType = "application/octet-stream"; 

break; 

response.WriteFile(file.FullName); 

response.End(); 

/// 

 

/// 确认字符串是否为空 

/// 

 

///  

///  

private bool Empty(string _value) 

if(_value == null | _value == string.Empty | _value == "") 

return true; 

else 

return false; 

/// 

 

/// 检查是否是非法链接 

/// 

 

///  

///  

///  

private bool errorLink(HttpContext context,out string _myDomain) 

HttpResponse response = context.Response; 

string myDomain = context.Request.ServerVariables["SERVER_NAME"]; 

_myDomain = myDomain ; 

string myDomainIp = context.Request.UserHostAddress; 

eWebapp_NoLink = ConfigurationSettings.AppSettings["eWebapp_NoLink"]; 

eWebapp_AllowLink = ConfigurationSettings.AppSettings["eWebapp_AllowLink"]; 

try 

eWebapp_AllowOnlyFile = Convert.ToBoolean(ConfigurationSettings.AppSettings["eWebapp_AllowOnlyFile"]); 

catch 

eWebapp_AllowOnlyFile = true; 

if(context.Request.UrlReferrer !

= null) 

//判定referDomain是否存在网站的IP或域名 

string referDomain = context.Request.UrlReferrer.AbsoluteUri.Replace(context.Request.UrlReferrer.AbsolutePath,""); 

string myPath = context.Request.RawUrl; 

if(referDomain.IndexOf(myDomainIp) >=0 | referDomain.IndexOf(myDomain)>=0) 

return false; 

else 

//这里使用正则表达对规则进行匹配 

try 

Regex myRegex ; 

//检查允许匹配 

if(!

Empty(eWebapp_AllowLink)) 

myRegex = new Regex(eWebapp_AllowLink); 

if(myRegex.IsMatch(myPath)) 

return false; 

//检查禁止匹配 

if(!

Empty(eWebapp_NoLink)) 

myRegex = new Regex(eWebapp_NoLink); 

if(myRegex.IsMatch(myPath)) 

return true; 

else 

return false; 

return true; 

catch 

//如果匹配出错,链接错误 

return true; 

else 

//是否允许直接访问文件 

if(eWebapp_AllowOnlyFile) 

return false; 

else 

return true; 

}

}

}

}

}

另一个源码例子:

.cs文件

/**********************************************************************/

                      大文件下载,防盗链                    

/**********************************************************************/

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.IO;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclassGetFile:

System.Web.UI.Page

{

   protectedvoidPage_Load(objectsender,EventArgse)

   {

       if(Request.QueryString["FileName"]==null)

       {

           InvalidRedirect();//返回首页

       }

       stringfileName=Request.QueryString["FileName"];

       if(fileName==string.Empty)

       {

           InvalidRedirect();//返回首页

       }

       //判断配置文件是否直接下载                 

       stringdownDirect=ConfigurationManager.AppSettings["Down"].ToLower();

       if(downDirect=="true")

       {

           UpdateHits(fileName);  //更新下载次数

           Response.Redirect("Upload/"+fileName);

           return;

       }

       //如果不是直接下载

       stringpath=Server.MapPath(Request.ApplicationPath+"/Upload/"+fileName);

       stringreferrer=string.Empty;

       if(Request.UrlReferrer!

=null)

       {

           referrer=Request.UrlReferrer.ToString().ToLower();

       }

       stringd=ConfigurationManager.AppSettings["Valid"].ToLower();

       string[]domainName=ConfigurationManager.AppSettings["Refers"].ToLower().Split(newchar[]{','});

       //如果设置为防止盗链,判断访问来源是否合法

       if(d=="false")

       {

           foreach(stringsindomainName)

           {

               if(referrer.IndexOf(s.ToLower())>0)

               {

                   UpdateHits(fileName);/

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 经济学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1