Angular 5 : Is there way hide API call? 还是将其设为私有(private)?

标签 angular

我开始学习 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/

相关文章:

java - 同一台服务器上的 Spring Boot + Angular2

javascript - TestCafe错误: "No tests to run. Either the test files contain no tests or the filter function is too restrictive"

html - 根据是否扩展动态更改 mat-expansion-panel-header 的背景颜色

angular - PrimeNG p-dataTable 响应式断点

angular - 如何在ngrx Angular2中将数据从父路由传递到子路由

Angular 6 - 路由到惰性模块的子模块不起作用

angular - Angular2- Material 2 : Want to setup a fixed sidenav below toolbar

asp.net-mvc - Angular 2 - 使用 ASP.NET MVC 进行路由

javascript - 相对路径中的Angular 2 img src

angular - 从子组件触发 Angular NG IF