在配置完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