單點登錄(SSO)解決方案之 CAS 入門案例


單點登錄:

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

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 等。

 

SSO單點登錄訪問流程主要步驟:

  1. 訪問服務:SSO客戶端發送請求訪問應用系統提供的服務資源。

  2. 定向認證:SSO客戶端會重定向用戶請求到SSO服務器。

  3. 用戶認證:用戶身份認證。

  4. 發放票據:SSO服務器會產生一個隨機的Service Ticket。

  5. 驗證票據:SSO服務器驗證票據Service Ticket的合法性,驗證通過后,允許客戶端訪問服務。

  6. 傳輸用戶信息:SSO服務器驗證票據通過后,傳輸用戶認證結果信息給客戶端。

 

CAS服務端(CAS Server)部署:

准備工作:

  cas服務端其實就是一個war包。 首先需要從 cas官網  下載cas-server,我這里下載並使用的是 cas-server-4.0.0-release.zip。

  解壓后,在modules目錄下的cas-server_webapp-4.0.0.war,將其改名為cas.war放入tomcat目錄下的webapps下。

  所以上述為需要准備的東西,cas-server的war包以及tomcat,下面用的tomcat為apache-tomcat-7.0.77

 

啟動tomcat自動解壓war包。瀏覽器輸入 http://localhost:8080/cas/login ,可看到登錄頁面:

這個頁面也就是我們做單點登錄各個系統共用的登錄頁面,有點丑,我們后面會用自己的登錄頁面替換。

這里有個固定的用戶名和密碼 casuser /Mellon

登錄成功后會跳到登錄成功的提示頁面:

 

地址欄輸入 http://localhost:8080/cas/logout可退出登錄。

 

 

CAS服務端配置:

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

1,修改tomcat端口:

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

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

這里案例中將端口8080修改為9100。

2,修改CAS配置文件:

修改cas的WEB-INF/cas.properties:

server.name=http://localhost:9100

3,去除https認證

CAS默認使用的是HTTPS協議,如果使用HTTPS協議需要SSL安全證書(需向特定的機構申請和購買) 。如果對安全要求不高或是在開發測試階段,可使用HTTP協議。我們這里講解通過修改配置,讓CAS使用HTTP協議。

 

3.1,修改cas的WEB-INF/deployerConfigContext.xml

找到以下配置:

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

修改為(requireSecure屬性意思為是否需要安全驗證,即HTTPS,false為不采用):

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

 

3.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" />

修改為(將cookieSecure改為false , cookieMaxAge 改為3600):

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

    參數p:cookieMaxAge="-1",是COOKIE的最大生命周期,-1為無生命周期,即只在當前打開的窗口有效,關閉或重新打開其它窗口,仍會要求驗證。可以根據需要修改為大於0的數字,比如3600等,意思是在3600秒內,打開任意窗口,都不需要驗證。
修改說明
<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
      p:cookieSecure="false"
      p:cookieMaxAge="3600"
      p:cookieName="CASTGC"
      p:cookiePath="/cas" />

 

3.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" />

修改為:

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

 

CAS客戶端入門demo:

搭建客戶端1:

創建Maven工程 (war)casclient_demo1 引入cas客戶端依賴並制定tomcat運行端口為9001

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>project_demo</artifactId>
        <groupId>com.zy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>casclient_demo1</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <!-- cas 所需依賴-->
        <dependency>
            <groupId>org.jasig.cas.client</groupId>
            <artifactId>cas-client-core</artifactId>
            <version>3.3.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定端口 -->
                    <port>9001</port>
                    <!-- 請求路徑 -->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

修改web.xml:(后面會把casserver放到一台虛擬機上:192.168.44.31,建議先放到本地,最后再放到其他機器上,因為后面還要修改配置文件,如果放在本地的話 可以把注釋放開 把下面一行注釋掉)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <display-name>Archetype Created Web Application</display-name>

    <!-- 用於單點退出,該過濾器用於實現單點登出功能,可選配置 -->
    <listener>
        <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
    </listener>

    <!-- 該過濾器用於實現單點登出功能,可選配置。 -->
    <filter>
        <filter-name>CAS Single Sign Out Filter</filter-name>
        <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CAS Single Sign Out Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- 該過濾器負責用戶的認證工作,必須啟用它 -->
    <filter>
        <filter-name>CASFilter</filter-name>
        <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
        <init-param>
            <param-name>casServerLoginUrl</param-name>
            <!--單點登錄 cas登錄url-->
            <!--<param-value>http://localhost:9100/cas/login</param-value>-->
            <param-value>http://192.168.44.31:9100/cas/login</param-value>
        </init-param>
        <init-param>
            <param-name>serverName</param-name>
            <!--程序的url-->
            <param-value>http://localhost:9001</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CASFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 該過濾器負責對Ticket的校驗工作,必須啟用它 -->
    <filter>
        <filter-name>CAS Validation Filter</filter-name>
        <filter-class>
            org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
        </filter-class>
        <init-param>
            <param-name>casServerUrlPrefix</param-name>
            <!--<param-value>http://localhost:9100/cas</param-value>-->
            <param-value>http://192.168.44.31:9100/cas</param-value>
        </init-param>
        <init-param>
            <param-name>serverName</param-name>
            <param-value>http://localhost:9001</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CAS Validation Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!--獲取用戶名的兩個過濾器-->
    <!-- 該過濾器負責實現HttpServletRequest請求的包裹, 比如允許開發者通過HttpServletRequest的getRemoteUser()方法獲得SSO登錄用戶的登錄名,可選配置。 -->
    <filter>
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
        <filter-class>
            org.jasig.cas.client.util.HttpServletRequestWrapperFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 該過濾器使得開發者可以通過org.jasig.cas.client.util.AssertionHolder來獲取用戶的登錄名。 比如AssertionHolder.getAssertion().getPrincipal().getName()。 -->
    <filter>
        <filter-name>CAS Assertion Thread Local Filter</filter-name>
        <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CAS Assertion Thread Local Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

編寫一個index.jsp頁面:

<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>1</title>
</head>
<body>
<h1>歡迎訪問網站1</h1>
<%=request.getRemoteUser()%>
<br/>
<%--<a href="http://localhost:9100/cas/logout?service=http://www.baidu.com">退出登錄</a>--%>
<a href="http://192.168.44.31:9100/cas/logout?service=http://www.baidu.com">退出登錄</a>
</body>
</html>

我們希望退出登錄后,能自動跳轉到某個頁面,那如何處理呢?

修改cas系統的配置文件cas-servlet.xml(把false修改成true,允許重定向)

  <bean id="logoutAction" class="org.jasig.cas.web.flow.LogoutAction"
        p:servicesManager-ref="servicesManager"
        p:followServiceRedirects="${cas.logout.followServiceRedirects:true}"/>

我們的index頁面上有一個退出登錄的功能,而且url中有個service=http://www.baidu.com,這個www.baidu.com就是退出后跳轉到的頁面

 

搭建客戶端2(casclient_demo2):參考casclient_demo1,只需要修改其中tomcat端口(9002)以及index頁面內容即可。

 

單點登錄測試:

(1)啟動cas部署的tomcat

(2)啟動客戶端工程1和客戶端工程2

(3)地址欄輸入http://localhost:9001/index.jsp 和http://localhost:9002/index.jsp ,地址均會跳轉到CAS登錄頁

(4)輸入用戶名和密碼后(默認賬號密碼 casuser /Mellon,后面我們再改造成從數據庫取賬號密碼),頁面跳轉回9002 ,刷新9001也可以打開index.jsp(不需要登錄)。

 

下一篇:單點登錄(SSO)解決方案之 CAS服務端數據源設置及頁面改造

 


免責聲明!

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



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