Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger 3.0.0: Can't disable in production without SwaggerConfig and @Profile

I'm upgrading to SpringFox Swagger 3.0.0 from 2.x, which introduces the Spring Boot starter springfox-boot-starter dependency that obviates the need for the 2.x-based SwaggerConfig:

/**
 * NO LONGER NEEDED
 */
@Configuration
@EnableSwagger2
@Profile({"local", "dev", "beta"}) // <- HOW TO DISABLE IN PROD INSTEAD OF THIS
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

Now that I no longer need this @Configuration, which allows me to specify my environment profiles in @Profile and therefore disable Swagger in production, how do I disable Swagger in production in SpringFox Swagger-UI 3.x?

NOTE: There is Spring Security-based approached discussed here that could be an option for some, but is not an option for this scenario for two reasons:

  • My application does not use Spring Security and it is not possible to include the spring-boot-security-starter dependency
  • It requires whitelisting all other endpoints in order to get them working again, which is not acceptable
like image 330
Geyser14 Avatar asked Dec 30 '22 14:12

Geyser14


1 Answers

The answer was not easy to find and was NOT in SpringFox's migration guide or documentation here (where it should be).

The CORRECT and by far best answer for Swagger UI 3.0.0 is here.

Just add springfox.documentation.enabled=[true|false] to the target environment's application.properties or application.yml.

As an aside, it would be nice to see a section with the list of all available Spring Boot properties listed in the SpringFox doc.

like image 133
Geyser14 Avatar answered Jan 02 '23 05:01

Geyser14