温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-07-06 10:42:20 来源:亿速云 阅读:1738 作者:chen 栏目:大数据

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

一、序言
在生产环境下,我们需要关闭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;
 
/**
 * <pre>
 *     @author : orange
 *     e-mail : 495314527@qq.com
 *     time   : 2018/8/27 14:49
 *     desc   : swagger配置
 *     version: 1.0
 * </pre>
 */
@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这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI