你还在生产节点开放swagger吗,赶紧停止这种暴露接口的行为吧。
学习目标
快速学会使用注解关闭swagger2,避免接口重复暴露。
使用教程
禁用方法1:使用注解 @profile({"dev","test"}) 表示在开发或测试环境开启,而在生产关闭。(推荐使用)
禁用方法2:使用注解 @conditionalonproperty(name = "swagger.enable", havingvalue = "true") 然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不填则默认关闭swagger.
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/** * swagger2 接口配置 */
@configuration @enableswagger2 //@profile({"dev","test"}) @conditionalonproperty (name = "swagger.enable" , havingvalue = "true" ) public class swagger2config { /** * 添加摘要信息(docket) */ @bean public docket controllerapi() { return new docket(documentationtype.swagger_2) .apiinfo( new apiinfobuilder() .title( "标题:某公司_用户信息管理系统_接口文档" ) .description( "描述:用于管理集团旗下公司的人员信息,具体包括xxx,xxx模块..." ) .contact( new contact( "socks" , null , null )) .version( "版本号:1.0" ) .build()) .select() .apis(requesthandlerselectors.basepackage( "com.hehe.controller" )) .paths(pathselectors.any()) .build(); } } |
访问效果:
开发环境:http://localhost:8081/swagger-ui.html 正常访问swagger。
生产环境:http://localhost:8082/swagger-ui.html 已经禁用swagger。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://HdhCmsTestjianshu测试数据/p/34c5180a5134
查看更多关于SpringBoot在生产快速禁用Swagger2的方法步骤的详细内容...