最近在研究cas+ldap实现单点登录,所以想来记录下自己的成果。
一、准备工作
1.下载cas-server-3.5.0-release.zip,解压,下载地址http://www.jasig.org/cas/download/
2.下载cas-client-3.2.1-release.zip,解压,下载地址http://www.jasig.org/cas/download/
3.下载tomcat6.0以上版本,解压
4.下载open-ldap,安装,下载地址 http://download.bergmans.us/openldap/openldap-2.2.29/openldap-2.2.29-db-4.3.29-openssl-0.9.8a-win32_Setup.exe
二、配置
1.ldap配置
安装open-ldap后,需要修改文件sladp.config
在找到include ./schema/inetorgperson.schema 在它下面增加
include ./schema/corba.schema include ./schema/dyngroup.schema include ./schema/java.schema include ./schema/misc.schema include ./schema/nis.schema include ./schema/openldap.schema
suffix "o=sql,c=RU"
rootdn "cn=root,o=sql,c=RU"
修改为你自己设置的,我的配置如下
suffix "dc=my-domain,dc=com"
rootdn "cn=Manager,dc=my-domain,dc=com"
密码设置 rootpw secret
设置好后使用ldap工具登陆增加用户,我使用的是jxplorer-3.2.2
2.tomcat配置
解压cas-server-3.5.0-release.zip后,把cas-server-3.5.0\modules目录下的cas-server-webapp- 3.5.0.war 拷贝到tomcat的webapps目录,改名为cas.war
由于cas要使用到ssl,https协议,使用我们要在tomcat中配置https,我们要修改server.xml文件,配置如下
<!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" />--> 改成 <Connector protocol="org.apache.coyote.http11.Http11NioProtocol" port="8443" minSpareThreads="5" maxSpareThreads="75" enableLookups="true" disableUploadTimeout="true" acceptCount="100" maxThreads="200" scheme="https" secure="true" SSLEnabled="true" clientAuth="false" sslProtocol="TLS" keystoreFile="E:/tomcat/apache-tomcat-6.0.29-test1/key/server.keystore" keystorePass="123456" />
keystoreFile是你用keytool工具生成的keystore文件,
我使用到的命令如下:
1.生成密钥库 keytool -genkey -alias tomcat -keyalg RSA -keypass 123456 -storepass 123456-keystore server.keystore -validity 3600 2.导出证书 keytool -export -trustcacerts -alias tomcat -file server.cer -keystore server.keystore -storepass 123456 3.把证书导出到jdk中 keytool -import -trustcacerts -alias tomcat -file server.cer -keystore E:\Java\jdk1.6.0_10\jre\lib\security\cacerts -storepass changeit
配置成功的话,启动tomcat,那么就能访问cas服务器了,访问地址:https://localhost:8443/cas/login
在tomcat的webapps目录下随便放几个web项目,我用的是tomcat自带的servlets-examples和jsp-examples项目,要让这两个项目实现单点登录,必须在这两个项目中配置cas client,
a.首先到cas-client-3.2.1-release.zip解压的目录下把cas-client-core-3.2.1.jar文件拷贝到这连个项目的lib目录下,b.在servlets-examples和jsp-examples的web.xml文件中增加如下代码
<filter> <filter-name>CASFilter</filter-name> <filter-class>edu.yale.its.tp.cas.client.filter.CASFilter</filter-class> <init-param> <param-name>edu.yale.its.tp.cas.client.filter.loginUrl</param-name> <param-value>https://localhost:8443/cas/login</param-value> </init-param> <init-param> <param-name>edu.yale.its.tp.cas.client.filter.validateUrl</param-name> <param-value>https://localhost:8443/cas/proxyValidate</param-value> </init-param> <init-param> <param-name>edu.yale.its.tp.cas.client.filter.serverName</param-name> <param-value>localhost:8080</param-value> </init-param> </filter> <filter-mapping> <filter-name>CASFilter</filter-name> <url-pattern>/servlet/*</url-pattern> </filter-mapping>
这样单点登录就配置成功了,你访问http://localhost:8080/jsp-examples/或http://localhost:8080/servlets-examples时都会跳到cas 服务上去登录,登录成功会跳回到你的项目中类,那另一个项目就不需要登录了,
3.cas 服务配置
要实现cas 基于ldap的单点登录我们还有在cas服务中配置一下
1.把从cas-server-3.5.0-release.zip的解压目录中把cas-server-support-ldap-3.5.0.jar文件拷贝到tomcat/webapps/cas 目录下的WEB-INF/lib目录中,在spring中把spring-ldap-core-1.3.1.RELEASE.jar文件拷贝到
tomcat/webapps/cas 目录下的WEB-INF/lib目录中,
2.配置tomcat/webapps/cas/deployerConfigContext.xml文件
<!--<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />--> 改成 <bean class="org.jasig.cas.adaptors.ldap.BindLdapAuthenticationHandler"> <property name="filter" value="cn=%u" /> <property name="searchBase" value="ou=user,dc=my-domain,dc=com" /> <property name="contextSource" ref="contextSource" /> </bean>
增加
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> <property name="anonymousReadOnly" value="false" /> <property name="password" value="secret" /> <property name="pooled" value="true" /> <property name="urls"> <list> <value>ldap://localhost:389/</value> </list> </property> <!-- 如果是老版本,这里应该用的是userName,而不是userDn --> <property name="userDn" value="cn=manager,dc=my-domain,dc=com" /> <property name="baseEnvironmentProperties"> <map> <entry> <!--none 端口 389--> <!--ssl 端口 636--> <key><value>java.naming.security.protocol</value></key> <value>none</value> </entry> <entry> <key><value>java.naming.security.authentication</value></key> <value>simple</value> </entry> </map> </property> </bean>
这样就配置完了cas+ldap实现单点登录了,当跳转到cas服务器时,你就可以使用ldap中的用户名和密码登录了