使用Spring MVC禁用Swagger

Ath*_*mas 16 spring-mvc swagger

我正在使用Swagger和Spring MVC.我想在特定环境(如生产)中有选择地禁用招摇.我怎样才能做到这一点?

Dil*_*nan 40

万一你使用1.x版本的springfox以前swagger-springmvc

配置swagger spring-mvc插件时,您可以使用enable基于环境/配置文件等传递布尔值的方法.

@Bean 
public SwaggerSpringMvcPlugin customImplementation(){
    return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
        .apiInfo(apiInfo())
        .enable(environmentSpeficicBooleanFlag) //<--- Flag to enable or disable possibly loaded using a property file
        .includePatterns(".*pet.*");
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用弹簧轮廓

@Bean
@Profile("production")
public SwaggerSpringMvcPlugin customImplementation(){
    return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
        .apiInfo(apiInfo())
        .enable(false) //<--- Flag set to false in the production profile
        .includePatterns(".*pet.*");
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是2.x版本的springfox

配置swagger spring-mvc插件时,您可以使用enable基于环境/配置文件等传递布尔值的方法.

@Bean 
public Docket customImplementation(){
    return new Docket(SWAGGER_2)
        .apiInfo(apiInfo())
        .enable(environmentSpeficicBooleanFlag) //<--- Flag to enable or disable possibly loaded using a property file
        .includePatterns(".*pet.*");
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用弹簧轮廓

@Bean
@Profile("production")
public Docket customImplementation(){
    return new Docket(SWAGGER_2)
        .apiInfo(apiInfo())
        .enable(false) //<--- Flag set to false in the production profile
        .includePatterns(".*pet.*");
}
Run Code Online (Sandbox Code Playgroud)

  • /swagger-ui.html仍然可用,但没有方法.有没有办法禁止URL? (3认同)