前言
Acegi Security是Spring Framework的一个子项目,主要专注于容器中应用程序的安全控制。
CAS是耶鲁大学贡献的一个单点登陆系统,提供多个应用程序的单点登陆控制,CAS和acegi进行整合,CAS负责完成认证部分,acegi负责应用程序的授权。
在Acegi Security的文档中虽然有关于CAS整合的详细描述,但由于两个系统目前都很活跃,版本不断跟新,完全依照旧版本的文档并不能完成相关的整合工作,下面以实际操作的例子说明acegi如何整合CAS 3.0。由于CAS认证要求系统运行在SSL上,所以在对两个应用程序配置之前,首先要配置容器的证书。
本文配置对象为
CAS Server 3.04
Acegi Security 1.0RC2
Apache Tomcat 5.5.12
配置Tomcat
使用JAVA keytool生成Tomcat 使用证书
- keytool -alias tomcat -genkey -keyalg RSA -validity 9999 -storepass password -keypass password
- 您的名字与姓氏是什么?
- [localhost]:
- 您的组织单位名称是什么?
- [itdcl]: java dev
- 您的组织名称是什么?
- [itdcl]:
- 您所在的城市或区域名称是什么?
- [guangzhou]:
- 您所在的州或省份名称是什么?
- [guangdong]:
- 该单位的两字母国家代码是什么
- [CN]:
- CN=localhost, OU=java dev, O=itdcl, L=guangzhou, ST=guangdong, C=CN 正确吗?
- [否]: y
导出证书到文件中
- cd <jre></jre>\lib\security
- keytool -export -v -rfc -alias tomcat -file acegisecurity.txt -storepass password
把证书设置为本机可信任证书
- keytool -import -v -file acegisecurity.txt -keypass password -keystore cacerts -storepass changeit -alias tomcat
- Owner: CN=localhost, OU=java dev, O=itdcl, L=guangzhou, ST=guangdong, C=CN
- 发照者: CN=localhost, OU=java dev, O=itdcl, L=guangzhou, ST=guangdong, C=CN
- 序号: 4420ca9f
- 有效期间: Wed Mar 22 11:55:11 CST 2006 至: Sat Aug 06 11:55:11 CST 2033
- 认证指纹:
- MD5: EA:27:FA:1E:B8:C2:05:24:9A:2B:D3:44:8F:0F:C3:78
- SHA1: B7:61:12:19:BF:54:75:D4:E9:A4:EA:9C:2A:5C:A9:B9:17:99:CA:F1
- 信任这个认证? [否]: y
- 认证已添加至keystore中
- [正在存储 cacerts]
打开Tomcat的ssl端口,配置相关参数
- <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" keystorePass="password"/>
测试Tomcat SSL安装成功
打开页面https://localhost:8443/jsp-examples,访问相关例子,如果没有异常,说明SSL配置成功
重新配置 CAS Server的用户认证部分
打开在WEB-INF中deployerConfigContext.xml,重新配置用户认证,文件本身就是spring的配置文件,在最后增加以下bean
- <bean id="inMemoryDaoImpl" class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl">
- <property name="userMap">
- <value>
- marissa=koala,ROLES_IGNORED_BY_CAS
- dianne=emu,ROLES_IGNORED_BY_CAS
- scott=wombat,ROLES_IGNORED_BY_CAS
- peter=opal,disabled,ROLES_IGNORED_BY_CAS
- value>
- property>
- bean>
- <bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
- <property name="userDetailsService"><ref bean="inMemoryDaoImpl"/>property>
- bean>
- <bean id="MyAuthenticationManager" class="org.acegisecurity.providers.ProviderManager">
- <property name="providers">
- <list>
- <ref bean="daoAuthenticationProvider"/>
- list>
- property>
- bean>
用配置好的
- <bean id="casAuthenticationHandler" class="org.acegisecurity.adapters.cas3.CasAuthenticationHandler">
- <property name="authenticationManager"><ref bean="MyAuthenticationManager"/>property>
- bean>
替代原来的
- SimpleTestUsernamePasswordAuthenticationHandler
编译
org.acegisecurity.adapters.cas3.CasAuthenticationHandler
因为在acegisecurity 1.0RC2正式发布的CAS adapter里面没有casAuthencationHandler,所以需要从acegisecurity的仓库中把原码check出来
编译casAuthencationHandler, 打包到acegi-security-CAS-1.0.0-RC2.jar
中发布CAS应用程序
把重新打包过的
acegi-security-CAS-1.0.0-RC2.jar
和acegi-security-1.0.0-RC2.jar
复制到WEB-INF/lib目录中
启动tomcat, 如果看到错误提示信息,则说明CAS Server部署成功测试CAS部署成功
在浏览器中访问
http://localhost:8080/CAS
,根据配置文件的用户信息,这里使用scott/wombat登录,如果提示登录成功,则表示新的用户认证部分修改成功
配置contacts-CAS
确认配置
applicationContext-acegi-security.xml
文件中
https://localhost:8443/CAS/为CAS服务器的URL
https://localhost:8443/contacts-CAS为contacts应用程序URL
对于系统默认的可信证书<jre home=""></jre>\lib\security\cacerts
可以使用
- <property name="trustStore"><value>
- /some/path/to/your/lib/security/cacerts
- value>property>
配置,如果确信上面import动作已经把证书倒入,可以不用配置这个属性
部署contacts-CAS, 检验是否整合成功
使用http://localhost:8080/contacts-CAS访问应用程序,使用marissa/koala可以正常使用,如果用scott/wombat则为access denied.