【问题标题】:Spring Boot Swagger UI - Protect UI AccessSpring Boot Swagger UI - 保护 UI 访问
【发布时间】:2018-01-27 17:01:57
【问题描述】:

我通过将以下类添加到我的代码中,向我现有的 springboot REST API 添加了一个简单的招摇 UI:

@EnableSwagger2
@Configuration
public class SwaggerConfig {                                    
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
            .select()
            .paths(PathSelectors.regex("/v1.*"))
            .build()
            .pathMapping("/")
            .apiInfo(metadata());
    }


    private ApiInfo metadata() {
        return new ApiInfoBuilder()
          .title("My awesome API")
          .description("Some description")
          .version("1.0")
          .build();
      }
}

我的问题是 API 应该是公开的,但 swagger 文档不应该。我想要一种请求对 swagger 文档进行身份验证的方法,有人知道实现此目的的任何简单方法吗?

我试图用谷歌搜索它,但我只能找到 OAth 的东西,但这是对端点的身份验证,而不是招摇文档...

【问题讨论】:

标签: spring authentication spring-boot swagger-ui


【解决方案1】:
当 Swagger 与 Spring Boot 应用程序集成时,

Swagger 文档将在 /v2/api-docs 端点提供。

为了保护资源,利用spring security并限制访问文档的端点

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

安全配置:限制对端点的访问仅限于用户

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()               
                .antMatchers("/v2/api-docs").authenticated()
                .and()
                .httpBasic();

    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

另外,swagger-ui.html也可以根据需求进行保护。

【讨论】:

  • 非常感谢,这正是我想要的
  • @Barath:那么如何将用户详细信息传递给加载 swagger UI 呢?我的意思是,如果我使用上面的代码保护 swagger UI,登录会自动弹出吗?我有业务端点的 JWT 身份验证,我也希望同样用于 Swagger UI。
  • @Barath:我想,有了 JWT,事情就有点复杂了。我没有弹出,但 未能加载 API 定义。 undefined localhost:8080/mycontext/v2/api-docs ...由于您使用 inMemoryAuthentication 编码,因此您可能会弹出。此外,点击 swagger UI 并不能控制我的 JWT 过滤器。
  • 发布一个新问题需要了解设置。如果您注册了不同的过滤器,则需要使用不同的匹配器来处理它
  • 你能分享一下剔除类吗..因为我的不工作
【解决方案2】:

这是另一种解决方案。这是关于仅在开发/质量保证环境中限制对 swagger 的访问。生产环境将无法访问 Swagger。我使用属性 (prop.swagger.enabled) 作为标志,仅在开发/qa 环境中绕过 swagger-ui 的 spring 安全身份验证。

@Configuration
@EnableSwagger2
public class SwaggerConfiguration extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {

@Value("${prop.swagger.enabled:false}")
private boolean enableSwagger;

@Bean
public Docket SwaggerConfig() {
    return new Docket(DocumentationType.SWAGGER_2)
            .enable(enableSwagger)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.your.controller"))
            .paths(PathSelectors.any())
            .build();
}

@Override
public void configure(WebSecurity web) throws Exception {
    if (enableSwagger)  
        web.ignoring().antMatchers("/v2/api-docs",
                               "/configuration/ui",
                               "/swagger-resources/**",
                               "/configuration/security",
                               "/swagger-ui.html",
                               "/webjars/**");
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (enableSwagger) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
  }
}

【讨论】:

    【解决方案3】:

    我使用这个锅炉板来配置和保护 swagger

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig extends WebSecurityConfigurerAdapter {
    
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any()).build();
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
            .antMatchers("/v2/api-docs",
                    "/configuration/ui",
                    "/swagger-resources/**",
                    "/configuration/security",
                    "/swagger-ui.html",
                    "/webjars/**")
            .authenticated().and().httpBasic();
    
        }
    
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-16
      • 2023-01-04
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 2017-04-13
      相关资源
      最近更新 更多