文章目录
Referer防盗链的原理
Referer防盗链的方法是后台设置了Referer这个值
在请求这个url地址的时候,必须设置后台设置的Referer值,如果不设置,就会打不开这个url
例如app中的图片链接和视频链接,如果在网页中打开就会发现打不开,会显示404错误
但是在自己的app中请求这个链接的时候设置了后台给的reeferer值以后,就可以正常打开.
下面代码中的url,以为是我和后台做实验用的url所以不会长期有效
图片防盗链使用SDWebImage第三方来设置
oc版本图片防盗链
- SDWebImage设置Referer只要一次就行了,之后直接加载图片就行,所以最好Referer和加载图片代码分离
#import "OcVC.h"
#import <SDWebImage.h>
@interface OcVC ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end
@implementation OcVC
- (IBAction)btnAddRefererClick:(id)sender {
//设置Referer只要一次就行了,之后直接加载图片就行,所以最好Referer和加载图片代码分离
SDWebImageDownloader * manager = [SDWebImageManager sharedManager].imageLoader;
[manager setValue:@"enrVnZ3HmgRp2Qx55Ttp" forHTTPHeaderField:@"Referer"];
}
- (IBAction)btnLoadImageClick:(id)sender {
//加载图片
NSString *urlStr = @"http://387v43i951.wicp.vip/image/admin/a0e88e83800f138b94d2414621bd9704.png";
[self.imageV sd_setImageWithURL:urlStr];
}
swift版本图片防盗链
import UIKit
import SDWebImage
class ViewController: UIViewController {
@IBOutlet weak var imageV: UIImageView!
@IBAction func btnAddRefererClick(_ sender: Any) {
//设置Referer一次就可以了.之后再调用多少次加载图片都能正确显示图片
let manager = SDWebImageManager.shared.imageLoader as! SDWebImageDownloader
manager.setValue("enrVnZ3HmgRp2Qx55Ttp", forHTTPHeaderField: "Referer")
}
@IBAction func btnLoadImageClick(_ sender: Any) {
let urlStr = "http://387v43i951.wicp.vip/image/admin/a0e88e83800f138b94d2414621bd9704.png"
if let url = URL(string: urlStr) {
imageV.sd_setImage(with: url, placeholderImage: UIImage(named: "ph"), options: [], completed: nil)
}
}
AVFoundation播放视频防盗链
因为 Referer要添加在header里,header要添加到options里. 方法url和 options 是在同一个初始化AVURLAsset方法里,所以只要url改变,那么Referer就要重新设置一次
oc版本播放视频
#import "OcAvPlayerVC.h"
#import <AVFoundation/AVFoundation.h>
@interface OcAvPlayerVC ()
@property (weak, nonatomic) IBOutlet UIView *PlayerV;
@property (strong,nonatomic)AVPlayer *player;
@property (strong,nonatomic)AVPlayerLayer *playerLayer;
@end
@implementation OcAvPlayerVC
- (IBAction)btnAvPlayerClick:(id)sender {
NSDictionary *header = @{@"Referer":@"enrVnZ3HmgRp2Qx55Ttp"};
NSURL *url = [NSURL URLWithString:@"http://387v43i951.wicp.vip/image/admin/1bba2500a7338de242569eb31e5b4693.mp4"];
AVURLAsset *urlAssert = [AVURLAsset URLAssetWithURL:url options:@{@"AVURLAssetHTTPHeaderFieldsKey" : header}];
AVPlayerItem *playItem = [AVPlayerItem playerItemWithAsset:urlAssert];
self.player = [[AVPlayer alloc] initWithPlayerItem:playItem];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
self.playerLayer.frame = self.PlayerV.bounds;
[self.PlayerV.layer addSublayer:self.playerLayer];
// 播放
[self.player play];
}
@end
swift版本播放视频
import UIKit
import AVFoundation
class swiftPlayerVC: UIViewController {
@IBOutlet weak var playerV: UIView!
var player = AVPlayer()
var playerLayer = AVPlayerLayer()
@IBAction func btnAvPlayerClick(_ sender: Any) {
let header = [
"Referer": "enrVnZ3HmgRp2Qx55Ttp"
]
let url = URL(string: "http://387v43i951.wicp.vip/image/admin/1bba2500a7338de242569eb31e5b4693.mp4")
var urlAssert: AVURLAsset? = nil
if let url = url {
urlAssert = AVURLAsset(url: url, options: [
"AVURLAssetHTTPHeaderFieldsKey": header
])
}
var playItem:AVPlayerItem? = nil
if let urlAssert = urlAssert {
playItem = AVPlayerItem(asset: urlAssert)
}
player = AVPlayer(playerItem: playItem)
playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspect
playerLayer.frame = playerV.bounds
playerV.layer.addSublayer(playerLayer)
// 播放
player.play()
}
}