1. 程式人生 > >spring security與cas 整合(中)

spring security與cas 整合(中)

上一篇對於spring security與cas整合中涉及的名詞,認證與授權進行簡單說明,現在將spring security與cas整合的配置檔案簡單貼上來,這其中所需要的jar太多了,主要涉及cas client 3.1,spring security 3.2, spring security-cas client,spring 3.2這幾類jar包。

主要配置如下:

web.xml主要內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>Spring Security CAS Demo Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext-security-success.xml
        </param-value>
    </context-param>
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

spring-security.xml內容如下:其中關於proxy-ticket部分被我注掉了,因為我在實際專案中沒有應用,有興趣的盆友可以測試一下。
<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
    xmlns="http://www.springframework.org/schema/security" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <http entry-point-ref="casEntryPoint" >
     <intercept-url pattern="/**" access="ROLE_USER" />
        <custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER" />
        <custom-filter ref="singleLogoutFilter" before="CAS_FILTER" />
        <custom-filter ref="casAuthenticationFilter" position="CAS_FILTER" />
        <logout logout-success-url="/cas-logout.jsp" />
    </http>

    <authentication-manager alias="authenticationManager">
        <authentication-provider ref="casAuthenticationProvider" />
    </authentication-manager>

     <user-service id="userService">
        <user name="scott" password="scott" authorities="ROLE_USER" />
    </user-service>
    <b:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter" />
    <b:bean id="requestSingleLogoutFilter"
        class="org.springframework.security.web.authentication.logout.LogoutFilter"
        p:filterProcessesUrl="/j_spring_cas_security_logout">
        <b:constructor-arg value="${cas.server.host}/cas/logout" />
        <b:constructor-arg>
            <b:bean
                class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
        </b:constructor-arg>
    </b:bean>

    <b:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties"
        p:service="${cas.service.host}/j_spring_cas_security_check"
        p:authenticateAllArtifacts="true" />

    <b:bean id="casAuthenticationFilter"
        class="org.springframework.security.cas.web.CasAuthenticationFilter"
        p:authenticationManager-ref="authenticationManager" p:serviceProperties-ref="serviceProperties">
        <!--
        <b:property name="proxyGrantingTicketStorage" ref="pgtStorage" />
        <b:property name="proxyReceptorUrl" value="/j_spring_cas_security_proxyreceptor"/>
        <b:property name="authenticationDetailsSource">
            <b:bean
                class="org.springframework.security.cas.web.authentication.ServiceAuthenticationDetailsSource" />
        </b:property>
        <b:property name="authenticationFailureHandler">
            <b:bean
                class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"
                p:defaultFailureUrl="/casfailed.jsp" />
        </b:property> -->
    </b:bean>
    
    
    <b:bean id="casEntryPoint"
        class="org.springframework.security.cas.web.CasAuthenticationEntryPoint"
        p:serviceProperties-ref="serviceProperties" p:loginUrl="${cas.server.host}/cas/login" />
        
    <!-- NOTE: In a real application you should not use an in memory implementation.
        You will also want to ensure to clean up expired tickets by calling ProxyGrantingTicketStorage.cleanup()
    <b:bean id="pgtStorage"
        class="org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl" /> -->
        
    <b:bean id="casAuthenticationProvider"
        class="org.springframework.security.cas.authentication.CasAuthenticationProvider"
        p:serviceProperties-ref="serviceProperties" p:key="casAuthProviderKey">
        <b:property name="authenticationUserDetailsService">
            <b:bean
                class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
                <b:constructor-arg ref="userService" />
            </b:bean>
        </b:property>
        <b:property name="ticketValidator">
            <b:bean class="org.jasig.cas.client.validation.Cas20ProxyTicketValidator">
                 <b:constructor-arg value="${cas.server.host}/cas" />
                <!--
                <b:property name="acceptAnyProxy" value="true" />
                <b:property name="proxyCallbackUrl" value="${cas.service.host}/j_spring_cas_security_proxyreceptor" />
                <b:property name="proxyGrantingTicketStorage" ref="pgtStorage" />-->
            </b:bean>
        </b:property>
        <!--  
        <b:property name="statelessTicketCache">
            <b:bean
                class="org.springframework.security.cas.authentication.EhCacheBasedTicketCache">
                <b:property name="cache">
                    <b:bean class="net.sf.ehcache.Cache" init-method="initialise"
                        destroy-method="dispose">
                        <b:constructor-arg value="casTickets" />
                        <b:constructor-arg value="50" />
                        <b:constructor-arg value="true" />
                        <b:constructor-arg value="false" />
                        <b:constructor-arg value="3600" />
                        <b:constructor-arg value="900" />
                    </b:bean>
                </b:property>
            </b:bean>
        </b:property> -->
    </b:bean>
    
    <context:property-placeholder
        system-properties-mode="OVERRIDE" properties-ref="environment" />    
    <util:properties id="environment">
        <b:prop key="cas.service.host">http://localhost:8080/securitytest</b:prop>
        <b:prop key="cas.server.host">http://localhost:7080</b:prop>
    </util:properties>
</b:beans>

這個配置很簡單,但是在實際的專案中基本上不能使用,因為使用者的授權與資源的管理都是配置在xml檔案中,參考spring-security的文件,我們可以找到關於使用者授權的部分的功能進行資料庫配置的形式,但是關於資源管理的部分是沒有的,這樣是不能靈活進行系統資源管理。這個涉及到系統功能設計:系統資源管理,系統選單管理,使用者認證,使用者授權幾部分,後邊會分幾篇文章來分別完成相應的內容。後面的文章上來的速度可能會很慢,因為我要抽時間來完善我們系統框架,然後將相應的內容分享給大家。