我开始学习 Angular,我注意到我对后端所做的每一次调用都可以从开发者工具中看到。所以当我得到这样的方法/函数时:
getUser(userId){
return this.http.post('server/page/get-user', {id:userId});
}
然后在某些组件中我会这样调用它:
this.userService.getUser(2).subscribe((data)=> {
console.log(data)
})
基本上是根据发布的用户 ID 返回用户信息(姓名、地址等)。如果想获取随机用户信息,难道他们不能只对该端点进行 API 调用,在请求有效负载中使用随机数并获取该用户信息吗?
我读到一些东西,解决这个问题的一种方法是使用 JWT,它基本上加密有效负载,但是没有选项可以让这个 api 调用只在我的应用程序中可用吗?或至少使其对开发人员工具隐藏?
最佳答案
Is there way hide API call? or make it private?
没有。浏览器属于用户。它的作用在他们的控制之下,而不是您的控制之下。
What basically returns user information (name, address etc), based on what user id gets posted. If one wanted to get random user information, couldn't they just make API call to this endpoint, with random number in request payload and just get that user information?
如果您正在运行未经身份验证的 API。是的。
这听起来像你想要的security through obscurity这是非常不可靠的。
I read something that one way to fix this is to use JWT, what basically encrypts the payloads
不是真的。
您需要身份验证/授权。
您需要识别用户(这可以通过用户名和密码、OAuth 与 Facebook 或 Twitter 等提供程序等)。
然后您需要确保允许用户读取他们请求的数据。例如用户记录只能由拥有该记录的用户或具有管理员 Angular 色的用户访问。
isn't there option to like turn this api call usable only in my app?
没有
or make it at least hidden from developers tools?
没有
关于Angular 5 : Is there way hide API call? 还是将其设为私有(private)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53630002/