你可以使用Gin框架来实现基于路由的JWT授权的静态文件服务。

huangapple go评论80阅读模式
英文:

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

问题

我正在尝试从一个目录中提供视频。我知道如何做到这一点(r.Static("/videos", "./videos")),但是我在那里有一个为每个用户创建的目录,例如/videos/testuser/video.mp4。我已经实现了一个带有JWT的登录系统,但是我该如何让testuser只能访问/videos/testuser,而user1只能访问/videos/user1

谢谢!

英文:

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. 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?

Thanks!

答案1

得分: 2

将UserID存储在JWT中,并在某人下载视频时解析JWT声明。使用fmt.Sprintf("videos/%s/video.mp4", jwt.UserId)根据不同的用户获取所需的目录。

英文:

Store the UserID in JWT and parse the JWT claims whenever someone downloads the video. Use fmt.Sprintf("videos/%s/video.mp4", jwt.UserId) to get the desired directory depends on different user.

huangapple
  • 本文由 发表于 2022年11月27日 17:12:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/74588785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定