In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the ways of disabling Swagger in SpringBoot". In daily operation, I believe that many people have doubts about the way SpringBoot disables Swagger. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "what is the way to disable Swagger in SpringBoot?" Next, please follow the editor to study!
Abstract
In a production environment, we need to turn off the swagger configuration to avoid this dangerous behavior of exposing the interface.
Method disable method 1:
Recommended using the annotation @ Value ()
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 * @ date on 2018-1-16 17:33 * @ Description: main purpose: open online interface documentation and add related configuration * / @ Configuration@EnableSwagger2public class Swagger2Config extends WebMvcConfigurerAdapter {@ Value ("${swagger.enable}") private Boolean enable @ Bean public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .enable (enable) .apiInfo (apiInfo ()) .select () .apis (RequestHandlerSelectors.basePackage ("com.dc.controller")) .paths (PathSelectors.any ()) / / .notify ( PathSelectors.none () .build () } private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("auth system data interface document") .description ("this system is the Api description document for the new architecture") .termsOfServiceUrl (") .contact new Contact (" Chen Yongjia chen867647213@163.com ",") "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") .addResourceLocati ons ("classpath:/META-INF/resources/"); registry.addResourceHandler ("/ webjars/**") .addResourceLocations ("classpath:/META-INF/resources/webjars/") } / * swagger-ui.html path mapping, use / api-docs to access * @ param registry * / @ Override public void addViewControllers (ViewControllerRegistry registry) {registry.addRedirectViewController ("/ api-docs", "/ swagger-ui.html") in the browser;}} disable method 2:
Use the annotation @ Profile ({"dev", "test"}) to indicate that it is turned on in the development or test environment and closed 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 * @ date 17:33 * @ Description on 2018-1-16: main purpose: open online interface documentation and add related configurations * / @ Configuration@EnableSwagger2@Profile ({"dev") "test"}) public class Swagger2Config extends WebMvcConfigurerAdapter {@ Bean public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .select () .apis (RequestHandlerSelectors.basePackage ("com.dc.controller")) .requests (PathSelectors.any ()) / / .notify (PathSelectors. None () .build () } private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("auth system data interface document") .description ("this system is the Api description document for the new architecture") .termsOfServiceUrl (") .contact new Contact (" Chen Yongjia chen867647213@163.com ",") "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") .addResourceLocati ons ("classpath:/META-INF/resources/"); registry.addResourceHandler ("/ webjars/**") .addResourceLocations ("classpath:/META-INF/resources/webjars/") } / * swagger-ui.html path mapping, use / api-docs to access * @ param registry * / @ Override public void addViewControllers (ViewControllerRegistry registry) {registry.addRedirectViewController ("/ api-docs", "/ swagger-ui.html") in the browser;}} disable method 3:
Use the annotation @ ConditionalOnProperty (name = "swagger.enable", havingValue = "true") and then add swagger.enable = true to the test configuration or development configuration to turn it on. If the production environment is left empty, Swagger is turned off 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 * @ date 17:33 * @ Description on 2018-1-16: main purpose: open online interface documentation and add related configurations * / @ Configuration@EnableSwagger2@ConditionalOnProperty (name = "enabled", prefix = "swagger", havingValue = "true" MatchIfMissing = true) public class Swagger2Config extends WebMvcConfigurerAdapter {@ Bean public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .sele ct () .apis (RequestHandlerSelectors.basePackage ("com.dc.controller")) .steps (PathSelectors.any ()) / / .notify (PathSelectors.none () .build () } private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("auth system data interface document") .description ("this system is the Api description document for the new architecture") .termsOfServiceUrl (") .contact new Contact (" Chen Yongjia chen867647213@163.com ",") "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") .addResourceLocati ons ("classpath:/META-INF/resources/"); registry.addResourceHandler ("/ webjars/**") .addResourceLocations ("classpath:/META-INF/resources/webjars/") } / * swagger-ui.html path mapping, use / api-docs to visit * @ param registry * / @ Override public void addViewControllers (ViewControllerRegistry registry) {registry.addRedirectViewController ("/ api-docs", "/ swagger-ui.html") {registry.addRedirectViewController ("/ api-docs", "/ swagger-ui.html") in the browser. At this point, the study on "what are the ways to disable Swagger in SpringBoot" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.