I have upgraded my spring version to 2.7.12 and migrating from springfox swagger 2 to springdoc openapi 3 (swagger 3). The swagger config api is working fine but due to webMVCConfig in my application , the api-docs api response is coming encryted as a JWT token .
I tried following options :
- Ignoring the swagger urls in WebMVConfig configuration classes .
- Added resource handlers
- Added custom httpconverters for StringHttpMessageConverter and ByteArrayHttpMessageConverter
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").permitAll();
}
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.components(new Components())
.info(new Info().title("CONTACT Management").description(
"This is a sample Spring Boot RESTful service using springdoc-openapi and OpenAPI 3.").version("3.0.0"));
}
}
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private RequestInterceptor requestInterceptor;
@Value("${allowed.headers}")
private String allowedHeaders;
@Override
public void configureMessageConverters(@NotNull List<HttpMessageConverter<?>> converters) {
WebMvcConfigurer.super.configureMessageConverters(converters);
converters.add(0, new MappingJackson2HttpMessageConverter(provideObjectMapper()));
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
}
@Bean
public ObjectMapper provideObjectMapper() {
return new ObjectMapper()
.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").exposedHeaders(Constants.AUTHORIZATION_TOKEN).
allowedHeaders(allowedHeaders.split(","));
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(requestInterceptor);
}
}
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webmvc-core</artifactId>
<version>1.7.0</version>
</dependency>`
I tried following options :
- Ignoring the swagger urls in WebMVConfig configuration classes .
- Added resource handlers
- Added custom httpconverters for StringHttpMessageConverter and ByteArrayHttpMessageConverter [Swagger UI](https://i.stack.imgur.com/sOaxJ.png)