javaspringswagger

Spring Disable Swagger-ui for production


I know how to disable swagger for production - i only need to add annotation @Profile("!prod") in configuration class:

@Configuration
@EnableSwagger2
@RequiredArgsConstructor
@Profile("!prod")
public class SwaggerConfig {

result of adding annotation But the result is, that the swagger-ui.html still is available in browser, only its empty. I wonder is there solution to disable it fully, so the page will not load?


Solution

  • this could be simply done with spring-security by blocking the url for the production environment. Please try :

    Add dependency (if you are using spring-boot) to pom.xml :

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

    Add configuration file :

    @Configuration
    @Profile("prod")
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatchers("/**/swagger-ui.html").denyAll();
        }
    }
    

    It will send 403 forbidden status.