当前位置: 首页 > news >正文

nginx播放视频(auth_request鉴权)

学习链接

Nginx通过auth_request结合Springboot实现静态文件下载鉴权

nginx搭建直播推流服务&推流拉流鉴权

步骤

1、安装nginx

这里nginx的版本是nginx-1.24.0

./configure  --with-http_ssl_module  --with-stream  --with-stream_ssl_module  --with-http_auth_request_module
make
make install
# 默认安装在/usr/local/nginx

2、配置nginx


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$arg_token'
                      '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        # 鉴权服务地址
        location = /auth {
            internal;  # 仅允许内部请求
            proxy_pass http://192.168.134.5:8080/auth;  # 鉴权服务地址
            proxy_pass_request_body off;  # 不传递请求体
            proxy_set_header Content-Length "";  # 清空 Content-Length
            proxy_set_header X-Original-URI $request_uri;  # 传递原始请求路径
            proxy_set_header x-token $token;  # 传递 Token 参数, 这里使用$arg_token拿不到值,所以只能用自定义变量了
            proxy_set_header X-Original-Method $request_method;  # 传递请求方法
        }
        
        # 视频点播路径
        location /videos/ {
        
            # 将请求参数设置给指定变量
            set $token $arg_token;
        
            alias /usr/local/nginx/vod/;

            # 启用鉴权
            auth_request /auth;  # 发送鉴权请求
            auth_request_set $auth_status $upstream_status;  # 保存鉴权结果

            # 如果鉴权失败,返回 401 或 403
            error_page 401 = @error401;
            error_page 403 = @error403;
            
            # 记录鉴权失败的请求,便于排查问题。
            error_log /user/local/nginx/log/auth_error.log;
        }
        
        # 自定义错误页面
        location @error401 {
            return 401 "Unauthorized";
        }

        location @error403 {
            return 403 "Forbidden";
        }

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3、后端代码

@Slf4j
@RestController
public class VodAuthController {

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping("/auth")
    public void auth() {
        System.out.println("auth...");

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            System.out.println(headerName + " - " + request.getHeader(headerName));
        }

        String token= request.getHeader("x-token");

        if ("123".equals(token)) {
            log.info("鉴权通过!!!");
            response.setStatus(200);
        } else {
            log.info("鉴权失败!!!");
            response.setStatus(403);
        }

    }
}

4、dplayer播放mp4视频html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.css">
    <script src="https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.js"></script>
</head>
<body>
    <div id="dplayer" style="width: 800px;height: 500px;"></div>
    
</body>
<script>
    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'http://192.168.134.3/videos/netty03.mp4?token=123'
        },
    });
</script>
</html>

5、测试

在这里插入图片描述
此时后台输出

...
auth...
x-original-uri - /videos/netty03.mp4?token=123
x-token - 123
x-original-method - GET
host - 192.168.134.5:8080
connection - close
pragma - no-cache
cache-control - no-cache
user-agent - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
accept-encoding - identity;q=1, *;q=0
accept - */*
referer - http://127.0.0.1:5500/
accept-language - en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7
range - bytes=165478400-
 INFO 320 --- [io-8080-exec-10] com.zzhua.demo5.VodAuthController        : 鉴权通过!!!
 ...

相关文章:

  • 人工智能行为识别之slowfast源码解读
  • Linux Mem -- MTE in AArch64 Linux
  • 树莓集团全国拓展:产业园服务与人才培养的协同发展
  • 【开源项目】数字孪生武汉~超经典智慧城市CIM/BIM数字孪生可视化项目——开源工程及源码
  • DeepSeek操作Excel,实现图表自动化生成
  • 实用教程:用微服务搭建你的2025项目
  • HDFS应用-后端存储cephfs-文件存储和对象存储数据双向迁移
  • Node.js NativeAddon 构建工具:node-gyp 安装与配置完全指南
  • uni-app中常用的指令语句有哪些?
  • 获取 Windows 视频时长的正确方式——Windows Shell API 深度解析
  • vscode C/C++ CMake 调试
  • huggingface.co 网站无法访问的解决办法
  • TestHubo基础教程-创建项目
  • 如何评估云原生GenAI应用开发中的安全风险(上)
  • 如何优化React应用的性能?
  • Navicat 迁移数据库 传输数据
  • 光伏设计软件分类:无人机、Unity3D引擎齐上阵
  • OpenAI API测试
  • vue3: const一个function怎么写呢?
  • 顺序表SeqList(c语言)(动态顺序表)
  • 最高法:依法惩治损害民营企业合法权益的串通投标行为
  • 看展 | 黄永玉新作展,感受赤子般的生命力
  • 美国贸易政策|特朗普模式:你想做交易吗?
  • 大外交丨3天拿下数万亿美元投资,特朗普在中东做经济“加法”和政治“减法”
  • 查幽门螺杆菌的这款同位素长期被海外垄断,秦山核电站实现突破
  • 商务部新闻发言人就暂停17家美国实体不可靠实体清单措施答记者问