[英]403 Forbidden for OpenApiSwagger in Spring Boot Security
我有 Spring 具有 Spring 安全性的啟動應用程序。 配置配置類 localhost-Swagger 后返回 403 Forbidden。 我無法弄清楚可能是什么問題。
http://localhost:8080/swagger-ui.html#/ - 403 禁止
安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
@Bean
public SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests()
.antMatchers("/api/user/login").permitAll()
.anyRequest().authenticated();
return http.build();
}
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.antMatchers("/api/user/auth")
.antMatchers("/v3/api-docs")
.antMatchers("/swagger-resources/**")
.antMatchers("/swagger-ui.html")
.antMatchers("/configuration/**")
.antMatchers("/webjars/**")
.antMatchers("/public");
}
}
build.gradle:
dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.7.2'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.7.2'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.7.2'
implementation group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '9.4.1.jre16'
implementation group: 'org.json', name: 'json', version: '20220320'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.9'
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
}
PS 請不要建議從 WebSecurityConfigurerAdapter 繼承 class,因為它在我的 Spring Security 版本中已棄用。
您可以在 web 安全配置中使用“/swagger-ui/**”而不是“/swagger-ui.html”。
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.antMatchers("/api/auth/**")
.antMatchers("/v3/api-docs/**")
.antMatchers("configuration/**")
.antMatchers("/swagger*/**")
.antMatchers("/webjars/**")
.antMatchers("/swagger-ui/**");
}
你把springdoc.swagger-ui.path
放在你的應用程序屬性文件中了嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.