Aks 1316
Aks 1316

Reputation: 382

How to handle CORS configuration of API gateway and Socket in Springboot Microservices

I have a Springboot Microservice application. I have 4 microservices in my application.

API-gateway,
user-management,
idm-service,
engine-service.

I am using react as frontend here.

API-gateway contains CORS related configuration and I am using web-socket for my engine-service so it has web socket configuration. I am facing issue while communicating with socket of engine-service. When I am trying to initiate socket connection from react. First it will go to API-gateway so I have to add required CORS configuration in API-gateway.

API-gateway configuration:

cors-configurations:
  '[/**]':
    allowCredentials: true
    allowedOrigins:
      - "http://localhost:3000"
    allowedHeaders: "*"
    allowedMethods:
      - GET
      - POST
      - DELETE
      - PUT
      - OPTIONS
      - PATCH

I have to add allowCredentials: true to allow web socket connection and it is working fine.

engine-service socket configuration:

@Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint(registryEndpoint).setAllowedOrigins("http://localhost:3000").withSockJS();
  }

Now again I have to add setAllowedOrigins in websocket configuration. so when request comes to engine-service, I am getting error like this

The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:3000, http://localhost:3000', but only one is allowed.

Now I am unable to find solution for this. All the request must go via API-gateway so I have to add CORS configuration there else I will get CORS error in API-gateway. If I am adding setAllowedOrigins in API-gateway I am getting error in engine-service.

Can you please guide me on this error? For websocket connection, Should I directly communicate with engine-service and not going via API-gateway?

Upvotes: 0

Views: 61

Answers (0)

Related Questions