Specify Multiple Subdomains with Access Control Origin

后端 未结 7 862
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-13 17:57

I am trying to allow access to every subdomain on my site in order to allow cross subdomain AJAX calls. Is there a way to specify all subdomains of a site like *.example.c

7条回答
  •  深忆病人
    2021-02-13 18:39

    This was my challenge and solution:

    1 - Backend PHP on api.example.com.

    2 - Multiple JS front ends such as one.example.com, two.example.com etc.

    3 - Cookies needed to be passed both ways.

    4 - AJAX call from multiple front-ends to PHP backend on api.example.com

    5 - In PHP, I do not prefer to use $_SERVER["HTTP_ORIGIN"], not always reliable/safe in my opinion (I had some browsers where HTTP-ORIGIN was always empty).

    The normal way to do this in PHP with single front end domain is starting PHP code with:

    header('Access-Control-Allow-Origin: https://one.example.com');  
    header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');  
    header('Access-Control-Allow-Credentials: true');  
    

    And in JS on one.example.com domain:

    jQuery.ajax({
        url: myURL,
        type: "POST",
        xhrFields: {withCredentials: true},
        dataType: "text",
        contentType: "text/xml; charset=\"utf-8\"",
        cache: false,
        headers: "",
        data: myCallJSONStr,
        success: function(myResponse) {.....}
    

    However, this is not workable as I am using multiple subdomains to call my API domain.

    And this solution will NOT work as I want to pass on cookies:

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

    It conflicts with the pass on cookie setting on the JS site:

    xhrFields: {withCredentials: true}
    

    Here is what I did:

    1 - use GET parameter to pass the Subdomain. 2 - Hardcode the Main domain in PHP so only (all) Subdomains are allowed.

    This is the JS/JQuery AJAX part of my solution:

    function getSubDomain(){
        
        let mySubDomain = "";
        
        let myDomain = window.location.host;
        let myArrayParts = myDomain.split(".");
        if (myArrayParts.length == 3){
            mySubDomain = myArrayParts[0];
        }
        
        return mySubDomain;
        
    }
    

    And in the AJAX call:

        let mySubDomain = getSubDomain();
        if (mySubDomain != ""){
            myURL += "?source=" + mySubDomain + "&end"; //use & instead of ? if URL already has GET parameters
        }
        
        jQuery.ajax({
            url: myURL,
            type: "POST",
            xhrFields: {withCredentials: true},
            dataType: "text",
            contentType: "text/xml; charset=\"utf-8\"",
            cache: false,
            headers: "",
            data: myCallJSONStr,
            success: function(myResponse) {.....}
    

    Finally, the PHP part:

    IMPORTANT, don't forget to set the cookies for all subdomains, in this case the domain for the cookie would be: .example.com (so with a dot in front of the main domain):

    This solution allows me to call the API on api.example.com from any subdomains on example.com.

    NB. for situation where there is only a single calling subdomain, I prefer using .htaccess for setting CORS instead of PHP. Here is an example of .htaccess (linux/apache) for only one.example.com calling api.example.com:

    
        Header set Access-Control-Allow-Origin "https://one.example.com"
        Header set Access-Control-Allow-Headers "Origin, Content-Type, X-Auth-Token"
        Header set Access-Control-Allow-Credentials "true"
    
    

    And place this .htaccess in the root of api.example.com.

提交回复
热议问题