swagger2关闭配置(swagger怎么配置)

swagger2关闭配置(swagger怎么配置)p 一 序言 br 在生产环境下 我们需要关闭 swagger 配置 避免暴露接口的这种危险行为 p p 二 方法 br 禁用方法 1 使用注解 Profile dev test 表示在开发或测试环境开启 而在生产关闭 p

大家好,我是讯享网,很高兴认识大家。




讯享网

 <p>一、序言<br/> 在生产环境下,我们需要关闭swagger配置,避免暴露接口的这种危险行为。</p><p>二、方法:<br/> 禁用方法1:使用注解@Profile({&quot;dev&quot;,&quot;test&quot;}) 表示在开发或测试环境开启,而在生产关闭。(推荐使用)</p><p>禁用方法2:使用注解@ConditionalOnProperty(name = &quot;swagger.enable&quot;, havingValue = &quot;true&quot;) 然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不填则默认关闭Swagger.</p><p>如方法1:</p><p>package com.left;<br/> &nbsp;<br/> import org.springframework.context.annotation.Bean;<br/> import org.springframework.context.annotation.Configuration;<br/> import springfox.documentation.builders.ApiInfoBuilder;<br/> import springfox.documentation.builders.PathSelectors;<br/> import springfox.documentation.builders.RequestHandlerSelectors;<br/> import springfox.documentation.service.ApiInfo;<br/> import springfox.documentation.spi.DocumentationType;<br/> import springfox.documentation.spi.service.contexts.SecurityContext;<br/> import springfox.documentation.spring.web.plugins.Docket;<br/> import springfox.documentation.swagger2.annotations.EnableSwagger2;<br/> &nbsp;<br/> /<br/> &nbsp;* &lt;pre&gt;<br/> &nbsp;* &nbsp; &nbsp; @author : orange<br/> &nbsp;* &nbsp; &nbsp; e-mail : @.com<br/> &nbsp;* &nbsp; &nbsp; time &nbsp; : 2018/8/27 14:49<br/> &nbsp;* &nbsp; &nbsp; desc &nbsp; : swagger配置<br/> &nbsp;* &nbsp; &nbsp; version: 1.0<br/> &nbsp;* &lt;/pre&gt;<br/> &nbsp;*/<br/> @Configuration<br/> @EnableSwagger2<br/> @Profile({&quot;dev&quot;,&quot;test&quot;})<br/> public class Swagger2 {<br/> &nbsp;<br/> &nbsp; &nbsp; @Bean<br/> &nbsp; &nbsp; public Docket restApi(){<br/> &nbsp; &nbsp; &nbsp; &nbsp; return new Docket(DocumentationType.SWAGGER_2)<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .select()<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .apis(RequestHandlerSelectors.basePackage(&quot;com.left.controller&quot;))<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .paths(PathSelectors.any())<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build()<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .apiInfo(apiInfo());<br/> &nbsp; &nbsp; }<br/> &nbsp; &nbsp; private ApiInfo apiInfo() {<br/> &nbsp; &nbsp; &nbsp; &nbsp; return new ApiInfoBuilder().title(&quot;springboot整合swagger2&quot;)<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .description(&quot;springboot整合swagger2&quot;)<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .termsOfServiceUrl(&quot;https://blog.csdn.net/weixin_&quot;)<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .version(&quot;1.0&quot;)<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();<br/> &nbsp; &nbsp; }<br/> &nbsp;<br/> }<br/> 结果:</p><p>开发环境可以正常访问</p> 

讯享网
小讯
上一篇 2025-05-09 11:01
下一篇 2025-05-10 16:54

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/137377.html