springboot?filter配置多個時,執(zhí)行順序問題
springboot filter配置多個時,執(zhí)行順序
在 spring boot 配置Filter過濾器 中簡單介紹了spring boot 中如何添加過濾器,有人問到如果配置多個怎么控制,先經(jīng)過哪個過濾器,后經(jīng)過哪個過濾器。
在web.xml中,我們知道,執(zhí)行順序是誰在前邊執(zhí)行誰。
在spring boot中的FilterRegistrationBean注冊過濾器的類中有個order屬性,
private int order = Ordered.LOWEST_PRECEDENCE;
細(xì)看源碼可以知道,這個order的默認(rèn)值是Integer.MAX_VALUE 也就是int的最大值,
spring boot 會按照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; }
再有一個過濾器的話,可以設(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é)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springtask 的使用方法和 cron 表達(dá)式解析
這篇文章主要介紹了springtask 的使用方法和 cron 表達(dá)式解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10RocketMQ線程池創(chuàng)建實(shí)現(xiàn)原理詳解
這篇文章主要為大家介紹了RocketMQ線程池創(chuàng)建實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢
這篇文章主要介紹了Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11SpringBoot Session共享實(shí)現(xiàn)圖解
這篇文章主要介紹了SpringBoot Session共享實(shí)現(xiàn)圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01