最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot如何通過配置禁用swagger

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

一、序言

在生產(chǎn)環(huán)境下,我們需要關(guān)閉swagger配置,避免暴露接口的這種危險(xiǎn)行為。

二、方法:

禁用方法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)文章

  • Java Map 按照Value排序的實(shí)現(xiàn)方法

    Java Map 按照Value排序的實(shí)現(xiàn)方法

    Map是鍵值對的集合接口,它的實(shí)現(xiàn)類主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。這篇文章主要介紹了Java Map 按照Value排序的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2016-08-08
  • 如何解決Maven出現(xiàn)Could not find artifact的問題

    如何解決Maven出現(xiàn)Could not find artifact的問題

    這篇文章主要介紹了如何解決Maven出現(xiàn)Could not find artifact的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Java Resource路徑整理總結(jié)

    Java Resource路徑整理總結(jié)

    這篇文章主要介紹了 Java Resource路徑整理總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java排序算法之冒泡排序的原理及優(yōu)化

    Java排序算法之冒泡排序的原理及優(yōu)化

    這篇文章主要介紹了Java排序算法之冒泡排序的原理及優(yōu)化,冒泡排序的思想很簡單,遍歷數(shù)組,比較相鄰的兩個(gè)元素,順序錯(cuò)誤就把它們交換,直到整個(gè)數(shù)組排序完成,因?yàn)槊拷?jīng)過一趟排序,越小的元素會(huì)經(jīng)交換而慢慢“浮”到數(shù)列的頂端,因此叫做冒泡排序,需要的朋友可以參考下
    2023-11-11
  • Spring和MyBatis整合自動(dòng)生成代碼里面text類型遇到的坑

    Spring和MyBatis整合自動(dòng)生成代碼里面text類型遇到的坑

    Spring和MyBatis整合以后,使用自動(dòng)生成代碼工具生成dao和mapper配置文件。下面通過本文給大家介紹Spring和MyBatis整合自動(dòng)生成代碼里面text類型遇到的坑,需要的朋友參考下吧
    2018-01-01
  • Java基礎(chǔ)教程之類型轉(zhuǎn)換與多態(tài)

    Java基礎(chǔ)教程之類型轉(zhuǎn)換與多態(tài)

    這篇文章主要介紹了Java基礎(chǔ)教程之類型轉(zhuǎn)換與多態(tài),本文講解了 基本類型轉(zhuǎn)換、 upcast與多態(tài)、 Object類等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • java線性表排序示例分享

    java線性表排序示例分享

    這篇文章主要介紹了java線性表排序示例,需要的朋友可以參考下
    2014-03-03
  • SpringBoot后端驗(yàn)證碼的實(shí)現(xiàn)示例

    SpringBoot后端驗(yàn)證碼的實(shí)現(xiàn)示例

    為了防止網(wǎng)站的用戶被通過密碼典爆破,引入驗(yàn)證碼的功能是十分有必要的,本文主要介紹了SpringBoot后端驗(yàn)證碼的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • SpringBoot源碼閱讀之spring.factories的加載機(jī)制詳解

    SpringBoot源碼閱讀之spring.factories的加載機(jī)制詳解

    Spring Boot通過`spring.factories`文件實(shí)現(xiàn)自動(dòng)裝配,該文件位于`META-INF`目錄下,Spring Boot在啟動(dòng)時(shí)會(huì)讀取該文件并實(shí)例化其中配置的實(shí)現(xiàn)類
    2025-11-11
  • Java實(shí)戰(zhàn)之飛翔的小鳥小游戲

    Java實(shí)戰(zhàn)之飛翔的小鳥小游戲

    這篇文章主要介紹了Java實(shí)戰(zhàn)之飛翔的小鳥小游戲,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04

最新評論

自治县| 肥城市| 曲麻莱县| 丰台区| 贵州省| 安溪县| 周口市| 龙州县| 常州市| 汉沽区| 惠东县| 东乡县| 卓资县| 龙南县| 水城县| 乐安县| 昌吉市| 平舆县| 沐川县| 甘谷县| 合山市| 辽宁省| 慈溪市| 易门县| 连云港市| 东兴市| 阿拉善左旗| 萨迦县| 聂拉木县| 延津县| 社会| 罗甸县| 望都县| 蓬安县| 普兰县| 镇巴县| 郯城县| 兴海县| 读书| 济源市| 砚山县|