從零開始學CAS單點登錄——實例Demo
什么是單點登錄
單點登錄(Single Sign On),簡稱為 SSO,是目前比較流行的企業業務整合的解決方案之一。SSO的定義是在多個應用系統中,用戶只需要登錄一次就可以訪問所有相互信任的應用系統。
我們目前的系統存在諸多子系統,而這些子系統是分別部署在不同的服務器中,那么使用傳統方式的session是無法解決的,我們需要使用相關的單點登錄技術來解決。
什么是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 最基本的協議過程:
SSO單點登錄訪問流程主要有以下步驟:
1. 訪問服務:SSO客戶端發送請求訪問應用系統提供的服務資源。
2. 定向認證:SSO客戶端會重定向用戶請求到SSO服務器。
3. 用戶認證:用戶身份認證。
4. 發放票據:SSO服務器會產生一個隨機的Service Ticket。
5. 驗證票據:SSO服務器驗證票據Service Ticket的合法性,驗證通過后,允許客戶端訪問服務。
6. 傳輸用戶信息:SSO服務器驗證票據通過后,傳輸用戶認證結果信息給客戶端。
一 在Linux里的Tomcat部署CAS
1.進入路徑
[root@hostname /]# cd usr/local/cas
2.把提前准備好的cas.war 復制到我們所使用的路徑中
[root@hostname cas]# cp /root/cas/cas.war ./
3.將准備好的tomcat 放入這個文件夾中 並且解壓 更名為tomcat
[root@hostname /]# cp /root/tomcat/apache-tomcat-7.0.47.tar.gz ./
[root@hostname cas]# tar -zxvf apache-tomcat-7.0.47.tar.gz -C ./
[root@hostname cas]# mv apache-tomcat-7.0.47.tar.gz tomcat
4.打開EditPlus 3 利用FTP遠程打開文件進行更改配置文件
修改配置文件/usr/local/cas/tomcat/conf/server.xml (三處地方需要更改 原因是端口號沖突)
1.<Server port="8010" shutdown="SHUTDOWN">
2. <Connector port="9100" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
3. <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
5.我們將復制過來的cas.war包部署到tomcat中(移動到webapps包中 啟動tomcat 關閉tomcat 然后刪除webapps中的cas.war)
注意一定要關閉tomcat才刪除cas.war
[root@hostname cas]# mv cas.log tomcat/webapps/
[root@hostname cas]# sh tomcat/bin/startup.sh
[root@hostname cas]# sh tomcat/bin/shutdown.sh
[root@hostname webapps]# rm -rf cas.war
二 修改相關配置文件
更改配置文件
1.
/usr/local/cas/tomcat/webapps/cas/WEB-INF/cas.properties
server.name=http://192.168.200.128:9100 (將ip改為我們虛擬機的ip 端口號改為之前修改的端口號9100)
2.
/usr/local/cas/tomcat/webapps/cas/WEB-INF/deployerConfigContext.xml
(代碼中增加一串代碼p:requireSecure="false")
class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient" p:requireSecure="false"/>
3.
/usr/local/cas/tomcat/webapps/cas/WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml
p:cookieSecure 修改為false p:cookieMaxAge修改為3600
<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"
p:cookieMaxAge="3600"
p:cookieName="CASTGC"
p:cookiePath="/cas" />
</beans>
/usr/local/cas/tomcat/webapps/cas/WEB-INF/spring-configuration/warnCookieGenerator.xml 同理修改
三 啟動tomcat 訪問192.168.200.128:9100/cas/login
用戶名:casuser 密碼:Mellon 登錄即可
項目Demo
我們創建兩個項目來進行測試cas單點登錄系統
casDemo01項目:
pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <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> </dependency> </dependencies>
web.xml
<?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"> <!--用於單點退出--> <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> <param-value>http://192.168.200.128:9100/cas/login</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://localhost:9002</param-value> </init-param> </filter> <filter-mapping> <filter-name>CASFilter</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> <param-value>http://192.168.200.128:9100/cas</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://localhost:9002</param-value> </init-param> </filter> <filter-mapping> <filter-name>CAS Validation Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--負責實現request請求的--> <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> <!--開發者可以通過ssertionThreadLocalFilter獲取用戶名.AssertionThread.getAsssration.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
<html> <body> <h2>Hello World!</h2> this is demo01 </body> </html>
casDemo0項目: pom.xml與demo01相同
web.xml
<?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"> <!--用於單點退出--> <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> <param-value>http://192.168.200.128:9100/cas/login</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>CASFilter</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> <param-value>http://192.168.200.128: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> <!--負責實現request請求的--> <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> <!--開發者可以通過ssertionThreadLocalFilter獲取用戶名.AssertionThread.getAsssration.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
<html> <body> <h2>Hello World!</h2> this is casDemo0 </body> </html>
結果:
當我們訪問demo0進行登錄的時候 再次訪問demo01就不需要進行登錄可以直接訪問
cas與mysql數據庫遠程連接:
配置cas集成數據庫,實現動態賬號密碼
1、開啟mysql數據庫的root賬號的遠程連接權限
USR MYSQL;
UPDATE USER SET HOST='%' WHERE USER='root';
FLUSH PRIVILEGES;
2、在配置文件添加如下配置
1、修改配置文件
cas/WEB-INF/deployerConfigContext.xml
新增如下配置:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://192.168.188.1:3306/youlexuandb?characterEncoding=utf8"
p:user="root"
p:password="123" />
<bean id="passwordEncoder"
class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"
c:encodingAlgorithm="MD5"
p:characterEncoding="UTF-8" />
<bean id="dbAuthHandler"
class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"
p:dataSource-ref="dataSource"
p:sql="select password from tb_user where username = ?"
p:passwordEncoder-ref="passwordEncoder"/>
2、找到bean id 為authenticationManager
修改其中的:<entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />
改成:<entry key-ref="dbAuthHandler" value-ref="primaryPrincipalResolver"/>
3、上傳相關jar包到cas/WEB-INF/lib目錄
c3p0-0.9.1.2.jar
cas-server-support-jdbc-4.0.0.jar
mysql-connector-java-5.1.32.jar
4、重啟cas所在tomcat服務器
最后就可以直接用數據庫的數據進行cas登錄
ps:我在運行過程中遇到一個錯誤 登錄時一直在響應最后
頁面反應是:CAS is Unavailable
There was an error trying to complete your request. Please notify your support desk or try again.
有可能是防火牆的原因 我將防火牆關閉 重啟就可以了