1. 程式人生 > >Spring-Security整合CAS之Spring-Security.xml部分配置詳解

Spring-Security整合CAS之Spring-Security.xml部分配置詳解

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
	xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
	
	<!--   entry-point-ref  入口點引用,
		casProcessingFilterEntryPoint:不使用CAS,原來的登入系統是自行提供。CAS本身不做登入,而是跳轉一個點進行登入,這個點就是入口點(即不在當前系統登入)
		CAS不止一種技術,具體實現需要使用org.springframework.security.cas.web.CasAuthenticationEntryPoint
	 -->
	<http use-expressions="false" entry-point-ref="casProcessingFilterEntryPoint"> 
		<!-- 根目錄下資源訪問,都必須擁有ROLE_USER角色 --> 
        <intercept-url pattern="/**" access="ROLE_USER"/>   
        <!-- 避免跨域攻擊 -->
        <csrf disabled="true"/>  
        <!-- custom-filter為過濾器, position 表示將過濾器放在指定的位置上,before表示放在指定位置之前  ,after表示放在指定的位置之後  ,與Spring-Security相關-->           
        <!-- 表示在當前http請求中配置一個過濾器 ,CAS_FILTER是Spring-Security配置好的位置(也就是下面配置一個過濾器並放置到CAS_FILTER位置)-->
        <custom-filter ref="casAuthenticationFilter"  position="CAS_FILTER" />  
        <!-- 與單點登出有關 -->    
        <custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER"/>  
        <!-- 在requestSingleLogoutFilter過濾器執行之前執行 -->
        <custom-filter ref="singleLogoutFilter" before="CAS_FILTER"/>  
    </http>
    
  	<!-- CAS入口點 開始 -->
    <beans:bean id="casProcessingFilterEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">  
        <!-- 單點登入伺服器登入URL -->  
        <beans:property name="loginUrl" value="http://localhost:8084/cas/login"/> 
        <beans:property name="serviceProperties" ref="serviceProperties"/>  
    </beans:bean>      
    <beans:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">  
        <!--service 配置自身工程的根地址+/login/cas 
        	固定寫法,是由CAS整合的地址,這個地址就是登入之後的回撥地址
          -->  
        <beans:property name="service" value="http://localhost:9003/login/cas"/>
    </beans:bean>  
    <!-- CAS入口點 結束 -->

    
    <!-- 認證過濾器 開始 -->
    <beans:bean id="casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">  
        <!-- 認證管理器 -->
        <beans:property name="authenticationManager" ref="authenticationManager"/>  
    </beans:bean>  
		<!-- 認證管理器 -->
	<authentication-manager alias="authenticationManager">
		<authentication-provider  ref="casAuthenticationProvider">
		</authentication-provider>
	</authentication-manager>
		<!-- 認證提供者 -->
	<beans:bean id="casAuthenticationProvider"     class="org.springframework.security.cas.authentication.CasAuthenticationProvider"> 
		<!-- 
			相當於本身是個bean,就省去了寫id
		 --> 
        <beans:property name="authenticationUserDetailsService">  
            <beans:bean class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">  
                <beans:constructor-arg ref="userDetailsService" />  
            </beans:bean>  
        </beans:property>  
        <!-- 為了得到CAS客戶端地址 -->
        <beans:property name="serviceProperties" ref="serviceProperties"/>  
        <!-- ticketValidator 為票據驗證器,負責對Ticket的校驗工作,必須啟用 -->
        <beans:property name="ticketValidator">  
            <beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">  
                <beans:constructor-arg index="0" value="http://localhost:8084/cas"/>  
            </beans:bean>  
        </beans:property>  
        <beans:property name="key" value="an_id_for_this_auth_provider_only"/> 
    </beans:bean>        
  	<!-- 認證類 -->
	<beans:bean id="userDetailsService" class="com.phubing.service.UserDetailServiceImpl"/>  
	
	<!-- 認證過濾器 結束 -->
	
	
	<!-- 單點登出  開始,經過此配置後,當用戶在位址列輸入本地工程地址/logout/cas即可實現登出  --> 
	<!-- CAS原生包提供,真正對服務端進行登出 -->    
    <beans:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>          
    <!-- 由Spring-Security提供,只是為了配置地址,即對映地址 -->
    <beans:bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">  
    	<!-- CAS服務端登出地址 -->
        <beans:constructor-arg value="http://localhost:8084/cas/logout?service=http://www.baidu.com"/>  
        <beans:constructor-arg>  
            <beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>  
        </beans:constructor-arg>  
        <!-- 本地登出地址,用於做兩個地址之間的匹配 -->
        <beans:property name="filterProcessesUrl" value="/logout/cas"/>  
    </beans:bean>  
    <!-- 單點登出  結束 -->  
	
</beans:beans>

這裡只是配置了部分,初學者查閱資料所得,供日後使用參考,如有錯誤歡迎提出。