使用cas做sso helloword
因?yàn)楣ぷ餍枰佑|到sso,據(jù)知目前多數(shù)sso使用耶魯?shù)?/span>cas實(shí)現(xiàn),且發(fā)現(xiàn)使用cas真的是很簡(jiǎn)單就可以做出一個(gè)單點(diǎn)登錄系統(tǒng)來(lái),cas還提供豐富的擴(kuò)展功能,對(duì)于擴(kuò)展功能日后再細(xì)細(xì)研究,這里只使用cas做一個(gè)hello world來(lái)記錄本人的學(xué)習(xí)過(guò)程,如有幸被高人看到,還望指出弊病,不吝賜教。
在使用cas之前最好對(duì)數(shù)字證書(shū)有所了解,不了解也沒(méi)有關(guān)系,跟著我的步驟也一樣可以跑的通。
準(zhǔn)備工作
需要的文件:
ü Jdk6
ü Tomcat
ü cas-server-3.3.2
ü cas-client-3.1.9
證書(shū)
下面是keytool命令的一些常用方法,先在這里認(rèn)識(shí)一下它們,一會(huì)兒會(huì)用的到。
使用keytool命令生成密鑰庫(kù)
keytool -genkey -alias tomcat -keyalg RSA -dname "CN=pcma, OU=vanceinfo, O=vanceinfo, L=haidian, S=beijing, C=CN" -keystore c:"keystore5.jks
CN:主機(jī)名
OU:組織單位
O:組織
L:地區(qū)
S:城市
C:國(guó)家
如果需要指定密鑰有效期,添加-validity 365即可,單位是天,如:
keytool -genkey -alias tomcat -keyalg RSA -dname "xxxxx" -keystore xxxxx -validity 365
導(dǎo)出證書(shū)
keytool -export -file c:/server5.crt -alias tomcat -keystore c:"keystore5.jks
將證書(shū)導(dǎo)入到客戶(hù)端jdk
keytool -import -keystore "D:"Java"jdk1.6.0_14"jre"lib"security"cacerts" -file c:/server5.crt -alias tomcat
從密鑰庫(kù)中刪除指定別名的證書(shū)
keytool -delete -noprompt -alias tomcat -keystore E:"apache-tomcat-6.0.20_2"conf"keystore2.jks
查看密鑰庫(kù)中的證書(shū)
keytool -list -v -keystore c:"keystore5.jks
配置tomcat
使用keytool命令生成密鑰庫(kù)。
配置%tomcat_home%/conf/server.xml使tomcat支持SSL協(xié)議,并指定密鑰庫(kù)。
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystorePass="mashiguang"
keystoreFile="${catalina.home}/conf/keystore5.jks"/>
部署cas server
解壓縮cas-server-3.3.2-release.zip文件,在modules目錄里找到cas-server-webapp-3.3.2.war文件,這就是一個(gè)做好了的cas服務(wù)端,我們做的sso helloword可以直接使用,只需把cas-server-webapp-3.3.2.war改了個(gè)簡(jiǎn)單點(diǎn)的名字,如cas.war,然后部署到tomcat即可。
瀏覽器訪問(wèn)https://pcma:8443/cas,如果打開(kāi)顯示的是cas默認(rèn)的登錄頁(yè)面,則表示服務(wù)端已部署完畢。
客戶(hù)端使用cas client
新建兩個(gè)web工程,用于模擬單點(diǎn)登錄系統(tǒng)中的客戶(hù)端,并將cas-client-3.1.9"modules里的jar包放到web工程lib目錄下,是主要的是cas-client-core-3.1.9.jar文件,把spring2.5也放到lib目錄下。
Web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/casContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 負(fù)責(zé)用戶(hù)認(rèn)證 -->
<filter>
<filter-name>CAS Authentication Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<!-- CAS login 服務(wù)地址-->
<init-param>
<param-name>casServerLoginUrl</param-name>
<param-value>https://pcma:8443/cas/login</param-value>
</init-param>
<init-param>
<param-name>renew</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>gateway</param-name>
<param-value>false</param-value>
</init-param>
<!-- 客戶(hù)端應(yīng)用服務(wù)地址-->
<init-param>
<param-name>serverName</param-name>
<param-value>http://pcma:8081</param-value>
</init-param>
</filter>
<!--負(fù)責(zé)Ticket校驗(yàn)-->
<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>cas.validationfilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Authentication Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
casContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="cas.validationfilter" class="org.jasig.cas.client.validation.Cas10TicketValidationFilter">
<property name="ticketValidator">
<ref bean="cas10TicketValidator"/>
</property>
<property name="useSession">
<value>true</value>
</property>
<!-- 客戶(hù)端應(yīng)用服務(wù)地址-->
<property name="serverName">
<value>http://pcma:8081</value>
</property>
<property name="redirectAfterValidation">
<value>true</value>
</property>
</bean>
<bean id="cas10TicketValidator" class="org.jasig.cas.client.validation.Cas10TicketValidator">
<!-- 這里參數(shù)是cas服務(wù)器的地址-->
<constructor-arg index="0" value="https://pcma:8443/cas" />
</bean>
</beans>
新建index.jsp文件
<body>
hello sso<br>
<a >sso2</a>這個(gè)地址是另外一臺(tái)機(jī)器上的sso客戶(hù)端
</body>
上面的web.xml、casContext.xml、index.jsp是兩個(gè)客戶(hù)端中的一個(gè),另一個(gè)要根據(jù)實(shí)際情況修改。
最后不要忘記客戶(hù)端的jdk要使用keytool命令導(dǎo)入證書(shū)文件。
測(cè)試
啟動(dòng)tomcat,測(cè)試器訪問(wèn)http://pcma:8081/sso,出現(xiàn)cas登錄頁(yè)面,輸入相同的用戶(hù)名和密碼即可登錄,登錄成功后頁(yè)面自動(dòng)跳轉(zhuǎn)回http://pcma:8081/sso,這時(shí)點(diǎn)擊頁(yè)面上的sso2鏈接,就可以自動(dòng)登錄并跳轉(zhuǎn)到sso2應(yīng)用。
如果輸入用戶(hù)名密碼后提示下面的異常,是因?yàn)椴渴鹂蛻?hù)端的jdk沒(méi)有導(dǎo)入證書(shū)文件的原因。
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
源碼
http://m.tkk7.com/Files/mashiguang/sso.zip