Spring Security使用一組過濾器鏈來對用戶進行身份驗證和授權。首先,在web.xml文件中添加FilterToBeanProxy過濾器配置:
2 <filter-name>springSecurityFilterChain</filter-name>
3 <filter-class>
4 org.springframework.security.util.FilterToBeanProxy
5 </filter-class>
6 <init-param>
7 <param-name>targetClass</param-name>
8 <param-value>
9 org.springframework.security.util.FilterChainProxy
10 </param-value>
11 </init-param>
12 </filter>
13
當用戶發出請求,過濾器需要根據web.xml配置的請求映射地址來攔截用戶請求,這時Spring Security開始工作,它會驗證你的身份以及當前請求的資源是否與你擁有的權限相符,從而達到保護Web資源的功能,下面是本例所要過濾的用戶請求地址:
2
3 <filter-name>springSecurityFilterChain</filter-name>
5 <url-pattern>/j_spring_security_check</url-pattern>
6
7 </filter-mapping>
8
9 <filter-mapping>
10
11 <filter-name>springSecurityFilterChain</filter-name>
12
13 <url-pattern>/*</url-pattern>
14
15 </filter-mapping>
提示:
/j_spring_security_check是Spring Security默認的進行表單驗證的過濾地址,你也可以修改為別的名稱,但是需要和
applicationContext-security.xml中相對應,當然還會涉及到其它一些默認值(可能是一個成員變量,也可能是別的請
求地址),在下文我們將看到,建議你在閱讀此文的同時,應該參照Spring Security項目的源代碼,便于你更好的理解。
3 配置applicationContext-security.xml
3.1 FilterChainProxy過濾器鏈
FilterChainProxy會按順序來調用一組filter,使這些filter即能完成驗證授權的本質工作,又能享用Spring Ioc的功能來方便的得到其它依賴的資源。FilterChainProxy配置如下:
class="org.springframework.security.util.FilterChainProxy">
2 <property name="filterInvocationDefinitionSource">
3 <value><![CDATA[
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
4 PATTERN_TYPE_APACHE_ANT
/**=httpSessionContextIntegrationFilter,logoutFilter,
5 authenticationProcessingFilter,securityContextHolderAwareRequestFilter,
6 rememberMeProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,
7 filterSecurityInterceptor
8 ]]></value>
9 </property>
10 </bean>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON 定義URL在匹配之前必須先轉為小寫,PATTERN_TYPE_APACHE_ANT 定義了使用Apache ant的匹配模式,/**定義的將等號后面的過濾器應用在那些URL上,這里使用全部URL過濾,每個過濾器之間都適用逗號分隔,它們按照一定的順序排列。
提示:
特別需要注意的是,即使你配置了系統提供的所有過濾器,這個過濾器鏈會很長,但是千萬不要使用換行,否則它們不會正常工作,
容器甚至不能正常啟動。
下面根據FilterChainProxy的配置來介紹各個過濾器的配置,各個過濾器的執行順序如以上配置。
首先是通道處理過濾器,如果你需要使用HTTPS,這里我們就使用HTTP進行傳輸,所以不需要配置通道處理過濾器,然后是集成過濾器,配置如下:
2
3 class="org.springframework.security.context.HttpSessionContextIntegrationFilter"/>
httpSessionContextIntegrationFilter是集成過濾器的一個實現,在用戶的一個請求過程中,用戶的認證信息通過SecurityContextHolder(使用ThreadLoacl實現)進行傳遞的,所有的過濾器都是通過SecurityContextHolder來獲取用戶的認證信息,從而在一次請求中所有過濾器都能共享Authentication(認證),減少了HttpRequest參數的傳送,下面的代碼是從安全上下文的獲取Authentication對象的方法:
2
3 Authentication authentication = context.getAuthentication();
但是,ThreadLoacl不能跨越多個請求存在,所以,集成過濾器在請求開始時從Http會話中取出用戶認證信息并創建一個SecurityContextHolder將Authentication對象保存在其中,在請求結束之后,在從SecurityContextHolder中獲取Authentication對象并將其放回Http會話中,共下次請求使用,從而達到了跨越多個請求的目的。集成過濾器還有其它的實現,可以參考相關文檔。
提示: 集成過濾器必須在其它過濾器之前被使用。 |
logoutFilter(退出過濾器) ,退出登錄操作:
2
3 class="org.springframework.security.ui.logout.LogoutFilter">
4
5 <constructor-arg value="/index.jsp"/>
6
7 <constructor-arg>
8
9 <list>
10
11 <!-- 實現了LogoutHandler接口(logout方法) -->
12
13 <ref bean="rememberMeServices"/>
14
15 <bean class="org.springframework.security.ui.logout.SecurityContextLogoutHandler"/>
16
17 </list>
18
19 </constructor-arg>
20
21 </bean>