简体   繁体   English

在 AWS EC2 中添加 CORS 标头的位置

[英]Where to add CORS headers in AWS EC2

I'm making a POST request from a browser to my EC2 AWS server but getting the following error back.我正在从浏览器向我的 EC2 AWS 服务器发出 POST 请求,但返回以下错误。

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.example.com' is therefore not allowed access. The response had HTTP status code 401.

I've done some research and release I need to enable CORS on my EC2 AWS server - I understand that I should be adding something along the lines of我做了一些研究并发布了我需要在我的 EC2 AWS 服务器上启用 CORS - 我知道我应该添加一些东西

<CORSConfiguration>
    <CORSRule>
    <AllowedOrigin>http://www.example1.com</AllowedOrigin>

    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>

   <AllowedHeader>*</AllowedHeader>
 </CORSRule>
 <CORSRule>
     <AllowedOrigin>http://www.example2.com</AllowedOrigin>

     <AllowedMethod>PUT</AllowedMethod>
     <AllowedMethod>POST</AllowedMethod>
     <AllowedMethod>DELETE</AllowedMethod>

     <AllowedHeader>*</AllowedHeader>
 </CORSRule>
 <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
 </CORSRule>
</CORSConfiguration>

from AWS docs: AWS EC2 CORS来自 AWS 文档: AWS EC2 CORS

However I'm unsure of where I actually need to add this too.但是我不确定我实际上也需要在哪里添加它。 My AWS server knowledge it limited.我对 AWS 服务器的了解有限。

It's a Neo4j db and the call is:这是一个 Neo4j 分贝,调用是:

getRequest = function () {
    $.ajax({
        url: 'http://exampleEC2:port/',
        type: 'POST',
        dataType: 'JSON',
        contentType: 'application/json;charset=UTF-8',
        password: 'xxxxxx',
        username: 'xxxx',
        data: dataObject,
        success: function (data) {
            console.log("success");
        },
        error: function(error) {
            console.log(error);
        }
    });
};

You can add the CORS condition to your EC2 instance on the server itself.您可以将 CORS 条件添加到服务器本身上的 EC2 实例。 Assuming that this is an Apache EC2 instance, you can edit your .htaccess file to allow any domain or one specific domain:假设这是一个 Apache EC2 实例,您可以编辑 .htaccess 文件以允许任何域或一个特定域:

Header set Access-Control-Allow-Origin "*"

or

Header set Access-Control-Allow-Origin "yourexternaldomain.tld"

If I remember correctly, CORS can only have one domain associated with it.如果我没记错的话,CORS 只能关联一个域。

I did find someone had come up with a conditional declaration in the .htaccess file so that a couple different and specific domains could be allowed access:我确实发现有人在 .htaccess 文件中提出了一个条件声明,以便可以允许访问几个不同的特定域:

Stackoverflow: Access-Control-Allow-Origin Multiple Origin Domains? Stackoverflow:访问控制允许源多个源域?

I spent hours wrestling with this.我花了几个小时来解决这个问题。 Tried .htaccess.试过.htaccess。 Tried .conf.试过.conf。 Fooling around with firewalls.玩弄防火墙。

In the end it was a single line of php:最后是一行 php:

header('Access-Control-Allow-Origin: *');

I had the same error and finally solved it by adding:我有同样的错误,最后通过添加解决了它:

@CrossOrigin(origins = {"https://example.com"}) to the controller of in my Spring application. @CrossOrigin(origins = {"https://example.com"}) 到我的 Spring 应用程序中的 controller。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM