Rest API not working with client side scripting technologies [Cross-Origin Resource Sharing (CORS)

Hi,

I am unable to consume Rest API’s exposed from Cuba platform web module through AngularJS.
After some research I found that it’s due to CORS specification that during initial handshake OPTIONS http method is sent to the Rest API before POST method is invoked.

Now coming to the question, how should I allow access to Cuba Rest APIs via client side scripting technologies such as AngularJS or how to enable CORS on Cuba Platform?

Hi,

By default CUBA REST-API allows Cross-Origin Resource Sharing, it adds Access-Control-Allow-Origin: * header to all responses of LoginController and DataServiceController.

If you have problems with standard LoginController / DataServiceController please check if you have api pattern in your portal-dispatcher-spring.xml


<http pattern="/api/**" security="none"></http>

If you want to expose your own controller/method for another origin you can use @CrossOrigin from Spring framework:


@CrossOrigin(origins = "*") // or [url=http://my-cool-front-end.test]http://my-cool-front-end.test[/url]
@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public String test() {
    return "{}";
}

You can read more about CORS and Spring here: https://spring.io/guides/gs/rest-service-cors/

Also you can try Portal sample from Studio Samples menu where we’ve implemented simple public facing app for CUBA using Angular JS.

Hi Yuriy,

It doesn’t work.
As for the sample platform-sample-portal, it’s working as both the AngularJS client and Server are in same domain i.e same host and port.
If you just deploy the same platform-sample-portal in another tomcat or other application server which is running on another server or even if you start another instance of tomcat and deploy only the AngularJS Client it wont’ work.
As the two different tomcat instances are separate processes this will reproduce the CORS issue in Cuba.

I’m sure this is some sort of bug as I have tried the mentioned approach of adding the entry in portal-dispatcher-spring.xml but it doesn’t work either.

You could try the attached sample to reproduce the issue and please assist to resolve it as this is hindering the POC process for us.
Please note: I have been running Cuba sample application on Port 7070 on my localhost and the attached AngularJS app is running in another instance of tomcat on same host on port 8080. Let me know in case you face any issue with execution of the attachment.

AngularJSTest.zip (432.6K)

Please check the latest version of portal sample, we have added @CrossOrigin(origins = “*”) for the method of MenuController. The problem was with this custom Spring MVC controller, not with generic REST-API.

I’ve tried your example with two tomcats on different domains and ports and it works with the current version of portal sample.