加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 当Azure上托管后端时,create-react-app代理请求失败,

发布时间:2020-12-15 05:08:14 所属栏目:百科 来源:网络整理
导读:我在设置我的create-react-app应用程序以将请求代理到Microsoft azure上的测试托管时遇到了一些麻烦.我在我的应用程序的package.json中设置了代理,如下所示: "proxy":{ "/api/*":{ "target":"https://mytestbackend.azurewebsites.net","secure":false}} 我
我在设置我的create-react-app应用程序以将请求代理到Microsoft azure上的测试托管时遇到了一些麻烦.我在我的应用程序的package.json中设置了代理,如下所示:
"proxy":{
   "/api/*":{
   "target":"https://mytestbackend.azurewebsites.net","secure":false
}
}

我已经设置了一个axios请求,要求发送到azure上的后端服务器.它是一个独立的.js,我从我的一个反应应用程序的事件中调用它.它看起来像这样:

import axios from 'axios';

const login = async (username,password) => {
   console.log("Username to send is:"+username);
   console.log("password to send is:"+password);
   let response = await axios.post('/api/user/login',{username:username,password:password});
   console.log(response);
};

export {login};

问题不在我的反应组件中,因为这两个console.log()调用显示正在接收输入的值.如果我从package.json中删除“secure”:false设置,则请求失败并显示Http Error:500.但如果我使用安全设置,则会失败并显示404页面.有人可以说清楚我做错了什么吗?我可以只在“localhost”上使用代理吗?文档另有说明.任何帮助是极大的赞赏.

我已经验证在Azure管理门户上运行dev服务器的域上启用了CORS.如果我直接使用后端的URL(即不使用create-react-app代理)来执行请求,它就可以工作.问题必须是代理配置的方式.

不使用安全时发生的HTTP Errpr 500的响应文本是:

Proxy error: Could not proxy request /api/user/login from localhost:3000 to https://mytestbackend.azurewebsites.net (undefined).

附加信息:我还通过在我的开发机器上本地运行我的后端进行了测试.出现错误消息,但括号中的“未定义”表示“UNABLE_TO_VERIFY_LEAF_SIGNATURE”.如果使用“secure:false,我可以成功调用登录端点,但是对需要身份验证的其他端点的调用失败,因为axios不发送cookie.

这样做:
卷曲-v https://mytestbackend.azurewebsites.net/api/user/login

有这个输出:

* SSLv3,TLS handshake,Client hello (1):
* SSLv3,Server hello (2):
* SSLv3,CERT (11):
* SSLv3,TLS alert,Server hello (2):
* SSL certificate problem: unable to get local issuer certificate
* Closing connection #0
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default,using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate,you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle,the certificate verification probably failed due to a
problem with the certificate (it might be expired,or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate,use
the -k (or --insecure) option.
create-react-app使用使用 https://github.com/chimurai/http-proxy-middleware#options的WebPackDevServer

所以你可以使用相同的所有选项

现在,在外部托管服务器的情况下导入的一个密钥头是host.如果不正确,这有时会出现问题,请参见下面的示例

Websocket works on EC2 url but not on ElasticBeanstalk URL

接下来是cookie可能与localhost相关联,我检查了它们应该没有任何修改.但您可能也想使用cookieDomainRewrite:“”选项

所以我将使用的最终配置如下

"proxy":{
   "/api/*":{
   "target":"https://mytestbackend.azurewebsites.net","secure":false,"headers": {
        "host": "mytestbackend.azurewebsites.net"
      },"cookieDomainRewrite": ""
  } 
}

同样在您的客户端上,您希望使用withCredentials:true

let userinfo =await axios.get('/api/secured/userinfo',{withCredentials:true});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读