nginx播放视频(auth_request鉴权)

打印 上一主题 下一主题

主题 896|帖子 896|积分 2688

学习链接

Nginx通过auth_request结合Springboot实现静态文件下载鉴权
nginx搭建直播推流服务&推流拉流鉴权
步调

1、安装nginx

这里nginx的版本是nginx-1.24.0
  1. ./configure  --with-http_ssl_module  --with-stream  --with-stream_ssl_module  --with-http_auth_request_module
  2. make
  3. make install
  4. # 默认安装在/usr/local/nginx
复制代码
2、配置nginx

  1. #user  nobody;
  2. worker_processes  1;
  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;
  6. #pid        logs/nginx.pid;
  7. events {
  8.     worker_connections  1024;
  9. }
  10. http {
  11.     include       mime.types;
  12.     default_type  application/octet-stream;
  13.     log_format  main  '$arg_token'
  14.                       '$remote_addr - $remote_user [$time_local] "$request" '
  15.                       '$status $body_bytes_sent "$http_referer" '
  16.                       '"$http_user_agent" "$http_x_forwarded_for"';
  17.     access_log  logs/access.log  main;
  18.     sendfile        on;
  19.     #tcp_nopush     on;
  20.     #keepalive_timeout  0;
  21.     keepalive_timeout  65;
  22.     #gzip  on;
  23.     server {
  24.         listen       80;
  25.         server_name  localhost;
  26.         #charset koi8-r;
  27.         #access_log  logs/host.access.log  main;
  28.         
  29.         # 鉴权服务地址
  30.         location = /auth {
  31.             internal;  # 仅允许内部请求
  32.             proxy_pass http://192.168.134.5:8080/auth;  # 鉴权服务地址
  33.             proxy_pass_request_body off;  # 不传递请求体
  34.             proxy_set_header Content-Length "";  # 清空 Content-Length
  35.             proxy_set_header X-Original-URI $request_uri;  # 传递原始请求路径
  36.             proxy_set_header x-token $token;  # 传递 Token 参数, 这里使用$arg_token拿不到值,所以只能用自定义变量了
  37.             proxy_set_header X-Original-Method $request_method;  # 传递请求方法
  38.         }
  39.         
  40.         # 视频点播路径
  41.         location /videos/ {
  42.         
  43.             # 将请求参数设置给指定变量
  44.             set $token $arg_token;
  45.         
  46.             alias /usr/local/nginx/vod/;
  47.             # 启用鉴权
  48.             auth_request /auth;  # 发送鉴权请求
  49.             auth_request_set $auth_status $upstream_status;  # 保存鉴权结果
  50.             # 如果鉴权失败,返回 401 或 403
  51.             error_page 401 = @error401;
  52.             error_page 403 = @error403;
  53.             
  54.             # 记录鉴权失败的请求,便于排查问题。
  55.             error_log /user/local/nginx/log/auth_error.log;
  56.         }
  57.         
  58.         # 自定义错误页面
  59.         location @error401 {
  60.             return 401 "Unauthorized";
  61.         }
  62.         location @error403 {
  63.             return 403 "Forbidden";
  64.         }
  65.         location / {
  66.             root   html;
  67.             index  index.html index.htm;
  68.         }
  69.         #error_page  404              /404.html;
  70.         # redirect server error pages to the static page /50x.html
  71.         #
  72.         error_page   500 502 503 504  /50x.html;
  73.         location = /50x.html {
  74.             root   html;
  75.         }
  76.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  77.         #
  78.         #location ~ \.php$ {
  79.         #    proxy_pass   http://127.0.0.1;
  80.         #}
  81.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  82.         #
  83.         #location ~ \.php$ {
  84.         #    root           html;
  85.         #    fastcgi_pass   127.0.0.1:9000;
  86.         #    fastcgi_index  index.php;
  87.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  88.         #    include        fastcgi_params;
  89.         #}
  90.         # deny access to .htaccess files, if Apache's document root
  91.         # concurs with nginx's one
  92.         #
  93.         #location ~ /\.ht {
  94.         #    deny  all;
  95.         #}
  96.     }
  97.     # another virtual host using mix of IP-, name-, and port-based configuration
  98.     #
  99.     #server {
  100.     #    listen       8000;
  101.     #    listen       somename:8080;
  102.     #    server_name  somename  alias  another.alias;
  103.     #    location / {
  104.     #        root   html;
  105.     #        index  index.html index.htm;
  106.     #    }
  107.     #}
  108.     # HTTPS server
  109.     #
  110.     #server {
  111.     #    listen       443 ssl;
  112.     #    server_name  localhost;
  113.     #    ssl_certificate      cert.pem;
  114.     #    ssl_certificate_key  cert.key;
  115.     #    ssl_session_cache    shared:SSL:1m;
  116.     #    ssl_session_timeout  5m;
  117.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  118.     #    ssl_prefer_server_ciphers  on;
  119.     #    location / {
  120.     #        root   html;
  121.     #        index  index.html index.htm;
  122.     #    }
  123.     #}
  124. }
复制代码
3、后端代码

  1. @Slf4j
  2. @RestController
  3. public class VodAuthController {
  4.     @Autowired
  5.     private HttpServletResponse response;
  6.     @Autowired
  7.     private HttpServletRequest request;
  8.     @RequestMapping("/auth")
  9.     public void auth() {
  10.         System.out.println("auth...");
  11.         Enumeration<String> headerNames = request.getHeaderNames();
  12.         while (headerNames.hasMoreElements()) {
  13.             String headerName = headerNames.nextElement();
  14.             System.out.println(headerName + " - " + request.getHeader(headerName));
  15.         }
  16.         String token= request.getHeader("x-token");
  17.         if ("123".equals(token)) {
  18.             log.info("鉴权通过!!!");
  19.             response.setStatus(200);
  20.         } else {
  21.             log.info("鉴权失败!!!");
  22.             response.setStatus(403);
  23.         }
  24.     }
  25. }
复制代码
4、dplayer播放mp4视频html代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7.     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.css">
  8.     <script src="https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.js"></script>
  9. </head>
  10. <body>
  11.     <div id="dplayer" style="width: 800px;height: 500px;"></div>
  12.    
  13. </body>
  14. <script>
  15.     const dp = new DPlayer({
  16.         container: document.getElementById('dplayer'),
  17.         video: {
  18.             url: 'http://192.168.134.3/videos/netty03.mp4?token=123'
  19.         },
  20.     });
  21. </script>
  22. </html>
复制代码
5、测试


此时后台输出
  1. ...
  2. auth...
  3. x-original-uri - /videos/netty03.mp4?token=123
  4. x-token - 123
  5. x-original-method - GET
  6. host - 192.168.134.5:8080
  7. connection - close
  8. pragma - no-cache
  9. cache-control - no-cache
  10. 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
  11. accept-encoding - identity;q=1, *;q=0
  12. accept - */*
  13. referer - http://127.0.0.1:5500/
  14. accept-language - en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7
  15. range - bytes=165478400-
  16. INFO 320 --- [io-8080-exec-10] com.zzhua.demo5.VodAuthController        : 鉴权通过!!!
  17. ...
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

用户云卷云舒

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表