spring security 核心过滤器
2019獨角獸企業重金招聘Python工程師標準>>>
#14. Core Security Filter 有很多關鍵過濾器在spring 安全的項目中總是使用,所以我們可以查看這些支持的類和接口.想要全面了解,請查看javadoc.
##14.1 FilterSecurityInterceptor
一個典型的配置如下
<bean id="filterSecurityInterceptor" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor"> <property name="authenticationManager" ref="authenticationManager"/> <property name="accessDecisionManager" ref="accessDecisionManager" /> <property name="securityMetadataSource"><security:filter-security-metadata-source><security:intercept-url pattern="/secure/super/**" access="ROLE_WE_DONT_HAVE"/><security:intercept-url pattern="/secure/**" access="ROLE_SUPERVISOR,ROLE_TELLER"> </property>FilterSecurityInterceptor主要保護http資源的安全.它要求一個AuthenticationManager和一個AccessDecisionManager的引用.它也提供了一些配置屬性來適應不同的http請求.
FilterSecurityInterceptor可以通過兩種方式來配置屬性.第一個,如上所述,使用<filter-security-metadata-source>命名空間.它與<http>空間很像,但它的子元素只有pattern,access元素.逗號用來劃分不同http url的配置元素.第二選項是寫自己的SecurityMetadataSource,但是這個超出了文本文件的內容.無論何種方法,SecurityMetadataSource用來返回關于單個http url的List<ConfigAttribute>.
filter-security-metadata-source的匹配表達式可以通過request-matcher來設置.默認是ant匹配.
<bean id="filterInvocationInterceptor"class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor"> <property name="authenticationManager" ref="authenticationManager"/> <property name="accessDecisionManager" ref="accessDecisionManager"/> <property name="runAsManager" ref="runAsManager"/> <property name="securityMetadataSource"><security:filter-security-metadata-source request-matcher="regex"><security:intercept-url pattern="\A/secure/super/.*\Z" access="ROLE_WE_DONT_HAVE"/><security:intercept-url pattern="\A/secure/.*\" access="ROLE_SUPERVISOR,ROLE_TELLER"/></security:filter-security-metadata-source> </property> </bean>模式按定義的順序執行.重要的是指定一些具體模式的順序在一些模糊模式之前.在我們的例子中,具體路徑"/secure/super"的模式在"/secure/"模式之前.如果反轉順序,那么/secure/模式總會匹配,而/secure/super模式不會執行.
14.2 ExceptionTranslationFilter
ExceptionTranslationFilter是基于FilterSecurityInterceptor的棧中的.它本身不參與安全攔截,負責處理安全攔截器拋出的異常,并提供合適的http請求.
<bean id="exceptionTranslationFilter" class="org.springframework.security.web.access.ExceptionTranslationFilter"><property name="authenticationEntryPoint" ref="authenticationEntryPoint"/> <property name="accessDeniedHandler" ref="accessDeniedHandler"/> <bean/> <bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> <property name="loginFormUrl" value="/login.jsp"/> </bean> <bean id="accessDeniedHandler"class="org.springframework.security.web.access.AccessDeniedHandlerImpl"> <property name="errorPage" value="/accessDenied.htm"/>###14.2.1 AuthenticationEntryPoint
當你訪問一個收保護的資源而沒有認證時,AuthenticationEntryPoint就會被調用.此時你希望調用的是LoginUrlAuthenticationEntryPoint,它會重定向到不同的url中.
###14.2.2 AccessDeniedHandler
有時已認證的用戶會訪問被保護的資源會失敗.例如,一個user的用戶訪問admin的資源,有時是直接用html連接,有時是restful的參數問題.通常需要在web層驗證,或在服務層的接口固定調用許可.
如果一個AccessDeniedException被拋出,如果用戶已被認證,那么ExceptiontranslationFilter會啟用第二種方案,AccessDeniedHandler.一般它會返回403的狀態碼,另外你可以指定一個錯誤頁面.它可以是簡單的"access denied"頁面,也可以是jsp,還可以實現自己的接口.
你也可以實現一個自定義的AccessDeniedHandler.
###14.2.3 SavedRequest and the RequestCache Interface 異常轉化器的另一個職責是在調用AuthenticationEntryPoint之前保存當前請求.當用戶被認證之后,它允許請求重置.典型的例子,如表單登陸.一般它可以通過SavedRequestAwareAuthenticationSuccessHandler進行重定向.
RequestCache的一個作用就是存儲和恢復HttpServletRequest實例.當HttpSessionRequestCache被使用時,它會在httpSession中存儲請求.RequestCacheFilter的工作就是從緩存中取出已保存的請求,并將它們重定向到原來的url中.
##14.3 SecurityContextPersistenceFilter 基本配置如下:
<bean id="securityContextPersistenceFilter" class="org.springframework.security.web.context.SecurityContextPersistenceFilter"/>它的職責是存儲http請求之間的securityContext內容,并在請求完成之后清空SecurityContextHolder.如果不清空ThreadLocal,當它被容器線程池回收時,關于用戶的上下文信息就會一直儲存.這個線程可能會在以后的階段使用錯誤的認證信息繼續執行.
14.3.1 SecurityContextRepository
spring security 3.0,負載和存儲security上下文是由一個單獨的策略接口完成的:
public interface SecurityContextRepository{SecurityContext loadContext(HttpRequestResponseHolder requestResponseHodler);void saveContext(SecurityContext context,HttpServletRequest,request,HttpServletResponse response); }HttpRequestResponseHolder,包含了到來的請求和返回信息,并替換他們.它返回的內容會通過攔截鏈.
其默認實現是HttpSessionSecurityContextRepository,它將security上下文作為httpSesion的一個屬性來存儲.重要屬性,allowSessionCreation,默認值是true.它允許類在需要存儲認證用戶上下文時來創建一個session.(只有當認證發生,且上下文內容改變才創建).如果你不想創建session,你需要將屬性設為false.
<bean id="securityContextPersistenceFilter"class="org.springframework.security.web.context.SecurityContextPersistenceFilter"> <property name='securityContextRepository'><bean class='org.springframework.security.web.context.HttpSessionSecurityContextRepository'><property name='allowSessionCreation' value='false' /></bean> </property> </bean>另外,你也可使用一個NullSecurityContextRepository的實例,一個空的對象實現.這樣會阻止安全上下文存儲.即使session已經存在. ##14.4 UsernamePasswordAuthenticationFilter form-login元素的配置,有三個方面需要配置
-
在loginUrlAuthenticationEntryPoint配置登陸頁的url.就像之前一樣,并在ExceptionTranslationFilter中設置
-
實現登陸頁(使用 jsp或mvc控制器)
-
在應用上下文中配置UsernamePasswordAuthenticationFilter的實例
-
在你的攔截器代理中添加攔截器bean(認真排序)
登陸表單包含username和password兩個字段,默認路徑是URl.基本的攔截器配置如下:
<bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> <property name="authenticationManager" ref="authenticationManager"/> </bean>###14.4.1 認證成功或失敗的流向
成功或失敗都是有AuthenticationSuccessHandler或AuthenticationFailureHandler策略接口分別決定的.現在有SimpleUrlAuthenticationSuccessHandler,SaveRequestAwareAuthenticationSuccessHandler,SimpleUrlAuthenticationFailureHandler,ExceptionMappingAuthenticationFailureHandler and DelegatingAuthenticationFailureHandler.可以查看文檔了解更多.
如果認證成功,則AuthenticationSuccessHandler會調用,并重定向到合適的目的地.默認是使用SavedRequestAwareAuthenticationSuccessHandler,登陸成功后會重定向到原始目的地.
轉載于:https://my.oschina.net/u/1590027/blog/912736
總結
以上是生活随笔為你收集整理的spring security 核心过滤器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 慢动作输出 Linux 命令结果并用彩色
- 下一篇: 一个QQ旋风的BUG