springboot?filter配置多個(gè)時(shí),執(zhí)行順序問題
springboot filter配置多個(gè)時(shí),執(zhí)行順序
在 spring boot 配置Filter過濾器 中簡(jiǎn)單介紹了spring boot 中如何添加過濾器,有人問到如果配置多個(gè)怎么控制,先經(jīng)過哪個(gè)過濾器,后經(jīng)過哪個(gè)過濾器。
在web.xml中,我們知道,執(zhí)行順序是誰(shuí)在前邊執(zhí)行誰(shuí)。
在spring boot中的FilterRegistrationBean注冊(cè)過濾器的類中有個(gè)order屬性,
private int order = Ordered.LOWEST_PRECEDENCE;
細(xì)看源碼可以知道,這個(gè)order的默認(rèn)值是Integer.MAX_VALUE 也就是int的最大值,
spring boot 會(huì)按照order值的大小,從小到大的順序來依次過濾。
spring boot 配置Filter過濾器 中可以這樣修改
/**
* 配置過濾器
* @return
*/
@Bean
public FilterRegistrationBean someFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(sessionFilter());
registration.addUrlPatterns("/*");
registration.addInitParameter("paramName", "paramValue");
registration.setName("sessionFilter");
registration.setOrder(Integer.MAX_VALUE);
return registration;
}再有一個(gè)過濾器的話,可以設(shè)置成
registration.setOrder(Integer.MAX_VALUE - 1)
springboot也提供了注解的方式
例如:
/**
* 配置過濾器
* @return
*/
@Bean
@Order(Integer.MAX_VALUE)
public FilterRegistrationBean someFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(sessionFilter());
registration.addUrlPatterns("/*");
registration.addInitParameter("paramName", "paramValue");
registration.setName("sessionFilter");
return registration;
}上面兩種方法都行,想用那種看你喜歡。。。。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java web中使用cookie記住用戶的賬號(hào)和密碼
這篇文章主要介紹了java web中使用cookie記住用戶的賬號(hào)和密碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
springtask 的使用方法和 cron 表達(dá)式解析
這篇文章主要介紹了springtask 的使用方法和 cron 表達(dá)式解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
RocketMQ線程池創(chuàng)建實(shí)現(xiàn)原理詳解
這篇文章主要為大家介紹了RocketMQ線程池創(chuàng)建實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢
這篇文章主要介紹了Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot Session共享實(shí)現(xiàn)圖解
這篇文章主要介紹了SpringBoot Session共享實(shí)現(xiàn)圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01

