CAS:全稱爲Central Authentication Service即中央認證服務,是一個企業多語言單點登錄的解決方案。
環境
操作系統:CentOS7.7
服務器IP:192.168.142.155(此服務器會作爲CAS服務端)
服務器上已安裝Web容器Tomcat:/data/soft/apache-tomcat-7.0.52
構件工具:Maven
IDE:Eclipse
場景描述(模擬單點登錄場景)
- 通過瀏覽器分別訪問兩個系統的首頁,系統會自動跳轉到單點登錄頁,登錄成功後再跳轉回各自系統的首頁並顯示當前登錄的用戶。再次訪問首頁,可以直接跳轉到首頁,不再需要認證。最後,在首頁點擊退出登錄後,可跳轉到CAS指定的頁面。
搭建CAS服務端
-
下載安裝包:https://github.com/apereo/cas/releases/tag/v4.0.0
-
在其路徑下找到:cas-server-4.0.0-release\cas-server-4.0.0\modules\cas-server-webapp-4.0.0.war 即CAS的服務端,將其改名爲cas.war,並上傳至服務器,部署到Tomcat上
-
本地瀏覽器訪問:http://192.168.142.155:8080/cas/login,可看到登錄頁面。輸入賬號:casuser 密碼:Mellon,成功登陸
-
取消https認證(本案例客戶端是用的http訪問)
修改文件/data/soft/apache-tomcat-7.0.52/webapps/cas/WEB-INF/deployerConfigContext.xml,
添加p:requireSecure=“false”(這個屬性是新增的,默認沒有)<bean id="proxyAuthenticationHandler" class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler" p:httpClient-ref="httpClient" p:requireSecure="false"/>
-
設置cookie的保存時間爲3600秒(即不會因爲關閉瀏覽器而要求再次登錄)
修改文件:/data/soft/apache-tomcat-7.0.52/webapps/cas/WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml
其中,屬性p:cookieMaxAge,由p:cookieMaxAge="-1"改爲了p:cookieMaxAge=“3600”<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator" p:cookieSecure="true" p:cookieMaxAge="3600" p:cookieName="CASTGC" p:cookiePath="/cas" />
-
支持指定退出頁:
修改配置文件:/data/soft/apache-tomcat-7.0.52/webapps/cas/WEB-INF/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}"/>
-
重啓Tomcat
搭建CAS客戶端1
-
新建一個Maven工程,工程類型爲war,工程名爲casclient_demo1
-
添加cas客戶端的依賴及tomcat插件:
pom.xml:<dependencies> <!-- cas客戶端 --> <dependency> <groupId>org.jasig.cas.client</groupId> <artifactId>cas-client-core</artifactId> <version>3.3.3</version> </dependency> <!-- 這是解析jsp頁面需要用到的 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <!-- 指定端口 --> <port>9001</port> <!-- 請求路徑 --> <path>/</path> </configuration> </plugin> </plugins> </build>
-
創建客戶端1的首頁
index.jsp(即tomcat的默認主頁,爲了方便直接訪問):<%@ 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>demo1</title> </head> <body> 歡迎來到demo1 <%=request.getRemoteUser()%> <a href="http://192.168.142.155:8080/cas/logout?service=http://www.baidu.com">退出登錄</a> </body> </html>
-
修改web.xml,添加由CAS來實現的一系列過濾器
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.142.155:8080/cas/login</param-value> <!--這裏的server是服務端的IP --> </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> <!-- 該過濾器負責對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://192.168.142.155:8080/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>
搭建CAS客戶端2
-
新建一個Maven工程,工程類型爲war,工程名爲casclient_demo2
-
修改pom文件:參照CAS客戶端1,tomcat運行端口爲9002.
-
創建客戶端2的首頁
<%@ 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>demo2</title> </head> <body> 歡迎來到demo2 <%=request.getRemoteUser()%> <a href="http://192.168.142.155:8080/cas/logout?service=http://www.baidu.com">退出登錄</a> </body> </html>
-
修改web.xml,參照CAS客戶端1,在此基礎上只有這裏需要修改:
<init-param> <!-- 這是當前客戶端的服務器名 --> <param-name>serverName</param-name> <param-value>http://localhost:9002</param-value> </init-param>
運行客戶端,測試單點登錄
-
右鍵客戶端工程:Run as ->Maven builder,依次啓動客戶端1和客戶端2
-
瀏覽器訪問:http://localhost:9001/,跳轉到了cas的登錄頁,登錄成功後,才跳轉到了demo1首頁。
-
瀏覽器再次訪問:http://localhost:9001/,已經不需要認知,直接跳轉到了首頁
-
瀏覽器訪問:http://localhost:9002/,跳轉到了cas的登錄頁,登錄成功後,跳轉到了demo2首頁。
異常
- 沒有取消https,報錯: