Angular 和请求的 IP 地址

huangapple go评论71阅读模式
英文:

Angular and request IPs

问题

好的,以下是翻译好的部分:

晚安。

我目前正在开发一个应用,前端使用 Angular15,后端使用 ASP.NET API。它们都部署在 IIS v8.5 服务器上。

最近,我发现当我的同事们进入应用程序进行一些测试时,发送到后端的 REST 请求是使用我的同事们的 IP 地址而不是前端部署的服务器 IP 地址进行的。

这导致了一些问题,所以我想知道是否有办法让 Angular 前端使用服务器 IP 地址来执行所有请求,而不是使用客户端 IP 地址。

谢谢!

#更新1
这是我进行REST调用的方式:

在 .ts 文件中:

this.persistenceService.save(this.info).subscribe(
      data => {
        console.log(data);
      });

在 persistenceService 中:

save(info: CustomObject): Observable<any> {
return this.http.post('https://backend.com', info, {headers: {'Content-Type': 'application/json'}});
}
英文:

Good night.

I'm currently developing an app with Angular15 as frontend and an ASP.NET API as backend. Both are deployed in IIS v8.5 servers.

Recently, I have found that when my companions enter the app to do some tests, the REST requests that are sent to the backend are being done with my companions IP instead of doing it with the server IP where the frontend is deployed.

This is causing some troubles so I want to know if there is a way to make the Angular frontend do all the requests with the server IP instead of doing it with the client IP.

Thank you!

#Update1
This is the way I'm doing the REST calls:

In .ts:

this.persistenceService.save(this.info).subscribe(
      data =&gt; {
        console.log(data);
      });

In persistenceService:

save(info: CustomObject): Observable&lt;any&gt; {
return this.http.post(&#39;https://backend.com&#39;, info, {headers: {&#39;Content-Type&#39;: &#39;application/json&#39;}});
}

答案1

得分: 0

当您访问一个网络应用程序时,发生的情况是该网络应用程序会从部署它的服务器上物理下载到您的计算机上,然后该应用程序在您的浏览器中运行,并且应用程序发出的所有请求都通过浏览器进行,因此从您的计算机经过,使用您的IP地址进行。这在互联网通信中始终如此。如果您希望您的应用程序只能从特定网络访问,那么您需要设置一个VPN,您的同伴可以加入,然后他们将从网络的地址空间中获取他们的IP地址。

英文:

When you visit a web application whats happens is that web app is physically downloaded onto your computer from the server where it is deployed and the application runs in your browser and all the requests that the app makes go through the browser thus are made from your computer thought your IP address. This is always the case with the Internet communication. If you want your app to be accessible only from a certain network then what you have to do is to setup a a VPN that your companions can join and then they will get their IP addresses from the address space of the network.

答案2

得分: 0

我最终通过反向代理解决了这个问题,正如 @TengFeiXie 所说。

英文:

I could finally solve it with a reverse proxy, as @TengFeiXie said.

huangapple
  • 本文由 发表于 2023年7月11日 05:10:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定