02.09 Spring Boot 優雅的配置攔截器,實現權限管理,日誌記錄...

Spring Boot 優雅的配置攔截器,實現權限管理,日誌記錄...

其實spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了。下面主要介紹兩種常用的攔截器:

一、基於URL實現的攔截器:

Spring Boot 優雅的配置攔截器,實現權限管理,日誌記錄...

關鍵代碼:path.matches(Const.NO_INTERCEPTOR_PATH 就是基於正則匹配的url。

Spring Boot 優雅的配置攔截器,實現權限管理,日誌記錄...

二、基於註解的攔截器

①創建註解:

Spring Boot 優雅的配置攔截器,實現權限管理,日誌記錄...

②創建攔截器:

Spring Boot 優雅的配置攔截器,實現權限管理,日誌記錄...

三、把攔截器添加到配置中,相當於SpringMVC時的配置文件乾的事兒:

Spring Boot 優雅的配置攔截器,實現權限管理,日誌記錄...

1、一定要加@Configuration 這個註解,在啟動的時候在會被加載。

2、有一些教程是用的“WebMvcConfigurerAdapter”,不過在spring5.0版本後這個類被丟棄了 WebMvcConfigurerAdapter ,雖然還可以用,但是看起來不好。

3、也有一些教程使用的WebMvcConfigurationSupport,我使用後發現,classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/)不生效。具體可以原因,大家可以看下源碼因為:WebMvcAutoConfiguration上有個條件註解:

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

所以還是建議使用WebMvcConfigurer, 其實springMVC很多東西,都可以搬到springboot中來使用,只需要把配置文件的模式,改成 對應@Configuration 類就好了。

Spring Boot 優雅的配置攔截器,實現權限管理,日誌記錄...

原文地址:https://my.oschina.net/bianxin/blog/2876640


分享到:


相關文章: