Spring Boot 403错误提示(openapi v3)

azpvetkf  于 2023-01-05  发布在  Spring
关注(0)|答案(1)|浏览(345)

我使用springdoc-openapi-ui来编写API文档

<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-ui</artifactId>
   <version>1.6.14</version>
</dependency>

并且,遵循Spring Boot安全配置

.
.
public static String[] SWAGGER_WHITELIST = {
        "/api-docs",
        "/swagger-ui.html",
        "/swagger-resources/**",
        "/webjars/**",
        "/swagger.json"
};
@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.cors().disable();
        http.csrf().disable();

        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http
            .authorizeHttpRequests()
                .requestMatchers(SWAGGER_WHITELIST).permitAll()
                .requestMatchers(AUTH_WHITELIST).permitAll()

        .and()
            .addFilterAt(new JWTAuthenticationFilter(userService, jwtService, authenticationProvider()), UsernamePasswordAuthenticationFilter.class)
//            .addFilterAfter(new UserAuthorizationFilter(), JWTAuthenticationFilter.class)
            .authorizeHttpRequests()
                .anyRequest().authenticated();

        return http.build();
    }
.
.

Spring Boot父版本:3
当我尝试访问http://localhost:8080/swagger-ui.html时,我得到了403。
有人面临类似的问题吗?问题可能是什么?
我试过了

  • 将招摇的URL列入白名单
  • 从配置更改swagger文档路径

我越来越

  • 调试失败,因为控制台未显示任何异常
  • 它只是拒绝请求而不打印任何日志
4c8rllxm

4c8rllxm1#

以下更改为我修复了该问题

  • 从springdoc-openapi-ui:1.6.14更改为springdoc-openapi-starter-webmvc-ui:2.0.2,因为它支持spring Boot v3。
  • 在白名单中添加了以下内容
public static String[] SWAGGER_WHITELIST = {
            "/api-docs/**",
            "/api-docs.yaml",
            "/swagger-ui/**",
            "/swagger-ui.html",
    };
  • 新的.properties文件(与白名单匹配)
#Swagger
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.api-docs.path=/api-docs

相关问题