如何允许匿名访问 springdoc-openapi-ui (OpenAPI 3.0 /swagger-ui.html
)在由 Spring Security 保护的 Spring Boot 应用程序中?
最佳答案
要使用 springdoc-openapi-ui /swagger-ui.html
,请允许匿名访问 WebSecurityConfigurerAdapter
中的以下端点使用permitAll方法:
/v3/api-docs/**
/swagger-ui/**
/swagger-ui.html
示例:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.
.authorizeRequests()
.antMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic(); //or anything else, e.g. .oauth2ResourceServer().jwt()
}
}
确保项目具有以下依赖项:
关于java - 允许使用 Spring Security 匿名访问 springdoc-openapi-ui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59898402/