第一步 部署CAS-Server(服務端)html
1.從CAS官方網站(http://developer.jasig.org/cas/)下載最新版本的CAS-Server(當前最新版本cas-server-4.0.0-release.zip),將其解壓,找到modules/cas-server-webapp-3.5.2.war,複製到本地tomcat下的webapps下,並重命名爲cas.war(能夠是其餘名稱),啓動tomcat,在webapps下生成了名爲cas的web項目。java
2.CAS默認使用https協議通訊,須要tomcat配置SSL協議(這一部分會在之後詳細介紹)。 可是因爲通常項目不須要這麼高的安全級別, 爲了簡化操做,將使用http協議通訊。web
打開webapps\cas\WEB-INF\spring-configuration\warnCookieGenerator.xml,找到以下配置,將p:cookieSecure="true"改成p:cookieSecure="false"spring
打開webapps\cas\WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml ,找到以下配置,將p:cookieSecure="true"改成p:cookieSecure="false"瀏覽器
打開webapps\cas\WEB-INF\deployerConfigContext.xml 文件 ,找到以下配置,將p:requireSecure="false"添加到下圖標記的位置tomcat
3.從新啓動tomcat,訪問 http://localhost:8085/cas 則能夠看到以下登錄界面。安全
至於下圖所顯示的錯誤Non-secure Connection,是因爲沒有使用HTTPS協議的關係,而默認的登錄界面有對此進行驗證的代碼,而在實際項目中的登錄界面通常須要本身寫,經過修改webapps\cas\WEB-INF\view\jsp\default\ui下的casLoginView.jsp便可。將下圖所示代碼刪掉便可去除錯誤警告。cookie
4.CAS-Server的默認驗證規則:只要用戶名和密碼相同就認證經過(僅僅用於測試,生成環境須要根據實際狀況修改,如何更改認證規則會在之後詳細介紹),輸入admin/admin 點擊登陸,就能夠看到登陸成功的頁面。至此CAS服務端配置完成。session
第二步 部署CAS-Client(客戶端)app
1.從網上下載對應CAS-Server版本的CAS-Client(當前對應版本cas-client-3.2.1-release.zip), 解壓提取cas-client-3.2.1/modules/cas-client-core-3.2.1.jar,複製到實際web項目的lib下。
2.配置web.xml文件,添加如下代碼
<!-- ======================== 單點登陸開始 ======================== --> <!-- 用於單點退出,該過濾器用於實現單點登出功能,可選配置--> <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>CAS Filter</filter-name> <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class> <init-param> <param-name>casServerLoginUrl</param-name> <param-value>http://127.0.0.1:8080/cas/login</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://127.0.0.1:8080</param-value> </init-param> </filter> <filter-mapping> <filter-name>CAS Filter</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://127.0.0.1:8080/cas</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://127.0.0.1:8080</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> <!-- ======================== 單點登陸結束 ======================== -->
3.模擬兩個子系統。
首先,建立兩個servlet(App1.java和App2.java)。
App1.java代碼
package servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class App1 extends HttpServlet { private static final long serialVersionUID = -6593274907821061823L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("App1"); } }
App2.java代碼
package servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class App2 extends HttpServlet { private static final long serialVersionUID = -6593274907821061823L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("App2"); } }
最終的web.xml代碼
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- ======================== 單點登陸開始 ======================== --> <!-- 用於單點退出,該過濾器用於實現單點登出功能,可選配置--> <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>CAS Filter</filter-name> <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class> <init-param> <param-name>casServerLoginUrl</param-name> <param-value>http://127.0.0.1:8080/cas/login</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://127.0.0.1:8080</param-value> </init-param> </filter> <filter-mapping> <filter-name>CAS Filter</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://127.0.0.1:8080/cas</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://127.0.0.1:8080</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> <!-- ======================== 單點登陸結束 ======================== --> <servlet> <display-name>App1</display-name> <servlet-name>App1</servlet-name> <servlet-class>servlet.App1</servlet-class> </servlet> <servlet> <display-name>App2</display-name> <servlet-name>App2</servlet-name> <servlet-class>servlet.App2</servlet-class> </servlet> <servlet-mapping> <servlet-name>App1</servlet-name> <url-pattern>/App1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>App2</servlet-name> <url-pattern>/App2</url-pattern> </servlet-mapping> </web-app>
4.添加jar依賴包commons-logging.jar(日誌工具),若是不添加會報錯誤,可是沒有測試是否影響正常功能的使用。
第三步 測試SSO
1.瀏覽器中輸入地址http://127.0.0.1:8080/SSO_CAS/App1(SSO_CAS爲web項目名稱),跳轉到CAS-Server的默認登錄界面中,輸入admin/admin,點擊登錄,顯示App1。
2.瀏覽器中輸入地址http://127.0.0.1:8080/SSO_CAS/App2,則跳過登錄驗證,直接顯示App2。
到此已經所有完成了基於CAS的單點登陸實例演示。