Skip to content
Advertisement

No ‘Access-Control-Allow-Origin’ header is present (CORS) – Spring Boot (Spring security) Microservices + Vue.js

I’m working on Spring Boot project based on microservices architecture on backend and Vue.js on frontend. Structure of my project is next: Project structure

For avoiding CORS error usually I add @CrossOrigin annotation on to class and it works. It was all good and has been working well, until I added security part with ability to login users.

What did I did:

1. To API Gateway that built on spring-cloud-gateway I’ve added AuthFilter that uses as interceptor to create and check JWT:
api-gateway/src/main/java/.../AuthFilter.java

JavaScript

2. I’ve added AuthFilter as filter to each service in application.properties:
api-gateway/src/resource/application.properties

JavaScript

3. Manager-service microservice used to control base entities for system, such as users, roles, organizations where users working are and so on, so here I added SecurityConfig and WebConfig, because this microservice will be responsible for JWT generating:
manager-service/src/main/java/.../SecurityConfig.java

JavaScript

manager-service/src/main/java/.../WebConfig.java

JavaScript

4. In controller, that represents auth I also added @CrossOrigin annotation to class:
manager-service/src/main/java/.../AuthController.java

JavaScript

5. For frontend I use Vue.js. For requests I use axios. Here are post-request to login:

JavaScript

All what I’m getting is an error: Access to XMLHttpRequest at 'http://localhost:8080/api/v1/auth/signIn' from origin 'http://localhost:8100' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.. Below you’ll see headers, that displays Chrome with request:
enter image description here
I’ve been trying to add another one corsConfiguration, tried to mark with CrossOrigin annotation only method, not class at all but it hadn’t take any effects. If I try to make such requests with postman it gives me expected response with generated token.

I’ll be grateful for any idea what could I do wrong.
Thanks!

UPDATE: As I understood well – all problems is in api-gateway. If I make requests directly to service – I get right response, but if I make request through gateway – I’m facing an error, logs of api-gateway below:

JavaScript

Advertisement

Answer

After research I’ve solved problem. It was all Gateway’s fault
As I mentioned before, direct request gives me right response, but only if I go through api-gateway it gives me an errors.

So solution is to add CORS Configuration rules to gateway:

JavaScript

Please, note that if you don’t add section with gateway: default-filters you will be facing similar error with header that contains multiple values.

Thanks to answer by Pablo Aragonés in another question and Spring Cloud Documentation

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement