gpt4 book ai didi

tomcat - 发布请求的 Elastic Beanstalk CORS 配置

转载 作者:行者123 更新时间:2023-11-28 22:11:15 28 4
gpt4 key购买 nike

我从我的本地主机提交发布请求时收到以下错误 -

XMLHttpRequest cannot load http://sample-env.elasticbeanstalk.com/resource 
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8080' is therefore not allowed access

但是,在我的 S3 实例的 CORS 配置编辑器中,我有以下内容 -

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedOrigin>http://*</AllowedOrigin>
<AllowedOrigin>https://*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>

我试过这三行代码,但仍然无法诊断出问题所在。

    <AllowedOrigin>*</AllowedOrigin>
<AllowedOrigin>http://*</AllowedOrigin>
<AllowedOrigin>https://*</AllowedOrigin>

我的开发者控制台显示了以下 header ,这让我觉得这个 CORS 配置甚至没有被读取 -

HTTP/1.1 200 OK
Date: Fri, 21 Apr 2017 02:48:01 GMT
Server: Apache-Coyote/1.1
Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS
Content-Length: 0
Keep-Alive: timeout=60, max=98
Connection: Keep-Alive
Content-Type: text/plain

有没有人发现我的配置有问题或者是否有其他必须添加的东西?我能够将 CORS 过滤器添加到我的 tomcat 的 web.xml 中,并且一切正常,所以我认为我没有正确配置我的弹性 beanstalk。

编辑

添加了以下 .config 文件(以前是 .yaml)

add_cors.config.config

files:
"/etc/httpd/conf.d/cors.conf" :
mode: "000644"
owner: root
group: root
content: |
<Location "/">
Header set Access-Control-Allow-Origin: *
Header set Access-Control-Allow-Methods: "POST, GET, PUT, DELETE, OPTIONS"
Header add Access-Control-Allow-Headers: "Authorization, Content-Type, Accept"
Header set Access-Control-Allow-Credentials: true

SetOutputFilter DEFLATE
</Location>

它就在爆炸 war 中的这个位置 -

.
├── app.war
├── .ebextensions
│   ├── add_cors.config.yaml
├── META-INF
│   ├── …
└── WEB-INF
├── classes
│   └── …
├── servlet.xml
├── lib
│   ├── …
└── web.xml

我重新部署了 war ,但我仍然有同样的错误 - “没有'Access-Control-Allow-Origin' header 存在”

解决方案

不需要 .ebextensions 文件。我只需要像在 tomcat 的 conf/web.xml 中一样将 CORS 过滤器添加到 web.xml

  <!-- ================== CORS FILTERS ===================== -->
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

最佳答案

我不太了解 AWS EB,但假设在 tomcat 前面有一个 apache 反向代理,看起来你可以使用 ebextensions 并在那里添加 CORS header 信息。看这个:

https://forums.aws.amazon.com/message.jspa?messageID=570570

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html

关于tomcat - 发布请求的 Elastic Beanstalk CORS 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43533280/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com