生产环境下springboot中怎么配置禁用swagger-成都快上网建站

生产环境下springboot中怎么配置禁用swagger

这篇文章主要讲解了“生产环境下springboot中怎么配置禁用swagger”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“生产环境下springboot中怎么配置禁用swagger”吧!

目前成都创新互联已为1000+的企业提供了网站建设、域名、网页空间、网站托管、服务器托管、企业网站设计、邹平网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

一、序言
在生产环境下,我们需要关闭swagger配置,避免暴露接口的这种危险行为。

二、方法:
禁用方法1:使用注解@Profile({"dev","test"}) 表示在开发或测试环境开启,而在生产关闭。(推荐使用)

禁用方法2:使用注解@ConditionalOnProperty(name = "swagger.enable", havingValue = "true") 然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不填则默认关闭Swagger.

如方法1:

package com.left;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
/**
 *


 *     @author : orange
 *     e-mail : 495314527@qq.com
 *     time   : 2018/8/27 14:49
 *     desc   : swagger配置
 *     version: 1.0
 *

 */
@Configuration
@EnableSwagger2
@Profile({"dev","test"})
public class Swagger2 {
 
    @Bean
    public Docket restApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.left.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("springboot整合swagger2")
                .description("springboot整合swagger2")
                .termsOfServiceUrl("https://blog.csdn.net/weixin_37591536")
                .version("1.0")
                .build();
    }
 
}
结果:

开发环境可以正常访问

感谢各位的阅读,以上就是“生产环境下springboot中怎么配置禁用swagger”的内容了,经过本文的学习后,相信大家对生产环境下springboot中怎么配置禁用swagger这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


新闻标题:生产环境下springboot中怎么配置禁用swagger
URL标题:http://kswjz.com/article/ijoped.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流