springdoc / springdoc-openapi-demos

Demo for OpenAPI 3 with spring-boot
http://springdoc.org
Apache License 2.0
493 stars 267 forks source link

CORS policy for springdoc-openapi-microservices #60

Closed fred314 closed 1 year ago

fred314 commented 1 year ago

Hi,

When testing the springdoc-openapi-microservices, I get an error when I want to test any endpoint with the "Try it out" button: Access to fetch at 'http://10.64.141.162:8091/' from origin 'http://localhost:8060' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Is it supposed to be working? (if yes, then I guess it's my way to run this locally which is wrong...) Please excuse me if it's not the correct place to ask this question.

fred314 commented 1 year ago

Hi,

Ok, I solved this problem (and mine at the same time!) by adding the parameter servers inside the annotation @OpenAPIDefinition. Here is the example for the department module: @OpenAPIDefinition(servers = @Server(url = "http://localhost:8060/department"), info = @Info(title = "Department API", version = "${springdoc.version}", description = "Documentation Department API v1.0") )