單點登錄解決方案-CAS


1.開源單點登錄系統CAS入門

  1.1 什么是單點登錄

    單點登錄(Single Sign On),簡稱為 SSO,是目前比較流行的企業業務整合的解決方案之一。SSO的定義是在多個應用系統中,用戶只需要登錄一次就可以訪問所有相互信任的應用系統。

  我們目前的系統存在諸多子系統,而這些子系統是分別部署在不同的服務器中,那么使用傳統方式的session是無法解決的,我們需要使用相關的單點登錄技術來解決。

                                                                            

  

  1.2 什么是CAS

    CAS 是 Yale 大學發起的一個開源項目,旨在為 Web 應用系統提供一種可靠的單點登錄方法,CAS 在 2004 年 12 月正式成為 JA-SIG 的一個項目。CAS 具有以下特點:

    【1】開源的企業級單點登錄解決方案。

    【2】CAS Server 為需要獨立部署的 Web 應用。

    【3】CAS Client 支持非常多的客戶端(這里指單點登錄系統中的各個 Web 應用),包括 Java, .Net, PHP, Perl, Apache, uPortal, Ruby 等。

    從結構上看,CAS 包含兩個部分: CAS Server 和 CAS Client。CAS Server 需要獨立部署,主要負責對用戶的認證工作;CAS Client 負責處理對客戶端受保護資源的訪問請求,

    需要登錄時,重定向到 CAS Server。下圖是 CAS 最基本的協議過程:

                         

      Spring-Security整合CAS認證流程:用戶通過客戶端瀏覽器訪問某一個網頁,網頁中的security安全框架會攔截此請求並重定向到CAS服務器的登錄驗證頁面,用戶通過輸入用戶名和密碼進行數據庫驗證,驗證通過服務器會隨機給客戶端瀏覽器返回一個票據(Service Ticket);客戶端瀏覽器攜帶此票據再次訪問原網頁,此時網頁會將攜帶的票據與服務器做驗證,驗證通過,服務器將對應的用戶信息返回給網頁的session域,此時用戶就能訪問網站了。

      注意:票據驗證過程是底層自動完成的,不需要用戶進行操作。用戶所能感受到的只是正常的登錄與訪問頁面。

  1.3 CAS服務端部署

    Cas服務端其實就是一個war包,將此war包復制到tomcat的webapp目錄下,運行tomcat自動解壓war包,瀏覽器輸入http://localhost:8080/cas/login  ,可看到登錄頁面

                

    此登陸頁面后期可以改為目標網站的登錄頁面樣式。

      這里有個固定的用戶名和密碼   casuser /Mellon,登錄成功后會跳到登錄成功的提示頁面

            

  

  1.4 CAS服務端配置   

    1.4.1端口修改   

      如果我們不希望用8080端口訪問CAS, 可以修改端口

      (1)修改TOMCAT的端口

      打開tomcat 目錄 conf\server.xml  找到下面的配置

         

      將端口8080改為9100(沒有限制,前提是端口在合理范圍內)

      (2)修改CAS配置文件

      修改cas的WEB-INF/cas.properties 

        server.name=http://localhost:9100
        erver.prefix=${server.name}/cas
        # IP address or CIDR subnet allowed to access the /status URI of CAS that exposes health check information
        cas.securityContext.status.allowedSubnet=127.0.0.1

      瀏覽器中將端口號改為9100再次訪問,看是否能訪問到cas登錄頁面。

    1.4.2去除https認證

      CAS默認使用的是HTTPS協議,如果使用HTTPS協議需要SSL安全證書(需向特定的機構申請和購買) 。如果對安全要求不高或是在開發測試階段,

     可使用HTTP協議。我們這里講解通過修改配置,讓CAS使用HTTP協議。

      (1)修改cas的WEB-INF/deployerConfigContext.xml

      找到下面的配置

<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient"/>

      這里需要增加參數p:requireSecure="false",requireSecure屬性意思為是否需要安全驗證,即HTTPS,false為不采用

      (2)修改cas的/WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml

      找到下面配置

<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"

      p:cookieSecure="true"

      p:cookieMaxAge="-1"

      p:cookieName="CASTGC"

      p:cookiePath="/cas" />

      參數p:cookieSecure="true",同理為HTTPS驗證相關,TRUE為采用HTTPS驗證,FALSE為不采用https驗證。

      參數p:cookieMaxAge="-1",是COOKIE的最大生命周期,-1為無生命周期,即只在當前打開的窗口有效,關閉或重新打開其它窗口,仍會要求驗證。

      可以根據需要修改為大於0的數字,比如3600等,意思是在3600秒內,打開任意窗口,都不需要驗證。

      我們這里將cookieSecure改為false ,  cookieMaxAge 改為3600

      (3)修改cas的WEB-INF/spring-configuration/warnCookieGenerator.xml

        找到下面配置

<bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="true"
p:cookieMaxAge="-1"
p:cookieName="CASPRIVACY"
p:cookiePath="/cas" />

      我們這里將cookieSecure改為false ,  cookieMaxAge 改為3600

  1.5 web模塊中的配置文件

  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans:beans xmlns="http://www.springframework.org/schema/security"
 3     xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5                         http://www.springframework.org/schema/beans/spring-beans.xsd
 6                         http://www.springframework.org/schema/security 
 7                         http://www.springframework.org/schema/security/spring-security.xsd">
 8     
 9     <http pattern="/css/**" security="none"></http>
10     <http pattern="/img/**" security="none"></http>
11     <http pattern="/js/**" security="none"></http>
12     <http pattern="/plugins/**" security="none"></http>
13     <http pattern="/user/add.do" security="none"></http>
14     <http pattern="/user/createSmsCode.do" security="none"></http>
15     <http pattern="/register.html" security="none"></http>
16     
17     <!--   entry-point-ref  入口點引用 -->
18     <http use-expressions="false" entry-point-ref="casProcessingFilterEntryPoint">  
19         <intercept-url pattern="/**" access="ROLE_USER"/>   
20         <csrf disabled="true"/>  
21         <!-- custom-filter為過濾器, position 表示將過濾器放在指定的位置上,before表示放在指定位置之前  ,after表示放在指定的位置之后  -->           
22         <custom-filter ref="casAuthenticationFilter"  position="CAS_FILTER" />      
23         <custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER"/>  
24         <custom-filter ref="singleLogoutFilter" before="CAS_FILTER"/>  
25     </http>
26     
27       <!-- CAS入口點 開始 -->
28     <beans:bean id="casProcessingFilterEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">  
29         <!-- cas登錄服務器登錄URL -->  
30         <beans:property name="loginUrl" value="http://localhost:8080/cas/login"/>
31         <beans:property name="serviceProperties" ref="serviceProperties"/>  
32     </beans:bean>      
33     <beans:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">  
34         <!--service 配置自身工程的根地址+/login/cas   -->  
35         <beans:property name="service" value="http://localhost:9106/login/cas"/>
36     </beans:bean>  
37     <!-- CAS入口點 結束 -->
38 
39     
40     <!-- 認證過濾器 開始 -->
41     <beans:bean id="casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">  
42         <beans:property name="authenticationManager" ref="authenticationManager"/>  
43     </beans:bean>  
44         <!-- 認證管理器 -->
45     <authentication-manager alias="authenticationManager">
46         <authentication-provider  ref="casAuthenticationProvider">
47         </authentication-provider>
48     </authentication-manager>
49         <!-- 認證提供者 -->
50     <beans:bean id="casAuthenticationProvider"     class="org.springframework.security.cas.authentication.CasAuthenticationProvider">  
51         <beans:property name="authenticationUserDetailsService">  
52             <beans:bean class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">  
53                 <beans:constructor-arg ref="userDetailsService" />  
54             </beans:bean>  
55         </beans:property>  
56         <beans:property name="serviceProperties" ref="serviceProperties"/>  
57         <!-- ticketValidator 為票據驗證器 -->
58         <beans:property name="ticketValidator">  
59             <beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">  
60                 <!-- 設置cas票據驗證地址 -->
61                 <beans:constructor-arg index="0" value="http://localhost:8080/cas"/>
62             </beans:bean>  
63         </beans:property>  
64         <beans:property name="key" value="an_id_for_this_auth_provider_only"/> 
65     </beans:bean>        
66      <!-- springScurity認證類 -->
67     <beans:bean id="userDetailsService" class="com.pinyougou.user.service.UserDetailServiceImpl"/>  
68     
69     <!-- 認證過濾器 結束 -->
70     
71     
72     <!-- 單點登出  開始  -->     
73     <beans:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>          
74     <beans:bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">  
75         <beans:constructor-arg value="http://localhost:8080/cas/logout?service=http://localhost:9106/"/>
76         <beans:constructor-arg>  
77             <beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>  
78         </beans:constructor-arg> 
79         <!-- 將本地/logout/cas和 64行退出后的部分進行綁定 -->     
80         <beans:property name="filterProcessesUrl" value="/logout/cas"/>  
81     </beans:bean>  
82     <!-- 單點登出  結束 -->  
83     
84 </beans:beans>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM