CAS 實(shí)現(xiàn) SSO 單點(diǎn)登錄

環(huán)境

cas-server-4.1.8,

cas-client-3.4.0,

Java-8,Maven-3,

Tomcat-7.0.72

CAS Server 安裝

點(diǎn)此進(jìn)入CAS下載列表,選擇下載 cas-4.1.8.zip。

https://github.com/apereo/cas/releases

解壓縮 cas-4.1.8.zip 并進(jìn)入 cas-server-webapp 目錄,在當(dāng)前目錄打開 cmd 并執(zhí)行安裝命令。

mvn -e -ff clean install -Dmaven.test.skip=true

經(jīng)親身測試(自己拉的電信12M網(wǎng)絡(luò)),該安裝過程非常漫長,主要因?yàn)殓R像原因?qū)е乱蕾嚢螺d非常慢,此過程需靜心等待?;蛑苯酉螺d我已經(jīng)打好包的 cas.war 文件(注:該文件的依賴包版本有稍做修改,此不影響正常使用)。

安裝完成后,在 cas-server-webapp/target 目錄下可以看到 cas.war 文件,該文件便是 cas?server 應(yīng)用服務(wù)的 war 包。

cas server 安全認(rèn)證是基于 https 的,這里使用 JDK 自帶的 keytool 工具生成數(shù)字證書,生產(chǎn)環(huán)境系統(tǒng)的應(yīng)用需要到證書提供商處購買證書。證書的生成及 Tomcat 的配置可參考文章:keytool 生成數(shù)字證書 – tomcat https 配置。

https://fanlychie.github.io/post/java-keytool-tomcat-https.html

首先確保?Tomcat 的?https 可以正常訪問,將 cas.war 文件拷貝到 apache-tomcat-7.0.72/webapps 下進(jìn)行發(fā)布,啟動(dòng) Tomcat,訪問 https://www.fanlychie.com:8443/cas。

上圖是用火狐瀏覽器打開的鏈接,選擇高級(jí) -> 添加例外 -> 確認(rèn)安全例外。

用戶名和密碼在 apache-tomcat-7.0.72/webapps/cas/WEB-INF/deployerConfigContext.xml 配置文件中,找到并打開該文件,大概在 105 行

<bean id="primaryAuthenticationHandler"

? ? class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">

? ? <property name="users">

? ? ? ? <map>

? ? ? ? ? ? <entry key="casuser" value="Mellon" />

? ? ? ? </map>

? ? </property>

</bean>

可以看到默認(rèn)的用戶名是 casuser,密碼是 Mellon。

看到上圖的頁面,表明 cas server 已經(jīng)部署成功。

CAS Server 配置基于數(shù)據(jù)庫用戶認(rèn)證

回到 cas-4.1.8.zip 解壓縮的目錄,并進(jìn)入 cas-server-support-jdbc 目錄,在當(dāng)前目錄打開 cmd 并執(zhí)行安裝命令

mvn -e -ff clean install -Dmaven.test.skip=true

安裝完成后在 target 目錄得到 cas-server-support-jdbc-4.1.8.jar 文件。

將該文件拷貝到 apache-tomcat-7.0.72/webapps/cas/WEB-INF/lib 目錄下,并向此目錄添加 c3p0-0.9.1.2.jar,mysql-connector-java-5.1.17.jar 兩個(gè)文件。嫌麻煩的話,點(diǎn)此下載這三個(gè) jar 包的壓縮包文件。

http://pan.baidu.com/s/1pLIrdWn

再次打開 apache-tomcat-7.0.72/webapps/cas/WEB-INF/deployerConfigContext.xml 文件,大概在第 54 行。

<bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager">

? ? <constructor-arg>

? ? ? ? <map>

? ? ? ? ? ? <!--

? ? ? ? ? ? ? ?| IMPORTANT

? ? ? ? ? ? ? ?| Every handler requires a unique name.

? ? ? ? ? ? ? ?| If more than one instance of the same handler class is configured, you must explicitly

? ? ? ? ? ? ? ?| set its name to something other than its default name (typically the simple class name).

? ? ? ? ? ? ? ?-->

? ? ? ? ? ? <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" />

? ? ? ? ? ? <!-- 注銷此項(xiàng)

? ? ? ? ? ? <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />

? ? ? ? ? ? -->

? ? ? ? ? ? <!-- 添加此項(xiàng) -->

? ? ? ? ? ? <entry key-ref="myAuthenticationHandler" value-ref="primaryPrincipalResolver" />

? ? ? ? </map>

? ? </constructor-arg>

? ? <!-- Uncomment the metadata populator to capture the password.

? ? <property name="authenticationMetaDataPopulators">

? ? ? ?<util:list>

? ? ? ? ? ?<bean class="org.jasig.cas.authentication.CacheCredentialsMetaDataPopulator"/>

? ? ? ?</util:list>

? ? </property>

? ? -->

? ? <!--

? ? ? ?| Defines the security policy around authentication. Some alternative policies that ship with CAS:

? ? ? ?|

? ? ? ?| * NotPreventedAuthenticationPolicy - all credential must either pass or fail authentication

? ? ? ?| * AllAuthenticationPolicy - all presented credential must be authenticated successfully

? ? ? ?| * RequiredHandlerAuthenticationPolicy - specifies a handler that must authenticate its credential to pass

? ? ? ?-->

? ? <property name="authenticationPolicy">

? ? ? ? <bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy" />

? ? </property>

</bean>

按以上配置注銷掉第二個(gè) entry 并添加一個(gè) entry。接著在后面添加兩個(gè) bean 配置。

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

? ? <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/cas_test_db?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8" />

? ? <property name="user" value="root" />

? ? <property name="password" value="root" />

? ? <property name="driverClass" value="com.mysql.jdbc.Driver" />

? ? <property name="initialPoolSize" value="10" />

? ? <property name="maxIdleTime" value="1800" />

? ? <property name="maxPoolSize" value="60" />

? ? <property name="acquireIncrement" value="5" />

? ? <property name="acquireRetryAttempts" value="60" />

? ? <property name="acquireRetryDelay" value="2000" />

? ? <property name="breakAfterAcquireFailure" value="false" />

? ? <property name="autoCommitOnClose" value="false" />

? ? <property name="checkoutTimeout" value="30000" />

? ? <property name="idleConnectionTestPeriod" value="900" />

</bean>

<bean id="myAuthenticationHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"

? ? p:dataSource-ref="dataSource"

? ? p:sql="SELECT passwd FROM user WHERE name = ?" />

其中 cas_test_db 數(shù)據(jù)庫中的 user 建表語句為

CREATE TABLE `user` (

? `id` int(11) NOT NULL AUTO_INCREMENT,

? `name` varchar(255) NOT NULL,

? `passwd` varchar(255) NOT NULL,

? PRIMARY KEY (`id`)

)

重啟 Tomcat,訪問 https://www.fanlychie.com:8443/cas,用數(shù)據(jù)庫中的 name/passwd 作為用戶名和密碼登錄系統(tǒng),若登錄成功,表明配置已成功。

CAS Client 客戶端使用和配置

使用 maven 創(chuàng)建兩個(gè) web 項(xiàng)目 cas-client1,cas-client2。點(diǎn)此下載 demo 文件。

cas-client1 項(xiàng)目 pom.xml 配置

<dependencies>

? ? <dependency>

? ? ? ? <groupId>org.jasig.cas.client</groupId>

? ? ? ? <artifactId>cas-client-core</artifactId>

? ? ? ? <version>3.4.0</version>

? ? </dependency>

? ? <dependency>

? ? ? ? <groupId>org.slf4j</groupId>

? ? ? ? <artifactId>slf4j-log4j12</artifactId>

? ? ? ? <version>1.7.12</version>

? ? </dependency>

</dependencies>

<build>

? ? <plugins>

? ? ? ? <plugin>

? ? ? ? ? ? <groupId>org.apache.tomcat.maven</groupId>

? ? ? ? ? ? <artifactId>tomcat7-maven-plugin</artifactId>

? ? ? ? ? ? <version>2.2</version>

? ? ? ? ? ? <configuration>

? ? ? ? ? ? ? ? <path>/</path>

? ? ? ? ? ? ? ? <port>8881</port>

? ? ? ? ? ? ? ? <httpsPort>8081</httpsPort>

? ? ? ? ? ? ? ? <uriEncoding>UTF-8</uriEncoding>

? ? ? ? ? ? ? ? <protocol>org.apache.coyote.http11.Http11NioProtocol</protocol>

? ? ? ? ? ? ? ? <clientAuth>false</clientAuth>

? ? ? ? ? ? ? ? <keystoreFile>C:\Users\fanlychie\.keystore\selfissue.jks</keystoreFile>

? ? ? ? ? ? ? ? <keystorePass>123654</keystorePass>

? ? ? ? ? ? ? ? <keystoreType>JKS</keystoreType>

? ? ? ? ? ? ? ? <url>http://localhost:8081/manager/html</url>

? ? ? ? ? ? </configuration>

? ? ? ? </plugin>

? ? </plugins>

</build>

首先必須確保項(xiàng)目 https 協(xié)議可以正常訪問,否則 cas server 無法認(rèn)證。

選中項(xiàng)目 -> Run As -> Maven build… -> tomcat7:run

訪問 https://www.fanlychie.com:8081,若能訪問到,表明 Tomcat 已準(zhǔn)備好。

cas-client1 項(xiàng)目 web.xml 配置


<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">

? ? <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>

? ? ? ? <init-param>

? ? ? ? ? ? <param-name>casServerUrlPrefix</param-name>

? ? ? ? ? ? <!-- 此處配置的是 cas server 地址 -->

? ? ? ? ? ? <param-value>https://www.fanlychie.com:8443/cas</param-value>

? ? ? ? </init-param>

? ? </filter>

? ? <filter-mapping>

? ? ? ? <filter-name>CAS Single Sign Out Filter</filter-name>

? ? ? ? <url-pattern>/*</url-pattern>

? ? </filter-mapping>


? ? <filter>

? ? ? ? <filter-name>CAS Authentication Filter</filter-name>

? ? ? ? <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>

? ? ? ? <init-param>

? ? ? ? ? ? <param-name>casServerLoginUrl</param-name>

? ? ? ? ? ? <!-- 此處配置的是 cas server 登錄地址 -->

? ? ? ? ? ? <param-value>https://www.fanlychie.com:8443/cas/login</param-value>

? ? ? ? </init-param>

? ? ? ? <init-param>

? ? ? ? ? ? <param-name>serverName</param-name>

? ? ? ? ? ? <!-- 此處配置的是當(dāng)前項(xiàng)目地址, 且必須使用 https 服務(wù), 否則 cas server 無法認(rèn)證 -->

? ? ? ? ? ? <param-value>https://www.fanlychie.com:8081</param-value>

? ? ? ? </init-param>

? ? </filter>

? ? <filter-mapping>

? ? ? ? <filter-name>CAS Authentication Filter</filter-name>

? ? ? ? <url-pattern>/*</url-pattern>

? ? </filter-mapping>

? ? <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>

? ? ? ? ? ? <!-- 此處配置的是 cas server 地址 -->

? ? ? ? ? ? <param-value>https://www.fanlychie.com:8443/cas</param-value>

? ? ? ? </init-param>

? ? ? ? <init-param>

? ? ? ? ? ? <param-name>serverName</param-name>

? ? ? ? ? ? <!-- 此處配置的是當(dāng)前項(xiàng)目地址, 且必須使用 https 服務(wù), 否則 cas server 無法認(rèn)證 -->

? ? ? ? ? ? <param-value>https://www.fanlychie.com:8081</param-value>

? ? ? ? </init-param>

? ? </filter>

? ? <filter-mapping>

? ? ? ? <filter-name>CAS Validation Filter</filter-name>

? ? ? ? <url-pattern>/*</url-pattern>

? ? </filter-mapping>

? ? <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>

? ? <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>

? ? <welcome-file-list>

? ? ? ? <welcome-file>index.jsp</welcome-file>

? ? </welcome-file-list>

</web-app>

以上是 cas client 標(biāo)準(zhǔn)配置,具體信息可參考 https://github.com/apereo/java-cas-client。

cas-client2 配置基本與 cas-client1 配置相同,詳情可見 demo,同時(shí)啟動(dòng)這兩個(gè)項(xiàng)目

cas-client1 - https://www.fanlychie.com:8081

cas-client2 - https://www.fanlychie.com:8082

訪問其中的一個(gè)項(xiàng)目 https://www.fanlychie.com:8081,會(huì)自動(dòng)跳到

https://www.fanlychie.com:8443/cas/login?service=https%3A%2F%2Fwww.fanlychie.com%3A8081%2F。

由于還沒有登錄過 CAS 認(rèn)證系統(tǒng),CAS 認(rèn)證系統(tǒng)攔截到你的訪問,進(jìn)入到認(rèn)證系統(tǒng)登錄界面,當(dāng)?shù)卿洺晒?,CAS 服務(wù)會(huì)跳轉(zhuǎn)向到你剛剛訪問的地址。

當(dāng)你訪問 https://www.fanlychie.com:8082,此時(shí)是不需要登錄了的。

至此,CAS 實(shí)現(xiàn) SSO 單點(diǎn)登錄系統(tǒng)搭建結(jié)束。

擴(kuò)展閱讀

掃碼登錄實(shí)現(xiàn)原理

單點(diǎn)登錄原理與簡單實(shí)現(xiàn)

單點(diǎn)登錄終極方案之 CAS 應(yīng)用及原理

來源:http://fanlychie.github.io/post/java-cas-server-client-configure.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容