Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

📖 the

Good attitude, not so tired. In a good mood, all you see is beautiful scenery.Copy the code

"If you can't solve a problem for a while, use this opportunity to see your limitations and put yourself out of order." As the old saying goes, it's easy to let go. If you are distracted by something, learn to disconnect. Cut out the paranoia, the trash in the community, and get rid of the negative energy. Good attitude, not so tired. In a good mood, all you see is beautiful scenery.

In a production environment, we need to turn off the Swagger configuration to avoid exposing the interface to this dangerous behavior.


🌂 method


🌂 Disable method 1:

Using the @value () annotation is recommended

package com.dc.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/ * * *@author sunny chen
 * @version V1.0
 * @Package com.dc.config
 * @date2018/1/16 opening *@Description: Main purpose: Enable online interface documents and add related configurations */
@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurerAdapter {

    @Value("${swagger.enable}")
    private Boolean enable;
   
    @Bean
    public Docket createRestApi(a) {
        return new Docket(DocumentationType.SWAGGER_2)
            .enable(enable)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dc.controller"))
                .paths(PathSelectors.any())
                //.paths(PathSelectors.none())
                .build();
    }

    private ApiInfo apiInfo(a)  {
        return new ApiInfoBuilder()
                .title("Auth System Data Interface Document")
                .description("This system is a new architecture Api documentation")
                .termsOfServiceUrl("")
                .contact(new Contact("Yongjia Chen [email protected]".""."https://blog.csdn.net/Mrs_chens"))
                .version("1.0")
                .build();
    }

    /** * Swagger UI resource mapping *@param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /** * Swagger -ui.html path mapping, browser use /api-docs access *@param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/api-docs"."/swagger-ui.html"); }}Copy the code

🌂 Disable Method 2:

Use the annotation @profile ({” dev “, “test”}) to indicate that it is turned on in development or test environments and turned off in production. (Recommended)

package com.dc.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/ * * *@author sunny chen
 * @version V1.0
 * @Package com.dc.config
 * @date2018/1/16 opening *@Description: Main purpose: Enable online interface documents and add related configurations */
@Configuration
@EnableSwagger2
@ Profile ({" dev ", "test"})
public class Swagger2Config extends WebMvcConfigurerAdapter {

    @Bean
    public Docket createRestApi(a) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dc.controller"))
                .paths(PathSelectors.any())
                //.paths(PathSelectors.none())
                .build();
    }

    private ApiInfo apiInfo(a)  {
        return new ApiInfoBuilder()
                .title("Auth System Data Interface Document")
                .description("This system is a new architecture Api documentation")
                .termsOfServiceUrl("")
                .contact(new Contact("Yongjia Chen [email protected]".""."https://blog.csdn.net/Mrs_chens"))
                .version("1.0")
                .build();
    }

    /** * Swagger UI resource mapping *@param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /** * Swagger -ui.html path mapping, browser use /api-docs access *@param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/api-docs"."/swagger-ui.html"); }}Copy the code

🌂 Disable Method 3:

ConditionalOnProperty(name = “swagger. Enable”, havingValue = “true”) and then add swagger. Enable = true to your test or development configuration. If the production environment is not specified, Swagger is disabled by default.

package com.dc.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/ * * *@author sunny chen
 * @version V1.0
 * @Package com.dc.config
 * @date2018/1/16 opening *@Description: Main purpose: Enable online interface documents and add related configurations */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name ="enabled" ,prefix = "swagger",havingValue = "true",matchIfMissing = true)
public class Swagger2Config extends WebMvcConfigurerAdapter {

    @Bean
    public Docket createRestApi(a) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dc.controller"))
                .paths(PathSelectors.any())
                //.paths(PathSelectors.none())
                .build();
    }

    private ApiInfo apiInfo(a)  {
        return new ApiInfoBuilder()
                .title("Auth System Data Interface Document")
                .description("This system is a new architecture Api documentation")
                .termsOfServiceUrl("")
                .contact(new Contact("Yongjia Chen [email protected]".""."https://blog.csdn.net/Mrs_chens"))
                .version("1.0")
                .build();
    }

    /** * Swagger UI resource mapping *@param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /** * Swagger -ui.html path mapping, browser use /api-docs access *@param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/api-docs"."/swagger-ui.html"); }}Copy the code

#Swagger lock
swagger:
    enabled: true
Copy the code

Finally, thank you for your patience to watch the end, the original is not easy, leave a point like collection is your biggest encouragement to me!


🎉 summary:

  • For more references, see here:The Blog of Chan Wing Kai

  • Like the small partner of the blogger can add a concern, a thumbs-up oh, continue to update hey hey!