我有一个 Spring Boot 微服务,其 Spring Boot 版本为 3.3.2,部署在 Kubernetes 上并通过 KONG API 网关公开。我已经在我的 POM 文件中添加了 swagger 依赖项
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()));
}
}
安全配置.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")
)
);
}
}
应用程序.属性
springdoc.api-docs.path=/fx-rate-api/v3/api-docs
springdoc.swagger-ui.path=/fx-rate-api/swagger-ui.html
我也在KONG Gateway上公开了该服务。
当我调用我的网址 https://api.dev.cslpay.io/fx-rate-api/v3/api-docs/public 来查看文档时,它返回一个 jwt 令牌而不是 json 对象。
此外,当我尝试访问 swagger ui 时,我收到下图中的错误
我期望 swagger ui 能够成功加载,并且文档显示为 json 而不是 jwt 编码的字符串
为了解决此问题,我编辑了 WebConfigurerConfig.java 并扩展了 WebMvcConfigurationSupport
@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()));
}
}