posts - 10, comments - 9, trackbacks - 0, articles - 17

          Spring Security

          Posted on 2009-04-21 16:11 wesley1987 閱讀(618) 評論(0)  編輯  收藏
          選自 http://java.chinaitlab.com/Spring/762022.html

           Spring Security使用一組過濾器鏈來對用戶進行身份驗證和授權。首先,在web.xml文件中添加FilterToBeanProxy過濾器配置:
           1 <filter>   
           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
                  org.springframework.security.util.FilterToBeanProxy實現了Filter接口,它通過調用WebapplicationContextUtils類的getWebApplicationnContext(servletContext)方法來獲取Spring的應用上下文句柄,并通過getBean(beanName)方法來獲取Spring受管Bean的對象,即這里targetClass參數配置的Bean,并通過調用FilterChain Proxy的init()方法來啟動Spring Security過濾器鏈進行各種身份驗證和授權服務(FilterChainProxy類也是實現了Filter接口),從而將過濾功能委托給Spring的FilterChainProxy受管Bean(它維護著一個處理驗證和授權的過濾器列表,列表中的過濾器按照一定的順序執行并完成認證過程),這樣即簡化了web.xml文件的配置,又能充分利用 Spring的IoC功能來完成這些過濾器執行所需要的其它資源的注入。

                  當用戶發出請求,過濾器需要根據web.xml配置的請求映射地址來攔截用戶請求,這時Spring Security開始工作,它會驗證你的身份以及當前請求的資源是否與你擁有的權限相符,從而達到保護Web資源的功能,下面是本例所要過濾的用戶請求地址:
           1 <filter-mapping>
           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配置如下:

           1 <bean id="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進行傳輸,所以不需要配置通道處理過濾器,然后是集成過濾器,配置如下:

          1 <bean id="httpSessionContextIntegrationFilter"
          2 
          3 class="org.springframework.security.context.HttpSessionContextIntegrationFilter"/>

              httpSessionContextIntegrationFilter是集成過濾器的一個實現,在用戶的一個請求過程中,用戶的認證信息通過SecurityContextHolder(使用ThreadLoacl實現)進行傳遞的,所有的過濾器都是通過SecurityContextHolder來獲取用戶的認證信息,從而在一次請求中所有過濾器都能共享Authentication(認證),減少了HttpRequest參數的傳送,下面的代碼是從安全上下文的獲取Authentication對象的方法:

          1 SecurityContext context = SecurityContextHolder.getContext();
          2 
          3 Authentication authentication = context.getAuthentication();

              但是,ThreadLoacl不能跨越多個請求存在,所以,集成過濾器在請求開始時從Http會話中取出用戶認證信息并創建一個SecurityContextHolder將Authentication對象保存在其中,在請求結束之后,在從SecurityContextHolder中獲取Authentication對象并將其放回Http會話中,共下次請求使用,從而達到了跨越多個請求的目的。集成過濾器還有其它的實現,可以參考相關文檔。

          提示:
          集成過濾器必須在其它過濾器之前被使用。
           

              logoutFilter(退出過濾器) ,退出登錄操作:

           1 <bean id="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>



          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 大丰市| 贵港市| 山阴县| 罗源县| 梁河县| 搜索| 永寿县| 岢岚县| 仪陇县| 长白| 郯城县| 阜新市| 紫金县| 望都县| 双辽市| 麟游县| 姚安县| 青铜峡市| 崇明县| 忻州市| 开鲁县| 云霄县| 长治县| 苗栗市| 乌审旗| 长宁县| 江津市| 河南省| 大宁县| 察雅县| 当涂县| 高要市| 武夷山市| 遵义县| 东港市| 惠安县| 芦山县| 巴楚县| 莱阳市| 平邑县| 长汀县|