亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot如何通過配置禁用swagger

 更新時間:2023年08月11日 09:11:58   作者:創(chuàng)客公元  
這篇文章主要給大家介紹了關(guān)于SpringBoot如何通過配置禁用swagger的相關(guān)資料,Swagger用來在開發(fā)階段方便前后端分離的項目實戰(zhàn)中,提高前后端人員的工作效率,降低交流成本,但是版本上線之后要是把Swagger帶上去會存在很大的風(fēng)險,需要的朋友可以參考下

一、序言

在生產(chǎn)環(huán)境下,我們需要關(guān)閉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: 主要用途:開啟在線接口文檔和添加相關(guān)配置
 */
@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系統(tǒng)數(shù)據(jù)接口文檔")
                .description("此系統(tǒng)為新架構(gòu)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”}) 表示在開發(fā)或測試環(huán)境開啟,而在生產(chǎn)關(guān)閉。(推薦使用)

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: 主要用途:開啟在線接口文檔和添加相關(guān)配置
 */
@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系統(tǒng)數(shù)據(jù)接口文檔")
                .description("此系統(tǒng)為新架構(gòu)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");
    }
}

禁用方法3:

使用注解@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) 然后在測試配置或者開發(fā)配置中 添加 swagger.enable = true 即可開啟,生產(chǎn)環(huán)境不填則默認(rèn)關(guān)閉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: 主要用途:開啟在線接口文檔和添加相關(guān)配置
 */
@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系統(tǒng)數(shù)據(jù)接口文檔")
                .description("此系統(tǒng)為新架構(gòu)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

總結(jié) 

到此這篇關(guān)于SpringBoot如何通過配置禁用swagger的文章就介紹到這了,更多相關(guān)SpringBoot禁用swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • scala中常用特殊符號詳解

    scala中常用特殊符號詳解

    這篇文章主要介紹了scala中常用特殊符號詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 從零開始使用IDEA創(chuàng)建SpringBoot項目(圖文)

    從零開始使用IDEA創(chuàng)建SpringBoot項目(圖文)

    這篇文章主要介紹了從零開始使用IDEA創(chuàng)建SpringBoot項目(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • MyBatis主鍵生成策略中useGeneratedKeys和<selectKey>的區(qū)別

    MyBatis主鍵生成策略中useGeneratedKeys和<selectKey>的區(qū)別

    本文主要介紹了MyBatis主鍵生成策略中useGeneratedKeys和<selectKey>的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • SpringBoot中自定義參數(shù)綁定步驟詳解

    SpringBoot中自定義參數(shù)綁定步驟詳解

    這篇文章主要介紹了SpringBoot中自定義參數(shù)綁定步驟詳解,非常不錯,具有參考借鑒價值 ,需要的朋友可以參考下
    2018-02-02
  • 盤點MQ中的異常測試

    盤點MQ中的異常測試

    這篇文章主要為大家介紹了盤點MQ中的異常測試,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Java實現(xiàn)簡單的萬年歷

    Java實現(xiàn)簡單的萬年歷

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)簡單的萬年歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring Boot/Cloud工程(圖解)

    使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring

    這篇文章主要介紹了使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring Boot/Cloud工程(圖解),需要的朋友可以參考下
    2018-01-01
  • Java+opencv3.2.0實現(xiàn)人臉檢測功能

    Java+opencv3.2.0實現(xiàn)人臉檢測功能

    這篇文章主要為大家詳細(xì)介紹了Java+opencv3.2.0實現(xiàn)人臉檢測功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • SpringBoot Shiro配置自定義密碼加密器代碼實例

    SpringBoot Shiro配置自定義密碼加密器代碼實例

    這篇文章主要介紹了SpringBoot Shiro配置自定義密碼加密器代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Springboot項目使用Slf4j將日志保存到本地目錄的實現(xiàn)代碼

    Springboot項目使用Slf4j將日志保存到本地目錄的實現(xiàn)代碼

    這篇文章主要介紹了Springboot項目使用Slf4j將日志保存到本地目錄的實現(xiàn)方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05

最新評論