SSO之CAS單點登錄實例演示
本文目錄:
- 一、概述
- 二、演示環境
- 三、JDK安裝配置
- 四、安全證書配置
- 五、部署CAS-Server相關的Tomcat
- 六、部署CAS-Client相關的Tomcat
- 七、 測試驗證SSO
一、概述
此文的目的就是為了幫助初步接觸SSO和CAS 的人員提供一個入門指南,一步一步演示如何實現基於CAS的單點登錄。
CAS的官網:http://www.jasig.org/cas
二、演示環境
本文演示過程在同一個機器上的(也可以在三台實體機器或者三個的虛擬機上),環境如下:
- windows7 64位,主機名稱:michael-pc
- JDK 1.6.0_18
- Tomcat 6.0.29
- CAS-server-3.4.11、CAS-client-3.2.1
127.0.0.1 demo.micmiu.com 127.0.0.1 app1.micmiu.com 127.0.0.1 app2.micmiu.com
- demo.micmiu.com =>> 對應部署cas server的tomcat,這個虛擬域名還用於證書生成
- app1.micmiu.com =>> 對應部署app1 的tomcat
- app2.micmiu.com =>> 對應部署app2 的tomcat
三、JDK安裝配置
這個詳細過程就不在描述,如果是免安裝版的,確保環境變量配置正確。
本機環境變量:JAVA_HOME=D:\jdk,如果看到以下信息則表示安裝成功:
四、安全證書配置
有關keytool工具的詳細運用見:http://www.micmiu.com/lang/java/keytool-start-guide/
4.1. 生成證書:
1 |
keytool -genkey -alias ssodemo -keyalg RSA -keysize 1024 -keypass michaelpwd -validity 365 -keystore g:\sso\ssodemo.keystore -storepass michaelpwd |
ps:
- 截圖中需要輸入的姓名和上面hosts文件中配置的一致;
- keypass 和 storepass 兩個密碼要一致,否則下面tomcat 配置https 訪問失敗;
4.2.導出證書:
1 |
keytool -export -alias ssodemo -keystore g:\sso\ssodemo.keystore -fileg:\sso\ssodemo.crt -storepass michaelpwd |
4.3.客戶端導入證書:
1 |
keytool -import -keystore %JAVA_HOME%\jre\lib\security\cacerts -fileg:\sso\ssodemo.crt -alias ssodemo |
ps:該命令中輸入的密碼和上面輸入的不是同一個密碼;如果是多台機器演示,需要在每一台客戶端導入該證書。
五、部署CAS-Server相關的Tomcat
5.1. 配置HTTPS
解壓apache-tomcat-6.0.29.tar.gz並重命名后的路徑為 G:\sso\tomcat-cas,在文件 conf/server.xml文件找到:
1 |
<!-- |
2 |
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" |
3 |
maxThreads="150" scheme="https" secure="true" |
4 |
clientAuth="false" sslProtocol="TLS" /> |
5 |
--> |
修改成如下:
1 |
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" |
2 |
maxThreads="150" scheme="https" secure="true" |
3 |
keystoreFile="g:/sso/ssodemo.keystore" keystorePass="michaelpwd" |
4 |
clientAuth="false" sslProtocol="TLS" URIEncoding="UTF-8" |
參數說明:
- keystoreFile 就是4.1中創建證書的路徑
- keystorePass 就是4.1中創建證書的密碼
5.2. 驗證HTTPS配置
其他按照默認配置不作修改,雙擊%TOMCAT_HOME%\bin\startup.bat 啟動tomcat-cas 驗證https訪問配置:
5.3 部署CAS-Server
CAS-Server 下載地址:http://www.jasig.org/cas/download
本文以cas-server-3.4.11-release.zip 為例,解壓提取cas-server-3.4.11/modules/cas-server-webapp-3.4.11.war文件,把改文件copy到 G:\sso\tomcat-cas\webapps\ 目下,並重命名為:cas.war.
啟動tomcat-cas,在瀏覽器地址欄輸入:https://demo.micmiu.com:8443/cas/login ,回車
CAS-server的默認驗證規則:只要用戶名和密碼相同就認證通過(僅僅用於測試,生成環境需要根據實際情況修改),輸入admin/admin 點擊登錄,就可以看到登錄成功的頁面:
看到上述頁面表示CAS-Server已經部署成功。
六、部署CAS-Client相關的Tomcat
6.1Cas-Client 下載
CAS-Client 下載地址:http://downloads.jasig.org/cas-clients/
以cas-client-3.2.1-release.zip 為例,解壓提取cas-client-3.2.1/modules/cas-client-core-3.2.1.jar
借以tomcat默認自帶的 webapps\examples 作為演示的簡單web項目
6.2 安裝配置 tomcat-app1
解壓apache-tomcat-6.0.29.tar.gz並重命名后的路徑為 G:\sso\tomcat-app1,修改tomcat的啟動端口,在文件conf/server.xml文件找到如下內容:
1 |
<Connector port="8080" protocol="HTTP/1.1" |
2 |
connectionTimeout="20000" |
3 |
redirectPort="8443" /> |
4 |
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> |
修改成如下:
1 |
<Connector port="18080" protocol="HTTP/1.1" |
2 |
connectionTimeout="20000" |
3 |
redirectPort="18443" /> |
4 |
<Connector port="18009" protocol="AJP/1.3" redirectPort="18443" /> |
啟動tomcat-app1,瀏覽器輸入 http://app1.micmiu.com:18080/examples/servlets/ 回車:
看到上述界面表示tomcat-app1的基本安裝配置已經成功。
接下來復制 client的lib包cas-client-core-3.2.1.jar到 tomcat-app1\webapps\examples\WEB-INF\lib\目錄下, 在tomcat-app1\webapps\examples\WEB-INF\web.xml 文件中增加如下內容:
1 |
<!-- ======================== 單點登錄開始 ======================== --> |
2 |
<!-- 用於單點退出,該過濾器用於實現單點登出功能,可選配置--> |
3 |
<listener> |
4 |
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class> |
5 |
</listener> |
6 |
7 |
<!-- 該過濾器用於實現單點登出功能,可選配置。 --> |
8 |
<filter> |
9 |
<filter-name>CAS Single Sign Out Filter</filter-name> |
10 |
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class> |
11 |
</filter> |
12 |
<filter-mapping> |
13 |
<filter-name>CAS Single Sign Out Filter</filter-name> |
14 |
<url-pattern>/*</url-pattern> |
15 |
</filter-mapping> |
16 |
17 |
<filter> |
18 |
<filter-name>CAS Filter</filter-name> |
19 |
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class> |
20 |
<init-param> |
21 |
<param-name>casServerLoginUrl</param-name> |
22 |
<param-value>https://demo.micmiu.com:8443/cas/login</param-value> |
23 |
</init-param> |
24 |
<init-param> |
25 |
<param-name>serverName</param-name> |
26 |
<param-value>http://app1.micmiu.com:18080</param-value> |
27 |
</init-param> |
28 |
</filter> |
29 |
<filter-mapping> |
30 |
<filter-name>CAS Filter</filter-name> |
31 |
<url-pattern>/*</url-pattern> |
32 |
</filter-mapping> |
33 |
<!-- 該過濾器負責對Ticket的校驗工作,必須啟用它 --> |
34 |
<filter> |
35 |
<filter-name>CAS Validation Filter</filter-name> |
36 |
<filter-class> |
37 |
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class> |
38 |
<init-param> |
39 |
<param-name>casServerUrlPrefix</param-name> |
40 |
<param-value>https://demo.micmiu.com:8443/cas</param-value> |
41 |
</init-param> |
42 |
<init-param> |
43 |
<param-name>serverName</param-name> |
44 |
<param-value>http://app1.micmiu.com:18080</param-value> |
45 |
</init-param> |
46 |
</filter> |
47 |
<filter-mapping> |
48 |
<filter-name>CAS Validation Filter</filter-name> |
49 |
<url-pattern>/*</url-pattern> |
50 |
</filter-mapping> |
51 |
52 |
<!-- |
53 |
該過濾器負責實現HttpServletRequest請求的包裹, |
54 |
比如允許開發者通過HttpServletRequest的getRemoteUser()方法獲得SSO登錄用戶的登錄名,可選配置。 |
55 |
--> |
56 |
<filter> |
57 |
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> |
58 |
<filter-class> |
59 |
org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class> |
60 |
</filter> |
61 |
<filter-mapping> |
62 |
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> |
63 |
<url-pattern>/*</url-pattern> |
64 |
</filter-mapping> |
65 |
66 |
<!-- |
67 |
該過濾器使得開發者可以通過org.jasig.cas.client.util.AssertionHolder來獲取用戶的登錄名。 |
68 |
比如AssertionHolder.getAssertion().getPrincipal().getName()。 |
69 |
--> |
70 |
<filter> |
71 |
<filter-name>CAS Assertion Thread Local Filter</filter-name> |
72 |
<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class> |
73 |
</filter> |
74 |
<filter-mapping> |
75 |
<filter-name>CAS Assertion Thread Local Filter</filter-name> |
76 |
<url-pattern>/*</url-pattern> |
77 |
</filter-mapping> |
78 |
79 |
<!-- ======================== 單點登錄結束 ======================== --> |
有關cas-client的web.xml修改的詳細說明見官網介紹:
https://wiki.jasig.org/display/CASC/Configuring+the+JA-SIG+CAS+Client+for+Java+in+the+web.xml
6.3 安裝配置 tomcat-app2
解壓apache-tomcat-6.0.29.tar.gz並重命名后的路徑為 G:\sso\tomcat-app2,修改tomcat的啟動端口,在文件 conf/server.xml文件找到如下內容:
1 |
<Connector port="8080" protocol="HTTP/1.1" |
2 |
connectionTimeout="20000" |
3 |
redirectPort="8443" /> |
4 |
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> |
修改成如下:
1 |
<Connector port="28080" protocol="HTTP/1.1" |
2 |
connectionTimeout="20000" |
3 |
redirectPort="28443" /> |
4 |
<Connector port="28009" protocol="AJP/1.3" redirectPort="28443" /> |
啟動tomcat-app2,瀏覽器輸入 http://app2.micmiu.com:28080/examples/servlets/ 回車,按照上述6.2中的方法驗證是否成功。
同6.2中的復制 client的lib包cas-client-core-3.2.1.jar到 tomcat-app2\webapps\examples\WEB-INF\lib\目錄下, 在tomcat-app2\webapps\examples\WEB-INF\web.xml 文件中增加如下內容:
1 |
<!-- ======================== 單點登錄開始 ======================== --> |
2 |
<!-- 用於單點退出,該過濾器用於實現單點登出功能,可選配置--> |
3 |
<listener> |
4 |
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class> |
5 |
</listener> |
6 |
7 |
<!-- 該過濾器用於實現單點登出功能,可選配置。 --> |
8 |
<filter> |
9 |
<filter-name>CAS Single Sign Out Filter</filter-name> |
10 |
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class> |
11 |
</filter> |
12 |
<filter-mapping> |
13 |
<filter-name>CAS Single Sign Out Filter</filter-name> |
14 |
<url-pattern>/*</url-pattern> |
15 |
</filter-mapping> |
16 |
17 |
<filter> |
18 |
<filter-name>CAS Filter</filter-name> |
19 |
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class> |
20 |
<init-param> |
21 |
<param-name>casServerLoginUrl</param-name> |
22 |
<param-value>https://demo.micmiu.com:8443/cas/login</param-value> |
23 |
</init-param> |
24 |
<init-param> |
25 |
<param-name>serverName</param-name> |
26 |
<param-value>http://app2.micmiu.com:28080</param-value> |
27 |
</init-param> |
28 |
</filter> |
29 |
<filter-mapping> |
30 |
<filter-name>CAS Filter</filter-name> |
31 |
<url-pattern>/*</url-pattern> |
32 |
</filter-mapping> |
33 |
<!-- 該過濾器負責對Ticket的校驗工作,必須啟用它 --> |
34 |
<filter> |
35 |
<filter-name>CAS Validation Filter</filter-name> |
36 |
<filter-class> |
37 |
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class> |
38 |
<init-param> |
39 |
<param-name>casServerUrlPrefix</param-name> |
40 |
<param-value>https://demo.micmiu.com:8443/cas</param-value> |
41 |
</init-param> |
42 |
<init-param> |
43 |
<param-name>serverName</param-name> |
44 |
<param-value>http://app2.micmiu.com:28080</param-value> |
45 |
</init-param> |
46 |
</filter> |
47 |
<filter-mapping> |
48 |
<filter-name>CAS Validation Filter</filter-name> |
49 |
<url-pattern>/*</url-pattern> |
50 |
</filter-mapping> |
51 |
52 |
<!-- |
53 |
該過濾器負責實現HttpServletRequest請求的包裹, |
54 |
比如允許開發者通過HttpServletRequest的getRemoteUser()方法獲得SSO登錄用戶的登錄名,可選配置。 |
55 |
--> |
56 |
<filter> |
57 |
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> |
58 |
<filter-class> |
59 |
org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class> |
60 |
</filter> |
61 |
<filter-mapping> |
62 |
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> |
63 |
<url-pattern>/*</url-pattern> |
64 |
</filter-mapping> |
65 |
66 |
<!-- |
67 |
該過濾器使得開發者可以通過org.jasig.cas.client.util.AssertionHolder來獲取用戶的登錄名。 |
68 |
比如AssertionHolder.getAssertion().getPrincipal().getName()。 |
69 |
--> |
70 |
<filter> |
71 |
<filter-name>CAS Assertion Thread Local Filter</filter-name> |
72 |
<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class> |
73 |
</filter> |
74 |
<filter-mapping> |
75 |
<filter-name>CAS Assertion Thread Local Filter</filter-name> |
76 |
<url-pattern>/*</url-pattern> |
77 |
</filter-mapping> |
78 |
79 |
<!-- ======================== 單點登錄結束 ======================== --> |
七、 測試驗證SSO
啟動之前配置好的三個tomcat分別為:tomcat-cas、tomcat-app1、tomcat-app2.
7.1 基本的測試
預期流程: 打開app1 url —-> 跳轉cas server 驗證 —-> 顯示app1的應用 —-> 打開app2 url —-> 顯示app2應用 —-> 注銷cas server —-> 打開app1/app2 url —-> 重新跳轉到cas server 驗證.
打開瀏覽器地址欄中輸入:http://app1.micmiu.com:18080/examples/servlets/servlet/HelloWorldExample,回車:
地址欄中輸入:https://demo.micmiu.com:8443/cas/logout,回車顯示:
上述表示 認證注銷成功,此時如果再訪問 : http://app1.micmiu.com:18080/examples/servlets/servlet/HelloWorldExample 或 http://app2.micmiu.com:28080/examples/servlets/servlet/HelloWorldExample 需要重新進行認證。
7.2 獲取登錄用戶的信息
修改HelloWorldExample.java,重新編譯替換webapps\examples\WEB-INF\classes\HelloWorldExample.class文件,修改后的HelloWorldExample.java代碼如下:
1 |
import java.io.*; |
2 |
import java.util.*; |
3 |
import java.util.Map.Entry; |
4 |
5 |
import javax.servlet.*; |
6 |
import javax.servlet.http.*; |
7 |
8 |
import org.jasig.cas.client.authentication.AttributePrincipal; |
9 |
import org.jasig.cas.client.util.AbstractCasFilter; |
10 |
import org.jasig.cas.client.validation.Assertion; |
11 |
12 |
/** |
13 |
* The simplest possible servlet. |
14 |
* |
15 |
* @author James Duncan Davidson |
16 |
*/ |
17 |
18 |
public class HelloWorldExample extends HttpServlet { |
19 |
20 |
public void doGet(HttpServletRequest request, HttpServletResponse response) |
21 |
throws IOException, ServletException { |
22 |
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings", request |
23 |
.getLocale()); |
24 |
response.setContentType("text/html"); |
25 |
PrintWriter out = response.getWriter(); |
26 |
27 |
out.println("<html>"); |
28 |
out.println("<head>"); |
29 |
30 |
String title = rb.getString("helloworld.title"); |
31 |
32 |
out.println("<title>" + title + "</title>"); |
33 |
out.println("</head>"); |
34 |
out.println("<body bgcolor=\"white\">"); |
35 |
36 |
out.println("<a href=\"../helloworld.html\">"); |
37 |
out.println("<img src=\"../images/code.gif\" height=24 " |
38 |
+ "width=24 align=right border=0 alt=\"view code\"></a>"); |
39 |
out.println("<a href=\"../index.html\">"); |
40 |
out.println("<img src=\"../images/return.gif\" height=24 " |
41 |
+ "width=24 align=right border=0 alt=\"return\"></a>"); |
42 |
out.println("<h1>" + title + "</h1>"); |
43 |
44 |
Assertion assertion = (Assertion) request.getSession().getAttribute( |
45 |
AbstractCasFilter.CONST_CAS_ASSERTION); |
46 |
47 |
if (null != assertion) { |
48 |
out.println(" Log | ValidFromDate =:" |
49 |
+ assertion.getValidFromDate() + "<br>"); |
50 |
out.println(" Log | ValidUntilDate =:" |
51 |
+ assertion.getValidUntilDate() + "<br>"); |
52 |
Map<Object, Object> attMap = assertion.getAttributes(); |
53 |
out.println(" Log | getAttributes Map size = " + attMap.size() |
54 |
+ "<br>"); |
55 |
for (Entry<Object, Object> entry : attMap.entrySet()) { |
56 |
out.println(" | " + entry.getKey() + "=:" |
57 |
+ entry.getValue() + "<br>"); |
58 |
} |
59 |
60 |
} |
61 |
AttributePrincipal principal = assertion.getPrincipal(); |
62 |
63 |
// AttributePrincipal principal = (AttributePrincipal) request |
64 |
// .getUserPrincipal(); |
65 |
66 |
String username = null; |
67 |
out.print(" Log | UserName:"); |
68 |
if (null != principal) { |
69 |
username = principal.getName(); |
70 |
out.println("<span style='color:red;'>" + username + "</span><br>"); |
71 |
} |
72 |
73 |
out.println("</body>"); |
74 |
out.println("</html>"); |
75 |
} |
76 |
} |
再進行上述測試顯示結果如下:
http://app1.micmiu.com:18080/examples/servlets/servlet/HelloWorldExample :
http://app2.micmiu.com:28080/examples/servlets/servlet/HelloWorldExample
從上述頁面可以看到通過認證的用戶名。
到此已經全部完成了CAS單點登錄實例演示。














