如何限制express api仅允许来自您的客户端的post请求?
我正在开发我的第一个全栈应用程序,特别是 MERN 堆栈,但遇到了一些问题。我正在尝试在我的网站上为统一游戏实现存储在数据库中的排行榜。我已经一切正常,客户可以使用我的 Express api 从我的 MongoDB Atlas 数据库发布和获取分数。但是,在排行榜的情况下,我需要确保分数只能由客户端根据游戏的进展情况发送。在当前的工作配置下,任何人都可以通过 API 发送欺骗分数,而无需玩游戏。
我首先想到的是尝试实现 JWT 来验证 api 调用是否来自站点,但在我看来,用户仍然可以复制像 JWT 这样的任何身份验证令牌,并使用 Postman 轻松发送欺骗分数。
我对数据库不太熟悉,并且怀疑如果我不使用像 Atlas 这样的 DBaaS 提供商,这个问题可能会得到解决,但我并不完全确定。
任何想法或建议将不胜感激!
I am working on my first full stack application, specifically with the MERN stack, and have run into a bit of an issue. I am trying to implement a leaderboard stored in a db for a unity game on my website. I have everything working wherein the client can post and get scores from my MongoDB Atlas database using my Express api. However, in the case of a leaderboard, I need to insure that the scores can ONLY be sent by the client based on how the game goes. With the current working configuration, anyone can send spoof scores via the api without having to play the game.
I first thought was to try to implement JWT to authenticate that the api call was coming from the site, but in my head any auth token like JWT could still be copied down by a user and sent with spoofed scores easily with Postman.
I am not extensively familiar with databases and suspect that this could possibly be solved if I wasn't using a DBaaS provider like Atlas but I am not entire sure.
Any thoughts or recommendations would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以定义一个中间件函数并检查传入请求的方法:
然后将其应用于您想要保护的路由:
You could define a middleware function and check the method of the incoming request:
And then apply it to the routes you want to protect:
对当前答案功能的改进可能是:
所以你可以这样使用它:
An improvement to current answer's function could be:
So you could use it like this: