Spring Cloud APIGW, Spring Boot and OpenAPI issue - CORS issue
Asked Answered
W

3

3

I am using Spring Boot and Microservices stack using Spring Cloud APIGW. I am using the same code mentioned here: https://piotrminkowski.com/2020/02/20/microservices-api-documentation-with-springdoc-openapi/

When I hit any endpoint, I don't see response is coming and getting below error.

Access to fetch at 'http://192.168.0.2:49382/' from origin 'http://localhost:8060' has been blocked by CORS policy: 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.

Source code: https://github.com/piomin/sample-spring-microservices-new

enter image description here

Werewolf answered 15/8, 2022 at 5:9 Comment(0)
W
3

I was able to fix it by myself looking at suggestion here: Spring Cloud Gateway and Springdoc OpenAPi integration and https://github.com/springdoc/springdoc-openapi/issues/1144

I had to add below in apigw-service in application.properties file

server:
  forward-headers-strategy: framework

Also, in each microservice, you need to add below bean

@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {

            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
            }
        };
    }
Werewolf answered 16/8, 2022 at 14:58 Comment(4)
I followed all the steps that you mentioned and I am still getting this error. Weird.Bibliotherapy
server.forward-headers-strategy=framework The above property will go inside all the microservice which are running behind api-gateway and then it works. :) Thanks a lot buddy @WerewolfBibliotherapy
The Java code is not required. application.properties is sufficient to fix this issue.Bibliotherapy
@an0nh4x0r was correct, this fixed it for me, though just the property was needed and the bean wasn't required.Illusion
F
1

You should add swagger configuration

@Configuration
@OpenAPIDefinition(servers = {
    @Server(url = "/", description = "Default Server URL")
})
public class SwaggerConfiguration {

@Bean
public OpenAPI customOpenAPI(@Value("springdoc-openapi-ui") String serviceTitle, @Value("1.6.12") String serviceVersion) {
    final String securitySchemeName = "bearerAuth";
    return new OpenAPI()
            .components(
                    new Components().addSecuritySchemes(
                            securitySchemeName,
                            new SecurityScheme()
                                    .type(SecurityScheme.Type.HTTP)
                                    .scheme("bearer")
                                    .bearerFormat("JWT")
                    )
            )
            .security(List.of(new SecurityRequirement().addList(securitySchemeName)))
            .info(new Info().title(serviceTitle).version(serviceVersion));
}
}
Foeticide answered 18/11, 2022 at 14:55 Comment(0)
S
0

It worked me by adding the below application.properties

server.forward-headers-strategy: framework

in the Springboot-3.3.2 version, and used the swagger UI generation maven dependency

     <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.5.0</version>
    </dependency>

initally I added the Java code, actually it is not required. I tested by using the NGROCK server .

Shanelleshaner answered 27/7, 2024 at 3:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.