Allow localhost in CORS headers | Community
Skip to main content
iamnjain
New Participant
Sep 11, 2023 9:34 AM UTC
Solved

Allow localhost in CORS headers

Hello,

 

We have a Proxy pass setup to downstream systems in AEM Dev Environment which is development and test enviroment. While frontend team development, they are hitting AEM Dev server on localhost and facing CORS error.

So, what would be that right approach for this while developing on localhost?

 

iamnjain
iamnjainAccepted solution
New Participant
Sep 13, 2023 4:05 AM UTC

So I used below expression in Virtual host file

 

Header set Access-Control-Allow-Origin "*"

and did a dispatcher deployment. So It's started working as it's allow everything.

 

@arunpatidarI suggested to use CORS blocker plugin to FED team, but after internal discussion, we thought of allowing all on Dev env since it's for development purpose only.

 

Thanks @estebanbustamante as well for your suggestions.

9 replies

iamnjain
iamnjainAuthorAccepted solution
New Participant
Sep 13, 2023 4:05 AM UTC

So I used below expression in Virtual host file

 

Header set Access-Control-Allow-Origin "*"

and did a dispatcher deployment. So It's started working as it's allow everything.

 

@arunpatidarI suggested to use CORS blocker plugin to FED team, but after internal discussion, we thought of allowing all on Dev env since it's for development purpose only.

 

Thanks @estebanbustamante as well for your suggestions.

Translate
arunpatidar
New Participant
Sep 13, 2023 7:23 AM UTC
Header set Access-Control-Allow-Origin "*"

 

This will allow all the domains(external) to use/consume AEM content. You should be not using * , instead of use whitelisted domain here.

 

If it is just temporary then you can use *.

Translate
Arun Patidar
iamnjain
iamnjainAuthor
New Participant
Sep 13, 2023 8:23 AM UTC

How to use whitelisted domain? If you can help with one example syntax, please.

Translate
arunpatidar
New Participant
Sep 12, 2023 3:08 PM UTC

Hi @iamnjain 
If you are using google chrome browser then you can override CORS header locally in the browser

https://developer.chrome.com/docs/devtools/overrides/ 

Translate
Arun Patidar
kautuk_sahni
Employee
Sep 12, 2023 12:20 PM UTC

@iamnjain Do you find the suggestions from Esteban useful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. 

Translate
Kautuk Sahni
EstebanBustamante
New Participant
Sep 11, 2023 1:25 PM UTC

Hi 

 

You need to configure the OSGI Service "Adobe Granite Cross-Origin Resource Sharing Policy", here is the official documentation explaining how to set it up: https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/security/understand-cross-origin-resource-sharing.html?lang=en. And here is an article that shows how to enable CORS for external domains: https://www.albinsblog.com/2021/10/how-to-deliver-headless-content-through-graphql-api-and-content-fragments-in-aem.html#.Y43LD3ZBw2z:~:text=Consume%20GraphQL%20API%20Data%20from%20External%20System%3A 

 

A couple of things you need to keep in mind are:

- Usually, the CORS performs a preflight call, hence, you may need to add the OPTIONS method as part of the above configuration

- Check which CORS header your application is expecting to validate as a valid resource as this can vary

- If you are trying to do a POST operation on an author instance, you will also have CRSF issues, if so, read this https://www.albinsblog.com/2023/03/what-is-csrf-how-is-csrf-protection-enabled-in-aem.html to fix that as well. 

 

Hope this helps.

 

Translate
Esteban Bustamante
iamnjain
iamnjainAuthor
New Participant
Sep 11, 2023 1:35 PM UTC

Hi @estebanbustamante 

 

But my request is not hitting to AEM Instance, it's being proxy passed from Dispatcher server. How to allow localhost on Dispatcher itself?

 

Translate
EstebanBustamante
New Participant
Sep 11, 2023 2:29 PM UTC

Sorry for that @iamnjain, if you need to add the headers in the dispatcher just use something like this:

Header always set Access-Control-Allow-Origin "*"

Please check the section "Enabling the header through Apache" from this article: https://www.albinsblog.com/2018/08/how-to-enable-cross-origin-resource-sharing-support-in-aem.html 

Also, consider that these headers must be allowed in the dispatcher's /clientheaders config: 

https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/security/understand-cross-origin-resource-sharing.html?lang=en#allowing-cors-request-headers 

Translate
Esteban Bustamante