【问题标题】:How to access backend from frontend on another localhost port?如何从另一个 localhost 端口上的前端访问后端?
【发布时间】:2018-07-24 22:13:01
【问题描述】:

我们正在制作一个 Web 应用程序,我们将前端和后端拆分为单独的项目。使用 CAS 设置安全性后,我们无法从前端访问后端,得到401 Unauthorized。前端运行在localhost:3000,而后端运行在localhost:8080

我们想允许端口3000访问端口8080,但是不知道怎么做,有什么线索吗?

【问题讨论】:

  • 您有任何 CORS 注册表配置吗?如果有,请提供代码/配置
  • 也许this 可以提供帮助。

标签: java spring spring-mvc localhost port


【解决方案1】:

如果您在前端和后端的两个独立项目上运行,则需要编写 RESTAPI。要访问您需要设置身份验证的后端实例,它可能是 CSRF 或 CORS。 请查看 spring boot 中的 cors 文档 https://spring.io/guides/gs/rest-service-cors/

这是为 csrf https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html

【讨论】:

    【解决方案2】:

    如果您尝试与两个不同的项目进行通信,则需要编写 RESTAPI,因为您使用不同的端口号,出于安全目的,您需要启用跨源。参考这个https://www.concretepage.com/spring-4/spring-4-rest-cors-integration-using-crossorigin-annotation-xml-filter-example

    【讨论】:

      【解决方案3】:
      Front end : localhost:3000/yourfilename.html
      
      **Front end** default port of xamp port is 80. So We re basically use 
      localhost/yourfilename.html. But Here used 3000 port. so We should call port 
      number with that url 
      
      **Back end** : localhost:8080/apiurl
      It is ordinary port of backend. 
      

      【讨论】:

      • 请为您的代码添加一些解释,以利于其他读者!
      猜你喜欢
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 2021-05-22
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多