作者:龍智 (Dragon)
時(shí)間:2006-07-09
CAS(Central Authentication Service)是耶魯大學(xué)開發(fā)的一個(gè)開源的SSO(single sign on,單點(diǎn)登錄)系統(tǒng)。它提供了豐富的客戶端庫(kù),如Java, .NET, PHP, Perl等版本,使用這些庫(kù)用戶可以方便地給自己的應(yīng)用程序加上CAS支持。Acegi security system for Spring是Spring的一個(gè)子項(xiàng)目,它為Java EE開發(fā)者提供了一個(gè)易于使用的提供認(rèn)證和授權(quán)服務(wù)的安全框架。Acegi支持CAS,也可看作是CAS的一個(gè)Java版的Client。
以下詳細(xì)介紹如何配置CAS以及應(yīng)用程序,使其利用Acegi和CAS進(jìn)行用戶的登錄和認(rèn)證。我將以acegi-security-1.0.1發(fā)布包中附帶的acegi-security-sample-tutorial應(yīng)用為例,它使用DaoAuthenticationProvider對(duì)用戶進(jìn)行認(rèn)證,用戶帳號(hào)和權(quán)限信息保存在一個(gè)properties文件中,我將對(duì)其進(jìn)行改造,改造之后,acegi-security-sample-tutorial使用CAS進(jìn)行用戶認(rèn)證,授權(quán)信息仍從該properties文件讀取,因?yàn)?/span>CAS只負(fù)責(zé)認(rèn)證,不負(fù)責(zé)授權(quán),所以授權(quán)工作交由客戶端Acegi來(lái)完成,CAS的用戶源配置為數(shù)據(jù)庫(kù),利用JDBC進(jìn)行讀取。本文需要讀者對(duì)SSO和Acegi有一定的了解。
一.準(zhǔn)備工作
二.安裝CAS
解壓縮cas-server-3.0.5-rc2.zip,拷貝target目錄中的cas.war到%CATALINA_HOME%/webapps下即可。運(yùn)行Tomcat,訪問http://localhost:8080/cas應(yīng)可看到CAS登錄界面。
三.配置Tomcat支持SSL
由于CAS要求使用https和客戶端進(jìn)行通信,所以需要配置Tomcat支持SSL,首先介紹如何制作自簽名證書以及將其導(dǎo)入到證書庫(kù)。
1. keytool -keystore keystore -alias acegisecurity -genkey -keyalg RSA -validity 9999 -storepass password -keypass password
What is your first and last name?
[Unknown]: localhost
其他隨便填寫即可。
2. keytool -export -v -rfc -alias acegisecurity -file acegisecurity.txt -keystore keystore -storepass password
3. copy acegisecurity.txt %JAVA_HOME%\jre\lib\security
4. copy keystore %CATALINA_HOME %
5. cd %JAVA_HOME%\jre\lib\security
6. keytool -import -v -file acegisecurity.txt -keypass password -keystore cacerts -storepass changeit -alias acegisecurity
接下來(lái),用編輯器打開%CATALINA_HOME%/conf/server.xml,找到
<Connector port="8443" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
這一行默認(rèn)是被注釋掉的,取消注釋,并加入keystoreFile="keystore" keystorePass="password"這兩個(gè)屬性,注意keystoreFile屬性可以使用keystore文件的絕對(duì)路徑,也可使用基于%CATALINA_HOME%環(huán)境變量的相對(duì)路徑,keystorePass是訪問keystore的密碼,應(yīng)和上面制作證書時(shí)設(shè)定的密碼保持一致。
訪問https://localhost:8443,應(yīng)彈出一個(gè)對(duì)話框,告知用戶正要訪問的站點(diǎn)的證書不安全,是否接受,確認(rèn)接受,應(yīng)可看到那只熟悉可愛的小貓。(由于msn space對(duì)blog篇幅的限制,本文不得不分割為上、下兩篇)
(接上)
四.改造acegi-security-sample-tutorial
解壓縮acegi-security-1.0.1.zip,拷貝acegi-security-sample-tutorial.war到%CATALINA_HOME%/webapps目錄下,重啟tomcat,acegi-security-sample-tutorial即已發(fā)布。現(xiàn)在我們將其改造為使用CAS進(jìn)行用戶的登錄和認(rèn)證。
用編輯器打開WEB-INF/applicationContext-acegi-security.xml,找到
<bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="authenticationFailureUrl" value="/acegilogin.jsp?login_error=1"/>
<property name="defaultTargetUrl" value="/"/>
<property name="filterProcessesUrl" value="/j_acegi_security_check"/>
<property name="rememberMeServices" ref="rememberMeServices"/>
</bean>
將其替換為:
<bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.cas.CasProcessingFilter">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="authenticationFailureUrl" value="/acegilogin.jsp?login_error=1"/>
<property name="defaultTargetUrl" value="/"/>
<property name="filterProcessesUrl" value="/j_acegi_cas_security_check"/>
<property name="rememberMeServices" ref="rememberMeServices"/>
</bean>
其中,authenticationFailureUrl是認(rèn)證失敗時(shí)顯示的頁(yè)面,acegi-security-sample-tutorial登錄失敗時(shí)會(huì)在登錄頁(yè)(acegilogin.jsp)顯示失敗原因,現(xiàn)改為使用CAS之后,acegi-security-sample-tutorial使用CAS的登錄頁(yè)面,故acegilogin.jsp可去掉。接下來(lái),找到
<bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl" value="/acegilogin.jsp"/>
<property name="forceHttps" value="false"/>
</bean>
替換為:
<bean class="org.acegisecurity.ui.cas.CasProcessingFilterEntryPoint">
<property name="loginUrl">
<value>https://localhost:8443/cas/login</value>
</property>
<property name="serviceProperties">
<ref bean="serviceProperties"/>
</property>
</bean>
再接下來(lái),找到
<bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
<property name="providers">
<list>
<ref local="daoAuthenticationProvider"/>
<bean class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider">
<property name="key" value="changeThis"/>
</bean>
<bean class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider">
<property name="key" value="changeThis"/>
</bean>
</list>
</property>
</bean>
將<ref local="daoAuthenticationProvider"/>修改為<ref local="casAuthenticationProvider"/>,并添加以下bean:
<bean id="casAuthenticationProvider" class="org.acegisecurity.providers.cas.CasAuthenticationProvider">
<property name="ticketValidator">
<ref bean="ticketValidator"/>
</property>
<property name="casProxyDecider">
<ref bean="casProxyDecider"/>
</property>
<property name="statelessTicketCache">
<ref bean="statelessTicketCache"/>
</property>
<property name="casAuthoritiesPopulator">
<ref bean="casAuthritiesPopulator"/>
</property>
<property name="key">
<value>some_unique_key</value>
</property>
</bean>
<bean id="ticketValidator" class="org.acegisecurity.providers.cas.ticketvalidator.CasProxyTicketValidator">
<property name="casValidate">
<value>https://localhost:8443/cas/proxyValidate</value>
</property>
<property name="serviceProperties">
<ref bean="serviceProperties"/>
</property>
</bean>
<bean id="serviceProperties" class="org.acegisecurity.ui.cas.ServiceProperties">
<property name="service">
<value>https://localhost:8443/acegi-security-sample-tutorial/j_acegi_cas_security_check</value>
</property>
</bean>
<bean id="casProxyDecider" class="org.acegisecurity.providers.cas.proxy.RejectProxyTickets"/>
<bean id="statelessTicketCache" class="org.acegisecurity.providers.cas.cache.EhCacheBasedTicketCache">
<property name="cache">
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
</property>
<property name="cacheName" value="userCache"/>
</bean>
</property>
</bean>
<bean id="casAuthritiesPopulator" class="org.acegisecurity.providers.cas.populator.DaoCasAuthoritiesPopulator">
<property name="userDetailsService">
<ref bean="userDetailsService"/>
</property>
</bean>
改造完畢!
五.配置CAS使用JDBC數(shù)據(jù)源進(jìn)行用戶認(rèn)證
CAS默認(rèn)設(shè)置為只要用戶名和密碼相同,即可進(jìn)行登錄,這在現(xiàn)實(shí)使用中是不允許的。我們修改為使用MySQL的test數(shù)據(jù)庫(kù)中的app_user表作為用戶數(shù)據(jù)源。首先,我們?cè)?/font>test庫(kù)中創(chuàng)建一個(gè)表:
CREATE TABLE `app_user` (
`username` varchar(30) NOT NULL default '',
`password` varchar(45) NOT NULL default '',
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
并添加如下用戶:
INSERT INTO `app_user` (`username`,`password`) VALUES
('dianne','emu'),
('marissa','koala'),
('peter','opal'),
('scott','wombat');
用編輯器打開%CATALINA_HOME%/webapps/cas/WEB-INF/deployerConfigContext.xml,找到
<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />
注釋掉該行,在其下加入:
<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="sql" value="select password from app_user where username=?" />
<property name="dataSource" ref="dataSource" />
</bean>
并添加一個(gè)bean:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/test</value></property>
<property name="username"><value>test</value></property>
<property name="password"><value>test</value></property>
</bean>
拷貝cas-server-jdbc-3.0.5-rc2.jar和mysql-connector-java-3.1.12-bin.jar到%CATALINA_HOME%/webapps/cas/WEB-INF/lib下。
重新啟動(dòng)tomcat,在瀏覽器中輸入http://localhost:8080/acegi-security-sample-tutorial,你會(huì)發(fā)現(xiàn),一旦你訪問了受保護(hù)的頁(yè)面,請(qǐng)求就會(huì)被重定向到CAS的登錄頁(yè)面,登錄成功之后請(qǐng)求會(huì)被再被定向到最初訪問的頁(yè)面,如果有多個(gè)系統(tǒng),在這些系統(tǒng)之間進(jìn)行切換將不會(huì)要求用戶重新登錄,這就達(dá)到了單點(diǎn)登錄的目的。
參考文獻(xiàn):