【问题标题】:How to provide authentication in Swagger API on Spring Boot application如何在 Spring Boot 应用程序的 Swagger API 中提供身份验证
【发布时间】:2019-01-23 18:31:50
【问题描述】:

我已集成 Swagger 以使用 Spring Boot 为 Spring REST 应用程序生成 API 文档。它运行良好,当我点击 URL 时,我可以看到生成的 API 文档:http://localhost:8080/test/swagger-ui.html 我的问题是如何限制对 API 的访问?基于硬编码的用户名和密码的基本身份验证至少在开始时应该足够好。我使用 maven 添加“swagger2”依赖项。

这里是 pom.xml:

<dependency>                                                                           
    <groupId>io.springfox</groupId>                                                      
    <artifactId>springfox-swagger2</artifactId>                                          
    <version>2.7.0</version>                                                             
</dependency>                                                                          
<dependency>                                                                           
    <groupId>io.springfox</groupId>                                                      
    <artifactId>springfox-swagger-ui</artifactId>                                        
    <version>2.7.0</version>                                                             
</dependency>  

这是招摇的配置:

@Configuration                                                                         
@EnableSwagger2                                                                        
public class SwaggerConfig {                                                           
    @Bean                                                                              
    public Docket api() {                                                              
        return new Docket(DocumentationType.SWAGGER_2)                                 
          .select()                                                                    
          .apis(RequestHandlerSelectors.basePackage("com.eeocd.test.ws.resource"))     
          .build();                                                                    
    }                                                                                  
}                                                                                      

【问题讨论】:

    标签: swagger swagger-2.0


    【解决方案1】:

    您可以通过向 Docket 对象添加 securityScheme 和 securityContext 来启用身份验证。

    @Configuration                                                                         
    @EnableSwagger2                                                                        
    public class SwaggerConfig {                                                           
        @Bean                                                                              
        public Docket api() {                                                              
            return new Docket(DocumentationType.SWAGGER_2)                                 
              .select()                                                                    
              .apis(RequestHandlerSelectors.basePackage("com.eeocd.test.ws.resource"))     
              .build()
              .securitySchemes(newArrayList(basicAuth()))
              .securityContexts(newArrayList(securityContext()));                                                                    
        }
    private BasicAuth basicAuth() {
        BasicAuth ba = new BasicAuth("basic");
        return ba;
    }
    
    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(apiPaths())
                .build();
    }
    
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(new SecurityReference("basic", authorizationScopes));
    }
    
    
    
    private Predicate<String> apiPaths() {
            return or(regex("/api/v1.*")
            );
    
        }
    
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2017-11-21
      • 1970-01-01
      • 2015-04-10
      • 2021-02-28
      • 1970-01-01
      • 2018-04-15
      相关资源
      最近更新 更多