我正在尝试将 Springdoc open-api 定义集成到我的项目中。目前,由于某些奇怪的原因 /v3/api-docs 工作正常,它正在获取 API 的 JSON 版本,但 UI 不起作用,控制台正在打印未找到 index.html (404)。有没有办法让这个功能发挥作用?最新版本的 Springboot 有没有人遇到类似的问题?
这是我的安全配置:
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfiguration extends WebMvcConfigurationSupport {
private final JwtAuthenticationFilter jwtAuthFilter;
private final AuthenticationProvider authenticationProvider;
private static final String[] URL_WHITELIST = {
ApplicationConstants.AUTH + "/**",
ApplicationConstants.PARKING_SPOT + ID,
"/error",
"/favicon.ico",
"/swagger-resources",
"/swagger-resources/**",
"/configuration/ui",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**",
"/v3/api-docs/**",
"/api/public/**",
"/api/public/authenticate",
"/actuator/*",
"/swagger-ui/**"
};
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorize -> authorize
.requestMatchers(URL_WHITELIST)
.permitAll()
.anyRequest()
.authenticated())
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authenticationProvider(authenticationProvider)
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
以及OpenAPI初始化:
@Configuration
public class OpenApiConfiguration {
@Bean
public OpenAPI openAPI() {
Info info = new Info()
.title("ParkingTime API")
.version("1.0");
return new OpenAPI().info(info);
}
}
波姆:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.18</version>
</dependency>
控制台:
2023-11-17T10:56:36.805+01:00 WARN 8272 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound : No mapping for GET /swagger-ui/index.html
2023-11-17T10:56:37.440+01:00 WARN 8272 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound : No mapping for GET /swagger-ui/index.html
由于您从
WebMvcConfigurationSupport
扩展,Spring Boot 将跳过 WebMvcAutoConfiguration
,这反过来又会导致 Swagger UI 自动配置也被跳过。
解决此问题的最简单方法是不要从
WebmvcConfigurationSupport
扩展。如果您确实需要提供额外的配置,您可以从DelegatingWebMvcConfiguration
进行扩展,但在大多数情况下,您根本不需要扩展它。