英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论