Spring Boot 禁用 Swagger 的三種方式

      網友投稿 1203 2025-04-04

      一、序言

      在生產環境下,我們需要關閉swagger配置,避免暴露接口的這種危險行為。

      二、方法:

      禁用方法1:

      使用注解 @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 zhaohp

      * @version V1.0

      * @Package com.dc.config

      * @date 2018/1/16 17:33

      * @Description: 主要用途:開啟在線接口文檔和添加相關配置

      */

      @Configuration

      @EnableSwagger2

      public 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())

      //.paths(PathSelectors.none())

      .build();

      }

      private ApiInfo apiInfo() {

      return new ApiInfoBuilder()

      .title("auth系統數據接口文檔")

      .description("此系統為新架構Api說明文檔")

      .termsOfServiceUrl("")

      .contact(new Contact("趙化鵬 18310695431@163.com", "", "zhaohuapeng@zichan360.com"))

      .version("1.0")

      .build();

      }

      /**

      * swagger ui資源映射

      * @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路徑映射,瀏覽器中使用/api-docs訪問

      * @param registry

      */

      @Override

      public void addViewControllers(ViewControllerRegistry registry) {

      registry.addRedirectViewController("/api-docs","/swagger-ui.html");

      }

      }

      禁用方法2:

      使用注解@Profile({“dev”,“test”}) 表示在開發或測試環境開啟,而在生產關閉。(推薦使用)

      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 zhaohp

      * @version V1.0

      * @Package com.dc.config

      * @date 2018/1/16 17:33

      * @Description: 主要用途:開啟在線接口文檔和添加相關配置

      */

      @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"))

      .paths(PathSelectors.any())

      //.paths(PathSelectors.none())

      .build();

      }

      private ApiInfo apiInfo() {

      return new ApiInfoBuilder()

      .title("auth系統數據接口文檔")

      .description("此系統為新架構Api說明文檔")

      .termsOfServiceUrl("")

      .contact(new Contact("趙化鵬 18310695431@163.com", "", "zhaohuapeng@zichan360.com"))

      .version("1.0")

      .build();

      }

      /**

      * swagger ui資源映射

      * @param registry

      */

      @Override

      public void addResourceHandlers(ResourceHandlerRegistry registry) {

      Spring Boot 禁用 Swagger 的三種方式

      registry.addResourceHandler("swagger-ui.html")

      .addResourceLocations("classpath:/META-INF/resources/");

      registry.addResourceHandler("/webjars/**")

      .addResourceLocations("classpath:/META-INF/resources/webjars/");

      }

      /**

      * swagger-ui.html路徑映射,瀏覽器中使用/api-docs訪問

      * @param registry

      */

      @Override

      public void addViewControllers(ViewControllerRegistry registry) {

      registry.addRedirectViewController("/api-docs","/swagger-ui.html");

      }

      }

      禁用方法3:

      使用注解@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) 然后在測試配置或者開發配置中 添加 swagger.enable = true 即可開啟,生產環境不填則默認關閉Swagger.

      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 zhaohp

      * @version V1.0

      * @Package com.dc.config

      * @date 2018/1/16 17:33

      * @Description: 主要用途:開啟在線接口文檔和添加相關配置

      */

      @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())

      .select()

      .apis(RequestHandlerSelectors.basePackage("com.dc.controller"))

      .paths(PathSelectors.any())

      //.paths(PathSelectors.none())

      .build();

      }

      private ApiInfo apiInfo() {

      return new ApiInfoBuilder()

      .title("auth系統數據接口文檔")

      .description("此系統為新架構Api說明文檔")

      .termsOfServiceUrl("")

      .contact(new Contact("趙化鵬 18310695431@163.com", "", "zhaohuapeng@zichan360.com"))

      .version("1.0")

      .build();

      }

      /**

      * swagger ui資源映射

      * @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路徑映射,瀏覽器中使用/api-docs訪問

      * @param registry

      */

      @Override

      public void addViewControllers(ViewControllerRegistry registry) {

      registry.addRedirectViewController("/api-docs","/swagger-ui.html");

      }

      }

      #Swagger lock

      swagger:

      enabled: true

      Spring Spring Boot

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:WPS文字怎么設置好?
      下一篇:數據和人力資源:范式正在發生
      相關文章
      亚洲国产精品无码专区在线观看 | 亚洲美女视频一区| 亚洲国产精品成人AV在线 | 亚洲自偷自拍另类图片二区| 亚洲国产精品无码专区| 亚洲精品无码mv在线观看网站 | 亚洲中文字幕无码日韩| 亚洲人成网站在线观看青青| 亚洲色欲久久久久综合网| 久久精品国产亚洲AV天海翼| 亚洲AV无码一区二区三区电影 | 亚洲精品综合久久中文字幕| 亚洲综合激情视频| 亚洲国产成+人+综合| 亚洲五月丁香综合视频| 午夜在线a亚洲v天堂网2019| 亚洲日韩一中文字暮| 精品久久亚洲一级α| 日韩精品成人亚洲专区| MM1313亚洲精品无码久久| 亚洲性色成人av天堂| 亚洲一级免费毛片| 亚洲一区二区三区写真| 中文字幕亚洲综合久久综合| 亚洲av成人无码网站…| 亚洲精品国产综合久久一线| 亚洲熟妇中文字幕五十中出| 久久亚洲精品国产精品黑人| 亚洲av高清在线观看一区二区| 亚洲av无码片vr一区二区三区| 亚洲AV无码一区二区三区在线观看| 中文字幕亚洲电影| 久久久亚洲精品国产| 亚洲精品成人网站在线播放| 亚洲成人激情小说| 国产成人亚洲综合a∨| 亚洲中文字幕无码不卡电影| 亚洲AV无码成人精品区蜜桃 | 亚洲精品国产高清嫩草影院| 亚洲女初尝黑人巨高清| 亚洲一区二区三区四区在线观看|