Access-Control-Allow-Origin Multiple Origin Domains?

前端 未结 30 2096
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 07:08

Is there a way to allow multiple cross-domains using the Access-Control-Allow-Origin header?

I\'m aware of the *, but it is too open. I rea

相关标签:
30条回答
  • 2020-11-21 07:32

    Sounds like the recommended way to do it is to have your server read the Origin header from the client, compare that to the list of domains you would like to allow, and if it matches, echo the value of the Origin header back to the client as the Access-Control-Allow-Origin header in the response.

    With .htaccess you can do it like this:

    # ----------------------------------------------------------------------
    # Allow loading of external fonts
    # ----------------------------------------------------------------------
    <FilesMatch "\.(ttf|otf|eot|woff|woff2)$">
        <IfModule mod_headers.c>
            SetEnvIf Origin "http(s)?://(www\.)?(google.com|staging.google.com|development.google.com|otherdomain.example|dev02.otherdomain.example)$" AccessControlAllowOrigin=$0
            Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
            Header merge Vary Origin
        </IfModule>
    </FilesMatch>
    
    0 讨论(0)
  • 2020-11-21 07:34

    HTTP_ORIGIN is not used by all browsers. How secure is HTTP_ORIGIN? For me it comes up empty in FF.
    I have the sites that I allow access to my site send over a site ID, I then check my DB for the record with that id and get the SITE_URL column value (www.yoursite.com).

    header('Access-Control-Allow-Origin: http://'.$row['SITE_URL']);
    

    Even if the send over a valid site ID the request needs to be from the domain listed in my DB associated with that site ID.

    0 讨论(0)
  • 2020-11-21 07:35

    Here is what i did for a PHP application which is being requested by AJAX

    $request_headers        = apache_request_headers();
    $http_origin            = $request_headers['Origin'];
    $allowed_http_origins   = array(
                                "http://myDumbDomain.example"   ,
                                "http://anotherDumbDomain.example"  ,
                                "http://localhost"  ,
                              );
    if (in_array($http_origin, $allowed_http_origins)){  
        @header("Access-Control-Allow-Origin: " . $http_origin);
    }
    

    If the requesting origin is allowed by my server, return the $http_origin itself as value of the Access-Control-Allow-Origin header instead of returning a * wildcard.

    0 讨论(0)
  • 2020-11-21 07:36

    Below answer is specific to C#, but the concept should be applicable to all the different platforms.

    To allow Cross Origin Requests from a web api, You need to allow Option requests to your Application and Add below annotation at controller level.

    [EnableCors(UrlString,Header, Method)] Now the origins can be passed only a s string. SO if you want to pass more than one URL in the request pass it as a comma seperated value.

    UrlString = "https://a.hello.com,https://b.hello.com"

    0 讨论(0)
  • 2020-11-21 07:37

    As mentioned above, Access-Control-Allow-Origin should be unique and Vary should be set to Origin if you are behind a CDN (Content Delivery Network).

    Relevant part of my Nginx configuration:

    if ($http_origin ~* (https?://.*\.mydomain.example(:[0-9]+)?)) {
      set $cors "true";
    }
    if ($cors = "true") {
      add_header 'Access-Control-Allow-Origin' "$http_origin";
      add_header 'X-Frame-Options' "ALLOW FROM $http_origin";
      add_header 'Access-Control-Allow-Credentials' 'true';
      add_header 'Vary' 'Origin';
    }
    
    0 讨论(0)
  • 2020-11-21 07:38

    For ExpressJS applications you can use:

    app.use((req, res, next) => {
        const corsWhitelist = [
            'https://domain1.example',
            'https://domain2.example',
            'https://domain3.example'
        ];
        if (corsWhitelist.indexOf(req.headers.origin) !== -1) {
            res.header('Access-Control-Allow-Origin', req.headers.origin);
            res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
        }
    
        next();
    });
    
    0 讨论(0)
提交回复
热议问题