Enable CORS v2 Spring Boot

Using REST web services directly from a web page using javascript/jquery/angularjs is one of the hot issues these days. My guess is that many people (like me) have bugged a bit, trying to find out how to make it work using Spring Boot Rest Web Services.
The obstacle that stands between you and the solution has a name and this is CORS.
On my previous post here, I have some short explanation of what it is and how to overcome it in Java EE environment.
On Spring Boot we will need to do almost the same thing except from the fact that instead of changing a web.xml, we will add a configuration class that does more or less the same thing.

How to start then?  First follow this guide in order to create a REST web service with CORS enabled. Yeah that’s right. Spring.io will give you a great step by step guide how to do it. The problem is though that if it happens to also want to have some basic security there, then the CORS will break again. So, after completing the previous guide open you pom.xml and add the following dependency:


This will add some basic http authentication with a user named “user” and a random password that will be created each time you start your app (something like: Using default security password: 8f608422-3faf-4f94-b60a-67ebd966af10).

Now, the trick is to add the following class:

This will do 2 things.
1) Add a user admin will password 1111 on group USER.
2) Enable OPTIONS request to be called without basic http authentication.

That’s it. you are done!

Leave a Reply

Your email address will not be published. Required fields are marked *