【问题标题】:Spring Boot Swagger API not workingSpring Boot Swagger API 不起作用
【发布时间】:2017-06-12 22:41:26
【问题描述】:

这是我的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>

我正在使用 Spring Boot 的 1.5.3.RELEASE 版本。这是我的招摇配置文件:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket swagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

这是我的WebSecurityConfig.java

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

当我从端点 http://localhost:8080/v2/api-docs 获取时,我会返回我的 JSON:

{
  "swagger": "2.0",
  "info": {
    "description": "Api Documentation",
    "version": "1.0",
    "title": "Api Documentation",
    "termsOfService": "urn:tos",
    "contact": {},
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0"
    }
  },
  "host": "localhost:8080",
  "basePath": "/",
  //ETC
}

但是当我尝试访问localhost:8080/swagger-ui.html 的 UI 时,我得到一个如下所示的空白页面:

如果我点击该页面,我会得到提升

我做错了什么?这是某种春季安全问题吗?

【问题讨论】:

标签: java spring spring-boot spring-security swagger


【解决方案1】:

您可以在应用程序配置中向 Swagger 建议 API 描述路径,使用 springfox.documentation.swagger.v2.path 属性,例如 springfox.documentation.swagger.v2.path: /rest/docsapplication.yml

我已经发布了一个示例on github

【讨论】:

  • 这并没有真正帮助,因为我已经可以从 http://localhost:8080/v2/api-docs 访问 api json。我只是无法让 swagger-ui.html 从中读取
  • @Richard 尝试将 Web 安全忽略列表中的 "/swagger-resources" 更改为 "/swagger-resources/**",因为 Swagger 会转到 /swagger-resources/configuration/ui 端点来获取其配置。
  • @Richard 我已经使用安全配置更新了我的 github 示例。
【解决方案2】:

如果您使用 版本 - V3 || io.springfox >= 3.0.0

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-boot-starter</artifactId>
   <version>3.0.0</version>
</dependency>

Java 代码

@Configuration
@EnableSwagger2

public class SwaggerConfig {

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2).select()
            .apis(RequestHandlerSelectors.basePackage("Your Controller package name"))
            .paths(PathSelectors.any()).build();
}

}

V3 浏览器 URL -> http://localhost:8080/swagger-ui/#/ 运行(必须):Mvn clean

【讨论】:

  • 这就像一个魅力,谢谢!
【解决方案3】:

很可能 spring-security 不允许/阻止您的端点被发现。尝试将您的蚂蚁匹配器更改为以下内容,看看是否有帮助。安全/ui 配置端点不正确。

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

【讨论】:

【解决方案4】:

将 swagger 版本更改为 2.9.2 即可。

   <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.9.2</version>
   </dependency>

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    相关资源
    最近更新 更多