欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解SpringBoot禁用Swagger的三種方式

 更新時(shí)間:2021年11月22日 12:01:41   作者:Sunny_Chen  
在生產(chǎn)環(huán)境下,我們需要關(guān)閉swagger配置,避免暴露接口的這種危險(xiǎn)行為。本文就詳細(xì)的介紹了3種情況,感興趣的可以了解一下

摘要

在生產(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 sunny chen
 * @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("陳永佳 chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens"))
                .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ā)或測(cè)試環(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 sunny chen
 * @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("陳永佳 chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens"))
                .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”)? 然后在測(cè)試配置或者開發(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 sunny chen
 * @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("陳永佳 chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens"))
                .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");
    }
}

到此這篇關(guān)于詳解SpringBoot禁用Swagger的三種方式的文章就介紹到這了,更多相關(guān)SpringBoot禁用Swagger內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis的Dao層實(shí)現(xiàn)原理分析

    Mybatis的Dao層實(shí)現(xiàn)原理分析

    這篇文章主要介紹了Mybatis的Dao層實(shí)現(xiàn)原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java中的相除(/)和取余(%)的實(shí)現(xiàn)方法

    Java中的相除(/)和取余(%)的實(shí)現(xiàn)方法

    這篇文章主要介紹了Java中的相除(/)和取余(%)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Spring Boot 中的 @Field 注解的原理解析

    Spring Boot 中的 @Field 注解的原理解析

    本文詳細(xì)介紹了 Spring Boot 中的 @Field 注解的原理和使用方法,通過使用 @Field 注解,我們可以將 HTTP 請(qǐng)求中的參數(shù)值自動(dòng)綁定到 Java 對(duì)象的屬性上,簡(jiǎn)化了開發(fā)過程,提高了開發(fā)效率,感興趣的朋友跟隨小編一起看看吧
    2023-07-07
  • 淺談java泛型的作用及其基本概念

    淺談java泛型的作用及其基本概念

    下面小編就為大家?guī)硪黄獪\談java泛型的作用及其基本概念。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-08-08
  • Spring Boot與前端配合與Idea配置部署操作過程

    Spring Boot與前端配合與Idea配置部署操作過程

    這篇文章主要介紹了Spring Boot與前端配合與Idea配置部署的操作過程,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2018-02-02
  • MyBatis屬性名和字段名不一致的問題解決方法

    MyBatis屬性名和字段名不一致的問題解決方法

    這篇文章給大家詳細(xì)介紹了MyBatis屬性名和字段名不一致的問題解決,文中有詳細(xì)的代碼示例和圖文展示供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下
    2023-12-12
  • Java NIO寫大文件對(duì)比(win7和mac)

    Java NIO寫大文件對(duì)比(win7和mac)

    這篇文章主要介紹了Java NIO寫大文件對(duì)比(win7和mac),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 如何使用Jenkins構(gòu)建GIT+Maven項(xiàng)目

    如何使用Jenkins構(gòu)建GIT+Maven項(xiàng)目

    這篇文章主要介紹了如何使用Jenkins構(gòu)建GIT+Maven項(xiàng)目,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java中的final關(guān)鍵字使用方式

    Java中的final關(guān)鍵字使用方式

    這篇文章主要介紹了Java中的final關(guān)鍵字使用方式,final 關(guān)鍵字用于修飾不可改變內(nèi)容,更多相關(guān)梳理總結(jié),需要的小伙伴可以參考下面文章內(nèi)容
    2022-06-06
  • Spring執(zhí)行流程和Bean的生命周期詳解

    Spring執(zhí)行流程和Bean的生命周期詳解

    這篇文章主要介紹了Spring執(zhí)行流程和Bean的生命周期詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06

最新評(píng)論