关于SpringBoot整合Swagger-ui后,在配置文件中配置禁用Swagger失效的解决方法

在配置完Swagger测试完成后想到一个问题,swagger用来在开发阶段方便前后端开发。降低交流成本。但是版本上线之后,要是吧swagger带上去就危险了!

所以我想在生产环境中关闭Swagger,百度查询得知将swagger配置中的enable改为false,改正过来后进行测试;

在application-prod.yml中配置关闭Swagger:

server:
port: 8083

#是否开启 swagger-ui
swagger:
enable: false

spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
type-aliases-package: com.example.**.entity
mapper-locations: classpath:mapper/*.xml

在浏览器输入http://localhost:8083/swagger-ui.html,发现配置的不开启Swagger没有生效

后经过查阅资料发现了在swagger的Java配置类中缺少了配置swagger是否开启的注解,加上如下代码:

继续在浏览器中输入http://localhost:8083/swagger-ui.html,进行测试后结果如下:

发现无法访问Swagger了,说明了配置生效了,问题得到了解决。。。

对于注解@ConditionalOnProperty的使用方式:

@ConditionalOnProperty注解的意思是通过属性值来控制configuration是否生效

下面我们通过一段代码来展示下该注解:

@Configuration
public class WebConfig {
 
@Bean
@ConditionalOnProperty(prefix = "rest", name = "auth-open", havingValue = "true", matchIfMissing = true)
public AuthFilter jwtAuthenticationTokenFilter() {
return new AuthFilter();
}
 
}

  

prefix: application.properties配置的前缀
name: 属性是从application.properties配置文件中读取属性值
havingValue: 配置读取的属性值跟havingValue做比较,如果一样则返回true;否则返回false。
如果返回值为false,则该configuration不生效;为true则生效
matchIfMissing = true:表示如果没有在application.properties设置该属性,则默认为条件符合

上面代码的意思是
是否启动jwt的的配置,如果application.properties配置中没有设置就启动jwt,如果设置了true就启动,如果false就关闭
application.properties 配置如下

rest:
auth-open: true #jwt鉴权机制是否开启(true或者false)

 以上例子转自:https://www.cnblogs.com/xikui/p/11226400.html

原文地址:https://www.cnblogs.com/zhukf/p/12699414.html