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
相关产品推荐
相关产品推荐

