与Amazon S3和Cloudfront的CORS

我在Heroku上托管了一个Rails应用程序,它使用CloudFront和S3上托管的资产。

它完美地显示资产(虽然它需要一些摔跤)。

我对Cloudfront的设置:

Forward Headers: Whitelist Whitelist Headers: Origin Forward Query Strings: No 

S3存储桶的CORS设置:

    * GET HEAD *   

调用图像的JS(咖啡)

 @mousemove (e) -> ctx = $("").attr( width: @width height: @height )[0].getContext("2d") ctx.drawImage(this, 0, 0, @width, @height) # This is because firefox doesn't support offset[X|Y] if(e.offsetX == undefined) xpos = e.pageX - $(this).offset().left; ypos = e.pageY - $(this).offset().top; else xpos = e.offsetX; ypos = e.offsetY; e.rgb = ctx.getImageData(xpos, ypos, 1, 1).data return f.call this, e 

此错误包括: Uncaught SecurityError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.

curl请求:

curl -i -H "Origin: http://example.com/" http://sofzh.miximages.com/ruby-on-rails/Screen_Shot_2014-08-06_at_15.21.14.png?1407334881

  Response: HTTP/1.1 200 OK Content-Type: image/png Content-Length: 6888 Connection: keep-alive Date: Wed, 06 Aug 2014 16:15:54 GMT Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, HEAD Last-Modified: Wed, 06 Aug 2014 14:21:23 GMT ETag: "6f266467cf0a570526869bcf280da412" Accept-Ranges: bytes Server: AmazonS3 Age: 21 Vary: Origin X-Cache: Hit from cloudfront Via: 1.1 16d4ae36524b457e558b982004526450.cloudfront.net (CloudFront) X-Amz-Cf-Id: 5Vm_eCn_lTOHEMzPEcmtCGYCUdOZ2r_9R4W6mEPTOPFUIJe0ilMP7g== *snip* 

它不是一个缓存问题我每次都使用新的图像,虽然这个请求是在缓存版本上。

我知道有工作,但我真的希望它以这种方式工作。

我在这里想念的是什么?

问题是,默认情况下,浏览器不会检查这些cors标头 – 所有插入的图像都是脏的。 为了让您的cors标题可以执行任何操作,您需要在img上设置crossorigin属性,以便获得启用cors的图像 。

  

crossorigin属性可以采用anonymoususe-credentials的值。 这与进行跨域ajax请求时的withCredentials选项类似:默认情况下,不会发送cookie,http auth等。 如果您在javascript中创建图像,则相应的属性为crossOrigin

根据MDN文档 Chrome,Firefox和IE 11支持这一点,但Opera或Safari的发布版本没有