How to Enable CORS in CRXDE Lite/AEM

假如想象 提交于 2019-12-07 00:25:29

As of AEM 6.3 There is an OOTB service called Adobe Granite Cross-Origin Resource Sharing Policy. It was as easy as creating an OSGi configuration with the alloworigin=[http://localhost:8000] property. In my case, my Angular application was running on port 8000 trying to access the publisher on 4503.

Prior AEM 6.3 What I wound up doing (at first) was creating a service that implemented AuthenticationInfoPostProcessor. There, I set the following headers:

  • Access-Control-Allow-Credentials
  • Access-Control-Allow-Origin
  • Access-Control-Allow-Methods

And everything was fine for GET requests. But when we tried POST, we ran into the issue that the browser was sending the pre-flight OPTIONS request which was failing because the browser was not doing it with login-token cookie.

Then we tried a @SlingFiter, however that falls in the normal sling pipeline, therefore it was after authentication was checked, so having no auth cookie, the pre-flight would always fail.

Finally, we implemented a filter with the following annotations:

@Component(immediate = true)
@Service(value = Filter.class)
@Properties({ @Property(name = "pattern",
                        value = "/.*"),
              @Property(name = Constants.SERVICE_RANKING,
                        intValue = 1000) })

The key here was the pattern property, which registers the filter as an Apache Felix Whiteboard filter, not Sling. See here. So the filter will set CORS headers for OPTIONS and return, and set CORS headers for everything else, and pass the request to the next filter in the chain.

I don't know where to enable CORS in AEM (or if it's possible at all, I'd look in the OSGi console http://localhost:4502/system/console/configMgr if anywhere) but one way to work around CORS issues would be to expose AEM and the frontend in the same domain, which should be fairly easy by setting up a proxy on Apache.

On the Apache server in front of your AEM publish instance, in the httpd.conf, you could do something like this:

ProxyPass /your-frontend http://whatever.it.runs.on/actual-path-to-the-frontend/
ProxyPassReverse /your-frontend http://whatever.it.runs.on/actual-path-to-the-frontend/
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!