1.1 什么是CAS
CAS 是 Yale 大學(xué)發(fā)起的一個開源項目臊旭,旨在為 Web 應(yīng)用系統(tǒng)提供一種可靠的單點登錄方法落恼,CAS 在 2004 年 12 月正式成為 JA-SIG 的一個項目。CAS 具有以下特點:
【1】開源的企業(yè)級單點登錄解決方案离熏。
【2】CAS Server 為需要獨立部署的 Web 應(yīng)用佳谦。
【3】CAS Client 支持非常多的客戶端(這里指單點登錄系統(tǒng)中的各個 Web 應(yīng)用),包括 Java, .Net, PHP, Perl, Apache, uPortal, Ruby 等滋戳。
從結(jié)構(gòu)上看钻蔑,CAS 包含兩個部分: CAS Server 和 CAS Client。CAS Server 需要獨立部署胧瓜,主要負責(zé)對用戶的認證工作府喳;CAS Client 負責(zé)處理對客戶端受保護資源的訪問請求,需要登錄時兜粘,重定向到 CAS Server孔轴。下圖是 CAS 最基本的協(xié)議過程:
SSO單點登錄訪問流程主要有以下步驟:
- 訪問服務(wù):SSO客戶端發(fā)送請求訪問應(yīng)用系統(tǒng)提供的服務(wù)資源路鹰。
- 定向認證:SSO客戶端會重定向用戶請求到SSO服務(wù)器。
- 用戶認證:用戶身份認證优构。
- 發(fā)放票據(jù):SSO服務(wù)器會產(chǎn)生一個隨機的Service Ticket钦椭。
- 驗證票據(jù):SSO服務(wù)器驗證票據(jù)Service Ticket的合法性彪腔,驗證通過后,允許客戶端訪問服務(wù)德挣。
- 傳輸用戶信息:SSO服務(wù)器驗證票據(jù)通過后盲厌,傳輸用戶認證結(jié)果信息給客戶端祸泪。
1.2 CAS服務(wù)端配置
1.2.1端口修改
如果我們不希望用8080端口訪問CAS, 可以修改端口
(1)修改TOMCAT的端口
打開tomcat 目錄 conf\server.xml 找到下面的配置
將端口8080懂扼,改為28083
(1)修改CAS配置文件
修改cas的WEB-INF/cas.properties
1.2.2去除https認證
CAS默認使用的是HTTPS協(xié)議阀湿,如果使用HTTPS協(xié)議需要SSL安全證書(需向特定的機構(gòu)申請和購買) 瑰妄。如果對安全要求不高或是在開發(fā)測試階段间坐,可使用HTTP協(xié)議。我們這里講解通過修改配置地技,讓CAS使用HTTP協(xié)議。
1)修改cas的WEB-INF/deployerConfigContext.xml
找到下面的配置
<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient"/>
這里需要增加參數(shù)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" />
參數(shù)p:cookieSecure=“true”,同理為HTTPS驗證相關(guān)伊诵,TRUE為采用HTTPS驗證曹宴,F(xiàn)ALSE為不采用https驗證歉提。
參數(shù)p:cookieMaxAge=“-1”排龄,是COOKIE的最大生命周期,-1為無生命周期,即只在當前打開的窗口有效悼尾,關(guān)閉或重新打開其它窗口闺魏,仍會要求驗證∷静荩可以根據(jù)需要修改為大于0的數(shù)字翻伺,比如3600等,意思是在3600秒內(nèi)拉宗,打開任意窗口旦事,都不需要驗證急灭。
我們這里將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.3 CAS客戶端入門小Demo
1.3.1客戶端工程1搭建
1)搭建工程引入依賴
創(chuàng)建Maven工程 (war)casclient-demo1 引入cas客戶端依賴,tomcat運行端口28084
<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>
2)添加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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>casclient-demo1</display-name>
<!-- 用于單點退出卖鲤,該過濾器用于實現(xiàn)單點登出功能畴嘶,可選配置 -->
<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>
<!-- 該過濾器用于實現(xiàn)單點登出功能区匣,可選配置蒋院。 -->
<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>
<!-- 該過濾器負責(zé)用戶的認證工作姑丑,必須啟用它 -->
<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://localhost:28083/cas/login</param-value>
<!--這里的server是服務(wù)端的IP -->
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://localhost:28084</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CASFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 該過濾器負責(zé)對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:28083/cas</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://localhost:28084</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 該過濾器負責(zé)實現(xiàn)HttpServletRequest請求的包裹, 比如允許開發(fā)者通過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>
<!-- 該過濾器使得開發(fā)者可以通過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>
<welcome-file-list>
<welcome-file>index.shtml</welcome-file>
</welcome-file-list>
</web-app>
3)編寫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>一品優(yōu)購</title>
</head>
<body>
歡迎來到一品優(yōu)購
<%=request.getRemoteUser()%>
</body>
</html>
request.getRemoteUser()為獲取遠程登錄名
1.3.2客戶端工程2搭建
(1)創(chuàng)建Maven工程 (war)casclient-demo2 引入cas客戶端依賴并制定tomcat運行端口為28085
(2)創(chuàng)建web.xml,參照casclient-demo1 ,將serverName的值改為http://localhost:28085啦租,一共兩處
(3)創(chuàng)建index.jsp ,內(nèi)容顯示“歡迎來到二品優(yōu)購”
1.3.3單點登錄測試
(1)啟動cas部署的tomcat
(2)啟動客戶端工程1和客戶端工程2
(3)地址欄輸入http://localhost:28084/ 和http://localhost:28085/ 篷角,地址均會跳轉(zhuǎn)到CAS登錄頁
(4)輸入用戶名和密碼后系任,頁面跳轉(zhuǎn)回28084 阱缓,再次訪問28085也可以打開主頁面举农。
1.3.4單點退出登錄
地址欄輸入 http://localhost:28083/cas/logout
即可看到退出后的提示頁面
我們可以將這個鏈接添加到index.jsp中
<a href="http://localhost:28083/cas/logout">退出登錄</a>
但我們更希望退出登錄后,能自動跳轉(zhuǎn)到某個頁面敞嗡,那如何處理呢颁糟?
修改cas系統(tǒng)的配置文件cas-servlet.xml
<bean id="logoutAction" class="org.jasig.cas.web.flow.LogoutAction"
p:servicesManager-ref="servicesManager"
p:followServiceRedirects="${cas.logout.followServiceRedirects:true}"/>
改為true后,可以在退出時跳轉(zhuǎn)頁面到目標頁面喉悴,修改index.jsp的退出鏈接
<a href="http://localhost:28083/cas/logout?service=http://www.itheima.com">退出登錄</a>