简体   繁体   English

go - 如何在 Gin 中使用 JWT 提供基于路由授权的 static 文件?

[英]go - How can you serve static files with route based authorization with JWT in Gin?

I am trying to serve videos from a directory.我正在尝试从目录提供视频。 I know how to do that ( r.Static("/videos", "./videos") ) but I have a directory for each user in there like /videos/testuser/video.mp4 .我知道该怎么做 ( r.Static("/videos", "./videos") ),但我为每个用户都有一个目录,比如/videos/testuser/video.mp4 I have implemented a login system with JWT, but how would I got about only letting testuser access /videos/testuser and user1 access /videos/user1 ?我已经用 JWT 实现了一个登录系统,但是我怎么才能只让testuser访问/videos/testuseruser1访问/videos/user1呢?

Thanks!谢谢!

Store the UserID in JWT and parse the JWT claims whenever someone downloads the video.将 UserID 存储在 JWT 中,并在有人下载视频时解析 JWT 声明。 Use fmt.Sprintf("videos/%s/video.mp4", jwt.UserId) to get the desired directory depends on different user.使用fmt.Sprintf("videos/%s/video.mp4", jwt.UserId)获取所需的目录取决于不同的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM