You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot 3整合OpenApi遇403错误,配置权限后仍未解决

问题解决方案

1. 修复版本兼容性问题

Spring Boot 3.0.x 要求 springdoc-openapi 组件使用 2.x 版本,你当前使用的1.6.15是适配Spring Boot 2.x的版本,版本不匹配会导致路径映射、配置逻辑异常,这是核心问题。

更新依赖为Spring Boot 3适配的starter:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.2.0</version>
</dependency>

2. 简化并修正Security配置

移除WebSecurityCustomizer Bean,它与SecurityFilterChain的配置可能产生冲突,统一在SecurityFilterChain中配置放行规则即可:

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    AuthenticationFilter authenticationFilter = new AuthenticationFilter(customAuthenticationManager, userService);
    authenticationFilter.setFilterProcessesUrl("/authenticate");
    
    http.cors().and().csrf().disable()
            .authorizeHttpRequests(auth -> auth
                    // 放行所有Swagger相关路径
                    .requestMatchers("/swagger-ui/**", "/v3/api-docs/**", "/swagger-resources/**")
                    .permitAll()
                    // 放行注册接口
                    .requestMatchers(HttpMethod.POST, "/user/register")
                    .permitAll()
                    // 其余请求需认证
                    .anyRequest()
                    .authenticated()
            )
            .addFilter(authenticationFilter)
            .addFilterAfter(new JWTAuthorizationFilter(), AuthenticationFilter.class);
    
    return http.build();
}

3. 验证访问路径

启动项目后,使用以下路径访问:

  • Swagger UI页面:http://localhost:你的端口号/swagger-ui/index.html
  • OpenAPI文档数据:http://localhost:你的端口号/v3/api-docs

内容的提问来源于stack exchange,提问作者wazzupek01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 21:37:32