java - 允许使用 Spring Security 匿名访问 springdoc-openapi-ui

标签 java spring spring-security openapi springdoc

如何允许匿名访问 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/

相关文章:

java - 从父 Pane 加载现有 FXML

Java 信号量中的多线程概念

java - 如何在 child 的 pom 中将 jacoco 覆盖率设置为 60%?

java - Spring Security 不适用于 "hasRole(' ROLE_ADMIN')"或 ROLE_ADMIN

java - 在不同的 Web 应用程序中使用 Spring Security

java - Android SQLite SELECT 查询

spring - 自定义@RequestMapping注解

java - Spring Security询问身份验证问题

spring - 使用 spring3 @Value 访问 PropertyPlaceholderConfigurer 值?

grails - 在同一个项目中同时使用 Spock 和 Spring Security 插件是否有任何错误?