【问题标题】:What is the recommended CORS configuration of hosting Javascript on S3/CF?在 S3/CF 上托管 Javascript 的推荐 CORS 配置是什么?
【发布时间】:2017-05-30 14:03:26
【问题描述】:

我已经看到了类似问题的答案,但我想知道在 2017 年,如果我想限制对 *.domain.tld 的合法访问,那么为 S3/CF 配置 CORS 的最佳方式是什么。 Javascript 正在从 CF 加载,并使用 Ajax 请求向 api.domain.tld 呈现 Web 应用程序。

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

我还可以添加什么来改进 CORS 设置吗?

【问题讨论】:

    标签: amazon-s3 cors amazon-cloudfront


    【解决方案1】:

    以下是进行 CORS 配置的一般规则:

    1)A valid CORS configuration consists of 0 to 100 CORS rules.
    2)Each rule must include at least one origin.
    3)An origin may contain at most one wildcard *
    4)Each rule must include at least one method.
    5)The supported methods are: GET, HEAD, PUT, POST, DELETE.
    6)Each rule may contain an identifying string of up to 255 characters.
    7)Each rule may specify zero or more allowed request headers (which the client may include in the request).
    8)Each rule may specify zero or more exposed response headers (which are sent back from the server to the client).
    9)Each rule may specify a cache validity time of zero or more seconds. If not included, the client should supply their own default.
    

    最近我使用了一个 JS/CF 项目,这是我的 CORS 配置。

    <CORSConfiguration>
    <CORSRule>
        <ID>example.com: Allow PUT & POST with AWS S3 JS
        SDK</ID>
        <AllowedOrigin>https://www.example.com</AllowedOrigin>
        <AllowedOrigin>http://www.example.com</AllowedOrigin>
        <AllowedOrigin>https://example.com</AllowedOrigin>
        <AllowedOrigin>http://example.com</AllowedOrigin>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedHeader>Origin</AllowedHeader>
        <AllowedHeader>Content-Length</AllowedHeader>
        <AllowedHeader>Content-Type</AllowedHeader>
        <AllowedHeader>Content-MD5</AllowedHeader>
        <AllowedHeader>X-Amz-User-Agent</AllowedHeader>
        <AllowedHeader>X-Amz-Date</AllowedHeader>
        <AllowedHeader>Authorization</AllowedHeader>
        <ExposeHeader>ETag</ExposeHeader>
        <MaxAgeSeconds>1800</MaxAgeSeconds>
    </CORSRule>
    <CORSRule>
        <ID>example.com: Allow GET with AWS S3 JS SDK</ID>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
        <ExposeHeader>ETag</ExposeHeader>
        <MaxAgeSeconds>1800</MaxAgeSeconds>
    </CORSRule>
    </CORSConfiguration>
    

    更多详情您可以找到here

    谢谢

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 1970-01-01
      • 2019-05-16
      • 2011-06-25
      • 2012-01-08
      • 2013-06-20
      • 1970-01-01
      • 2011-06-09
      • 2021-03-28
      相关资源
      最近更新 更多