解決errorpage里面取不到Authentication的問題
項目中遇到一個很奇怪的問題,在錯誤頁面404里面取不到當前登錄用戶,即 SecurityContextHolder.getContext().getAuthentication()取不到當前的登陸用戶信息。這個問題花了我很長時間最終搞定了,下面講一下解決問題的過程。
首先來看一下項目的異常處理方式,在web.xml里面配置了錯誤頁:
Xml代碼:
1.<error-page>
2. <error-code>404</error-code>
3. <location>/WEB-INF/pages/errors/404.jsp</location>
4. </error-page>
當訪問一個不存在的url時,spring的前端控制器的邏輯如下:
其實就是會調用noHandlerFound函數,然后直接退出DispatcherServlet。
我們再來一下noHandlerFound的邏輯:
在這個里面實際上是返回一個404的錯誤,真正的錯誤頁面處理的轉向是由tomcat容器來完成的。通過調試發現在這個地方SecurityContextHolder.getContext().getAuthentication()還有值,但是訪問404頁面的tag里面就取不到了,后來通過監控網絡發現,訪問errorpage是由容器重新發起的一個請求,這個請求里面拿不到Authentication可能是沒有走springsecurity的前端攔截器 springSecurityFilterChain。
我們來看一下springSecurityFilterChain filter的配置:
Xml代碼:
1.<filter>
2. <filter-name>springSecurityFilterChain</filter-name>
3. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
4. </filter>
5. <filter-mapping>
6. <filter-name>springSecurityFilterChain</filter-name>
7. <url-pattern>/*</url-pattern>
8. </filter-mapping>
突然想到極有可能是這個filter沒有轉發,后來看了下filter-mapping的配置,還真是這樣。filter-mapping里面接受dispatcher參數。
我們就來看一下這個參數的含義。
2.4版本的servlet規范在部屬描述符中新增加了一個<dispatcher>元素,這個元素有四個可能的值:即REQUEST,FORWARD,INCLUDE和ERROR,可以在一個<filter-mapping>元素中加入任意數目的<dispatcher>,使得filter將會作用于直接從客戶端過來的request,通過forward過來的request,通過include過來的request和通過<error-page>過來的request。如果沒有指定任何< dispatcher >元素,默認值是REQUEST。
可以通過下面幾個例子來輔助理解。
Xml代碼:
- <filter-mapping>
- <filter-name>Logging Filter</filter-name>
- <url-pattern>/products/*</url-pattern>
- </filter-mapping>
這種情況下,過濾器將會作用于直接從客戶端發過來的以/products/…開始的請求。因為這里沒有制定任何的< dispatcher >元素,默認值是REQUEST。
Xml代碼:
- <filter-mapping>
- <filter-name>Logging Filter</filter-name>
- <servlet-name>ProductServlet</servlet-name>
- <dispatcher>INCLUDE</dispatcher>
- </filter-mapping>
這種情況下,如果請求是通過request dispatcher的include方法傳遞過來的對ProductServlet的請求,則要經過這個過濾器的過濾。其它的諸如從客戶端直接過來的對ProductServlet的請求等都不需要經過這個過濾器。
指定filter的匹配方式有兩種方法:直接指定url-pattern和指定servlet,后者相當于把指定的servlet對應的url-pattern作為filter的匹配模式
filter的路徑匹配和servlet是一樣的,都遵循servlet規范中《SRV.11.2 Specification of Mappings》一節的說明
Xml代碼:
- <filter-mapping>
- <filter-name>Logging Filter</filter-name>
- <url-pattern>/products/*</url-pattern>
- <dispatcher>FORWARD</dispatcher>
- <dispatcher>REQUEST</dispatcher>
- </filter-mapping>
看了這個,我修改了下springSecurityFilterChain的filter-mapping的配置,就 好了。
修改后的配置如下:
Xml代碼:
- <filter>
- <filter-name>springSecurityFilterChain</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>springSecurityFilterChain</filter-name>
- <url-pattern>/*</url-pattern>
- <dispatcher>REQUEST</dispatcher>
- <dispatcher>ERROR</dispatcher>
- </filter-mapping>
意思就是直接從客戶端過來的request和通過<error-page>過來的request 都要走這個filter,配置完后就果斷好了。
好了,就寫到這里了,希望對大家有所幫助。關于springSecurityFilterChain這個我會另寫一篇博客進行詳細講解。
本人原創,發現一些網站無道德的抓取,請自覺刪去內容,轉載請注明出處:http://www.software8.co/wzjs/java/2797.html