在 img 标签中显示从 API 到 Next.js 客户端的受保护图像
我正在尝试显示 Node.js 后端资产文件夹中受保护的图像。
它有一个中间件来检查用户是否登录并有权访问该文件。
在客户端,我想在 img 标签中显示图像:
有没有办法拦截此请求以传递用户的令牌,以便他能够访问图像?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下方式之一实现此目的:
使用令牌作为图像URL cookie
当登录时,您可以将cookie发送到浏览器并使用中间件来验证,如果用户有验证允许查看图像。
令牌
同样,您可以使用像this
“ localhost:5000/uploads/image.png?token = xxxxxxxxxxxxxxxxxx”
< img src = 用作用作示例。您必须在任何库中实现此中间件。
You can implement this in one of these ways:
Cookies
When login you can send cookies to the browser and use middleware to validate if the user has permission to view the image.
Token
similarly you can use a token like this
<img src="localhost:5000/uploads/image.png?token=xxxxxxxxxxxxxx" />
Note: This code uses express as an example. You'll have to implement this middleware in whatever Library you are usng.
使用blob可以将从后端API下载的图像延迟绑定到<图像> 来自 API 的标签
响应被传递到下面的函数
using blob you can delay bind the image downloaded from backend API to < img > tag
response from API is passed to below function