只是简单的验证csa服务器登录和读取数据库账号密码登录
环境window2008+tomcat7+jdk7+cas-server-4.0.0-release
1. 首先到 http://downloads.jasig.org/ 地址下载 cas-server-4.0.0-release.zip,解压后到modules目录中找到cas-server-webapp-4.0.0.war,复制到tomcat的webapp目录下,修改名称为cas.war
2. 创建证书
keytool -genkey -alias ssotest -keyalg RSA -keysize 2048 -keystore C:/cas/keys/ssotest.keystore
导出证书
keytool -export -file c:/cas/keys/ssotest.crt -alias ssotest -keystore C:/cas/keys/ssotest.keystore
客户端JVM导入证书
keytool -import -keystore C:\Users\Administrator\Desktop\jdk764\jre\lib\security\cacerts -file c:/cas/keys/ssotest.crt -alias ssotest
如果提示:
keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect
那么输入密码:changeit
这是因为JDK安装后会默认创建一个密钥库,密码为:changeit
也可以删除d:\Java\jdk1.7.0_60\jre\lib\security\cacerts 在输入上述命令
3. 让tomcat使用证书
打开tomcat目录的conf/server.xml文件,8443端处,并设置keystoreFile、keystorePass修改结果如下:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="c:/cas/keys/ssotest.keystore" keystorePass="123456" />
启动tomcat测试是否可用,https://localhost:8443 能进入到tomcat目录表示https配置成功。
4. 登录cas
启动tomcat之后再webapp中的cas.war自动解压,我们可以直接访问https://localhost:8443/cas/login, cas4.0 默认账号密码是casuser/Mellon 如下图
5. 读取数据库进行认证
需要的jar包有cas-server-support-jdbc.jar c3p0-0.9.1.2.jar mysql-connector-java-5.1.24 ,我这里是mysq 其他数据库根据情况整理jar包,放入到webapps\cas\WEB-INF\lib 。之后在cas\WEB-INF下找到deployerConfigContext.xml进行修改
首先注释掉默认的登录账号密码
<bean id="primaryAuthenticationHandler" class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler"> <property name="users"> <map> <entry key="casuser" value="Mellon"/> </map> </property> </bean>
注释之后,添加jdbc配置,这里为了方便没有采用加密
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" p:driverClass="com.mysql.jdbc.Driver" p:jdbcUrl="jdbc:mysql://ip:3306/dts?useUnicode=true& characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" p:user=user p:password=password /> <bean id="dbAuthHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"> <property name="sql" value="select password from user_info where name=?" /> <property name="dataSource" ref="dataSource" /> </bean>
再找到id=authenticationManager的bean
<bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager"> <constructor-arg> <map> <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" /> <!-- <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />--> <entry key-ref="dbAuthHandler" value-ref="primaryPrincipalResolver"/> </map> </constructor-arg> <property name="authenticationPolicy"> <bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy" /> </property> </bean>
是为了注释掉key-ref="primaryAuthenticationHandler"的部分,然后引入id=dbAuthHandler的bean
之后重启tomcat 进行验证
在此列出一些比较好的相关博文以供参考:
• http://blog.csdn.net/frinder/article/details/7969925
• http://sgq0085.iteye.com/blog/2003190
• http://sgq0085.iteye.com/blog/2099196
• http://www.iteye.com/blogs/tag/cas