Search code examples
spring-bootspring-securityswaggeropenapikong

Swagger doc url returns JWT Token instead of JSON String


I have a spring boot microservice with spring boot version 3.3.2 deployed on Kubernetes and exposed via KONG API Gateway. I have added the swagger dependency in my POM file

org.springdoc springdoc-openapi-starter-webmvc-ui 2.6.0

WebConverterConfig.java

@EnableWebMvc
@Configuration
public class WebConverterConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter messageConverter = new StringHttpMessageConverter();
        messageConverter.setSupportedMediaTypes(List.of(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL));
        converters.add(messageConverter);
        converters.add(new MappingJackson2HttpMessageConverter(new ObjectMapper()));
    }
}

SecurityConfig.java

@EnableWebSecurity
@Configuration
@RequiredArgsConstructor
public class SecurityConfig {
    @Bean
    SecurityFilterChain filterChain(HttpSecurity http, CustomAuthenticationEntryPoint customAuthenticationEntryPoint, CustomAccessDenied customAccessDenied) throws Exception {
        return http.cors(cors -> cors.configurationSource(request -> {
                    CorsConfiguration configuration = new CorsConfiguration();
                    configuration.setAllowedOrigins(List.of("*"));
                    configuration.setAllowedMethods(List.of("*"));
                    configuration.setAllowedHeaders(List.of("*"));
                    return configuration;
                }))
                .csrf(AbstractHttpConfigurer::disable)
                .authorizeHttpRequests(auth -> {
                    auth.requestMatchers("/api/v1/auth/**", "/fx-rate-api/v3/api-docs/**", "/fx-rate-api/swagger-ui/**").permitAll();
                });
    }
}

SwaggerConfig.java

@Configuration
public class SwaggerConfiguration {
    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("public")
                .pathsToMatch("/**")
                .build();
    }

    @Bean
    public OpenAPI usersMicroserviceOpenAPI() {
        final String securitySchemeName = "bearerAuth";
        final String securityDomainName = "Bearer";
        return new OpenAPI()
                .openapi("3.0.1")
                .addSecurityItem(new SecurityRequirement()
                        .addList(securitySchemeName)
                        .addList(securityDomainName)
                )
                .components(
                        new Components()
                                .addSecuritySchemes(securitySchemeName,
                                        new SecurityScheme()
                                                .name(securitySchemeName)
                                                .type(SecurityScheme.Type.HTTP)
                                                .scheme("bearer")
                                                .bearerFormat("JWT")
                                )
                );
    }
}

application.properties

springdoc.api-docs.path=/fx-rate-api/v3/api-docs springdoc.swagger-ui.path=/fx-rate-api/swagger-ui.html

I have also exposed the service on KONG Gateway.

When I call my url https://api.dev.cslpay.io/fx-rate-api/v3/api-docs/public to view the doc, it returns a jwt token instead of the json object.

Also when I try to access the swagger ui, I get the error in the image below

Image sowing the swagger ui rendered for my api

I am expecting the swagger ui to load successfully and also, the docs to display as json instead of jwt encoded string


Solution

  • To resolve this issue, I edited the WebConverterConfig.java and extended WebMvcConfigurationSupport instead of WebMvcConfigurer

    @EnableWebMvc
    @Configuration
    public class WebConverterConfig extends WebMvcConfigurationSupport {
    
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            StringHttpMessageConverter messageConverter = new StringHttpMessageConverter();
            messageConverter.setSupportedMediaTypes(List.of(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL));
            converters.add(messageConverter);
            converters.add(new MappingJackson2HttpMessageConverter(new ObjectMapper()));
        }
    }