CAS 5.2.x 單點登錄 - 搭建服務端和客戶端

一、簡介

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

CAS 是一個開源的企業級單點登錄系統,目前最新版本為 5.2.x
CAS 包含兩個部分:CAS Server 和 CAS Client,它們之間獨立部署。CAS 客戶端攔截未認證的用戶請求,并重定向至 CAS 服務端,由 CAS 服務端對用戶身份進行統一認證。

二、搭建服務端

對于本地搭建 CAS 服務端,官方提供了基于 Maven 和 Gradle 的 Overlay 構建方式,本文用的是 CAS Maven WAR Overlay

2.1 什么是 WAR Overlay?

Overlay 技術可以把多個項目 war 合并成為一個項目,如果項目存在同名文件,那么主項目中的文件將覆蓋掉其他項目的同名文件。

使用 Overlay 無需對 CAS 源碼進行編譯,也避免了對 CAS 源碼進行侵入性改造。

2.2 環境清單

  • JDK 1.8
  • Tomcat 8.0+
  • IntelliJ IDEA 2017.2

2.3 Overlay 構建

下載 CAS Maven WAR Overlay,修改 pom.xml ,設置 CAS 版本為 5.2.2。建議去除掉 pom.xml 文件中的 wrapper-maven-plugin 和無用的 profile 配置。

<properties>
    <cas.version>5.2.2</cas.version>
</properties>

首次導入 IDEA,可以看到后臺正在下載官方 cas.war。

CAS Maven Overlay

工程 overlays 目錄下的文件是由 maven 編譯后才產生的,可以在 pom.xml 中配置官方 cas.war 中的文件的那些文件可以排除,不要在 overlays 中生成:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <warName>cas</warName>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <recompressZippedFiles>false</recompressZippedFiles>
        <archive>
            <compress>false</compress>
            <manifestFile>${manifestFileToUse}</manifestFile>
        </archive>
        <overlays>
            <overlay>
                <groupId>org.apereo.cas</groupId>
                <artifactId>cas-server-webapp${app.server}</artifactId>
                <!--原有的服務不再初始化進去-->
                <excludes>
                    <exclude>WEB-INF/classes/services/*</exclude>
                    <exclude>WEB-INF/classes/application.*</exclude>
                </excludes>
            </overlay>
        </overlays>
    </configuration>
</plugin>

打開 Project Structure,可以觀察到該工程具有兩個 Web Root,但是 src/main/webapp 目錄并不存在,需要進行手動創建。

Project Structure

拷貝 overlays 目錄下的 application.properties 配置文件至 resources 目錄,用于覆蓋 CAS WAR 中的同名文件。最終工程目錄結構如下:

Project Structure

為工程配置 tomcat 8.0 并啟動,注意 CAS 5.2.x 不支持低于 tomcat 8.0 的版本。
看到控制臺打印 READY 表明啟動成功。

CAS Server Ready

訪問 http://localhost:8080/cas/login 進入登錄界面。

登錄界面

其中Non-secure Connection提示需要配置 SSL,Static Authentication提示需要對用戶配置進行修改,可以修改為 JDBC、REST 等方式。目前用戶配置寫死在 application.properties 配置文件中,用戶名為 casuser,密碼為 Mellon。

##
# CAS Authentication Credentials
#
cas.authn.accept.users=casuser::Mellon

2.4 Services配置

客戶端接入 CAS 首先需要在服務端進行注冊,否則客戶端訪問將提示“未認證授權的服務”警告:

未認證授權的服務

在 resources 文件夾下創建 services 文件夾進行服務定義,該目錄中可包含多個 JSON 文件,其命名必須滿足以下規則:

JSON fileName = serviceName + "-" + serviceNumericId + ".json"

創建 services/Localhost-10000003.json 文件,表示允許所有以 http://localhost 開頭的認證請求:

{
  "@class": "org.apereo.cas.services.RegexRegisteredService",
  "serviceId": "^(http)://localhost.*",
  "name": "本地服務",
  "id": 10000003,
  "description": "這是一個本地允許的服務,通過localhost訪問都允許通過",
  "evaluationOrder": 1
}

對其中屬性的說明如下,更多詳細內容見官方文檔-Service-Management

  • @class:必須為org.apereo.cas.services.RegisteredService的實現類
  • serviceId:對服務進行描述的表達式,可用于匹配一個或多個 URL 地址
  • name: 服務名稱
  • id:全局唯一標志
  • evaluationOrder:定義多個服務的執行順序

最后,根據官方文檔-service-registry,還需修改 application.properties 文件告知 CAS 服務端從本地加載服務定義文件:

#開啟識別json文件,默認false
cas.serviceRegistry.initFromJson=true
#自動掃描服務配置,默認開啟
#cas.serviceRegistry.watcherEnabled=true
#120秒掃描一遍
#cas.serviceRegistry.repeatInterval=120000
#延遲15秒開啟
#cas.serviceRegistry.startDelay=15000
#資源加載路徑
#cas.serviceRegistry.config.location=classpath:/services

啟動時打印以下日志,說明服務注冊成功。

2018-03-18 23:36:08,660 INFO [org.apereo.cas.services.AbstractServicesManager] - <Loaded [0] service(s) from [InMemoryServiceRegistry].>
2018-03-18 23:36:08,876 INFO [org.apereo.cas.config.CasServiceRegistryInitializationConfiguration] - <Attempting to initialize the service registry [InMemoryServiceRegistry] from service definition resources found at [class path resource [services]]>
2018-03-18 23:36:08,877 WARN [org.apereo.cas.services.ServiceRegistryInitializer] - <Service registry [InMemoryServiceRegistry] will be auto-initialized from JSON service definitions. This behavior is only useful for testing purposes and MAY NOT be appropriate for production. Consider turning off this behavior via the setting [cas.serviceRegistry.initFromJson=false] and explicitly register definitions in the services registry.>
2018-03-18 23:36:09,283 INFO [org.apereo.cas.services.AbstractServicesManager] - <Loaded [3] service(s) from [InMemoryServiceRegistry].>

三、搭建客戶端

官方文檔中提供了 CAS Java 客戶端樣例,即 cas-sample-java-webapp
修改 pom.xml,配置 tomcat7-maven-plugin:

<!--  tomcat7 plugin -->
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <port>8181</port>
        <uriEncoding>UTF-8</uriEncoding>
        <server>tomcat7</server>
        <path>/node1</path>
    </configuration>
</plugin>

CAS Client 通過攔截器將未認證的請求重定向到 CAS Server,這里對 cas-sample-java-webapp 的 web.xml 文件進行修改,將服務端、客戶端地址替換為實際測試的地址:

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

<!--
   <context-param>
       <param-name>renew</param-name>
       <param-value>true</param-value>
   </context-param>
-->
    <!--單點登出過濾器-->
    <filter>
        <filter-name>CAS Single Sign Out Filter</filter-name>
        <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
        <init-param>
            <param-name>casServerUrlPrefix</param-name>
            <param-value>http://localhost:8080/cas</param-value>
        </init-param>
    </filter>

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

    <!--用來跳轉登錄-->
    <filter>
        <filter-name>CAS Authentication Filter</filter-name>
        <!--<filter-class>org.jasig.cas.client.authentication.Saml11AuthenticationFilter</filter-class>-->
        <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
        <init-param>
            <param-name>casServerLoginUrl</param-name>
            <param-value>http://localhost:8080/cas/login</param-value>
        </init-param>
        <init-param>
            <param-name>serverName</param-name>
            <!--這是客戶端的部署地址,認證時會帶著這個地址,認證成功后會跳轉到這個地址-->
            <param-value>http://localhost:8181/node1</param-value>
        </init-param>
    </filter>

    <!--Ticket校驗過濾器-->
    <filter>
        <filter-name>CAS Validation Filter</filter-name>
        <!--<filter-class>org.jasig.cas.client.validation.Saml11TicketValidationFilter</filter-class>-->
        <filter-class>org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter</filter-class>
        <init-param>
            <param-name>casServerUrlPrefix</param-name>
            <param-value>http://localhost:8080/cas</param-value>
        </init-param>
        <init-param>
            <param-name>serverName</param-name>
            <param-value>http://localhost:8181/node1</param-value>
        </init-param>
        <init-param>
            <param-name>redirectAfterValidation</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>useSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>authn_method</param-name>
            <param-value>mfa-duo</param-value>
        </init-param>
    </filter>

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

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

    <filter-mapping>
        <filter-name>CAS Single Sign Out Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>CAS Validation Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>CAS Authentication Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>
            index.jsp
        </welcome-file>
    </welcome-file-list>
</web-app>

此時訪問
http://localhost:8181/node1
會跳轉至
http://localhost:8080/cas/login?service=http%3A%2F%2Flocalhost%3A8181%2Fnode1%2F
輸入用戶信息,登錄成功,返回
http://localhost:8181/node1/;jsessionid=6628138DCAAA5BA3481CD4C9238FEBFF

CAS Client

利用相同的方法配置第二個客戶端,訪問地址為 http://localhost:8282/node2,可知在 node1 登錄成功的情況下,無需再次輸入用戶密碼即可訪問 node2 后臺頁面。

至此,開發環境搭建完畢。由于客戶端只是對 web.xml 中的過濾器進行配置,可以很方便地集成到各個業務系統中。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容