Spring Boot環境下自定義shiro過濾器會過濾全部的url的問題

在配置shiro過濾器時增長了自定義的過濾器,主要是用來處理未登陸狀態下返回一些信息html

//自定義過濾器
Map<String, Filter> filtersMap = new LinkedHashMap<String, Filter>();
filtersMap.put("loginFilter", loginFilter);
shiroFilter.setFilters(filtersMap);

可是發現添加該過濾之後全部的url都會通過loginFilter,調試代碼發現除了在shiroFilter中添加了loginFilter外,還會被Spring Boot註冊到容器的filter chain中,默認過濾路徑爲/*,因此會過濾全部的url,要解決這個問題就是要取消Spring Boot自動註冊到filter chain容器中。web

下面是調試查看的容器過濾器列表,自定義loginFilter已經註冊到容器的過濾器中了,固然shiro中也會有
[ApplicationFilterConfig[name=characterEncodingFilter, filterClass=org.springframework.boot.web.filter.OrderedCharacterEncodingFilter], ApplicationFilterConfig[name=hiddenHttpMethodFilter, filterClass=org.springframework.boot.web.filter.OrderedHiddenHttpMethodFilter], ApplicationFilterConfig[name=httpPutFormContentFilter, filterClass=org.springframework.boot.web.filter.OrderedHttpPutFormContentFilter], ApplicationFilterConfig[name=requestContextFilter, filterClass=org.springframework.boot.web.filter.OrderedRequestContextFilter],
ApplicationFilterConfig[name=shiroFilter, filterClass=org.apache.shiro.spring.web.ShiroFilterFactoryBean$SpringShiroFilter],
ApplicationFilterConfig[name=loginFilter2, filterClass=org.shaofan.shiro.LoginFilter2],
ApplicationFilterConfig[name=Tomcat WebSocket (JSR356) Filter, filterClass=org.apache.tomcat.websocket.server.WsFilter], null, null, null]

解決方法:
經過使用FilterRegistrationBean來進行Filter的註冊,同時,設置enabled爲false,就能夠取消對應的Filter自動註冊了,其實就是先手動註冊再設置禁用。spring

@Bean
public FilterRegistrationBean loginFilter2Registration(LoginFilter loginFilter) {
FilterRegistrationBean registration = new FilterRegistrationBean(loginFilter);
registration.setEnabled(false);
return registration;
}

 

下面是啓動的日誌,也顯示該過濾器沒有註冊
apache

Spring Boot 文檔中也有說明:tomcat

Spring Boot Document 

參考地址:https://www.jianshu.com/p/bf79fdab9c19websocket

相關文章
相關標籤/搜索