Cas Server下载:http://developer.jasig.org/cas/
Cas Client下载:http://developer.jasig.org/cas-clients/
测试环境:
jdk:java version "1.8.0_60"
tomcat:apache-tomcat-7.0.65
mysql:mysql5.5.40
CAS Server:cas-server-4.0.0-release.zip
CAS Client:cas-client-3.2.1-release.zip
此教程是在jdk和tomcat成功安装的基础上完成,至于jdk和tomcat的安装大家可以网上找找资料。好吧,下面入正题。
一、生成证书
1、生成证书:
keytool -genkey -alias castest -keyalg RSA -keystore F:/keys/castest
生成一个别名为castest的证书。
此处需要特别注意口令(后续导入导出证书、CAS服务器端均要用到此口类)和“名字与姓氏”(为CAS跳转域名,否则会报错)
2、导出证书:
keytool -export -file F:/keys/castest.crt -alias castest -keystore F:/keys/castest
3、将证书导入到客户端JRE中(注意、是导入JRE中),如果security中已经存在cacerts,需要先将其删除。
keytool -import -keystore "D:\Program Files\Java\jdk1.8.0_60\jre\lib\security\cacerts" -file F:/keys/castest.crt -alias castest
二、配置服务器端
1、从http://developer.jasig.org/cas/上下载cas服务器端cas-server-4.0.0-release.zip,在modules目录下找到cas-server-webapp-4.0.0.war,将其复制到%TOMCAT_HOME%\webapps下,并将名称改为cas.war
2、修改%TOMCAT_HOME%\conf\server.xml文件,去掉此文件83到93行之间的注释,修改为:
<Connector SSLEnabled="true" clientAuth="false" keystoreFile="F:/keys/castest" <!—生成证书时的路径,证书名--> keystorePass="castest" <!—证书密码--> maxThreads="150" port="8443" protocol="org.apache.coyote.http11.Http11Protocol" scheme="https" secure="true" sslProtocol="TLS"/>
3、测试:https://localhost:8443/
点击继续浏览此网页
如果此时可以正常访问,说明证书安装成功
https://localhost:8443/cas/login,出现
输入账号和密码
casuser
Mellon
此时说明服务器端已经配置成功。可通过https://localhost:8443/cas/logout退出登录
三、配置客户端
1. 从http://developer.jasig.org/cas-clients/上下载cas-client-3.2.1-release.zip,在modules目录下找到cas-client-core-3.2.1.jar、commons-logging-1.1.jar复制到项目WEB-INF/lib下
2. 添加映射域名,在C:\Windows\System32\drivers\etc\hosts文件中添加
127.0.0.1 sso.castest.com
3. 创建web项目CasClient,并在项目的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"><display-name>cas-demo</display-name><!-- ======================== 单点登录开始 ======================== --><!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置--><listener><listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class></listener><!-- 该过滤器用于实现单点登出功能,可选配置。 --><filter><filter-name>CAS Single Sign Out Filter</filter-name><filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class></filter><filter-mapping><filter-name>CAS Single Sign Out Filter</filter-name><url-pattern>/CasClient/*</url-pattern></filter-mapping><!-- 该过滤器负责用户的认证工作,必须启用它 --><filter><filter-name>CASFilter</filter-name><filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class><init-param><param-name>casServerLoginUrl</param-name><param-value>https://sso.castest.com:8443/cas/login</param-value><!--这里的server是服务端的IP--></init-param><init-param><param-name>serverName</param-name><param-value>http://localhost:8080</param-value></init-param></filter><filter-mapping><filter-name>CASFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 该过滤器负责对Ticket的校验工作,必须启用它 --><filter><filter-name>CAS Validation Filter</filter-name><filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class><init-param><param-name>casServerUrlPrefix</param-name><param-value>https://sso.castest.com:8443/cas/</param-value><!-- 此处必须为登录url/cas/,带有任何其它路径都会报错,如“https://sso.castest.com:8443/cas/login”,这样也会报错。 --></init-param><init-param><param-name>serverName</param-name><param-value>http://localhost:8080</param-value></init-param></filter><filter-mapping><filter-name>CAS Validation Filter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--该过滤器负责实现HttpServletRequest请求的包裹,比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。--><filter><filter-name>CAS HttpServletRequest Wrapper Filter</filter-name><filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class></filter><filter-mapping><filter-name>CAS HttpServletRequest Wrapper Filter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。比如AssertionHolder.getAssertion().getPrincipal().getName()。--><filter><filter-name>CAS Assertion Thread Local Filter</filter-name><filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class></filter><filter-mapping><filter-name>CAS Assertion Thread Local Filter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- ======================== 单点登录结束 ======================== --><!-- session超时定义,单位为分钟 --><session-config><session-timeout>2</session-timeout></session-config></web-app>
4. 此时访问http://localhost:8080/CasClient/Index.jsp时会自动跳转到sso.castest.com下去登录
输入账号和密码casuser:Mellon
此时客户端已安装成功。
可以用类似的方式新建多个站点,站点1登录以后站点而无需登录也可访问,如果未登录则会跳转到相应的url进行登录验证。
至此,简单的服务器搭建已经完成了。然而,此时用户的验证是通过配置文件cas\WEB-INF\deployerConfigContext.xml中指定用户名和密码的方式进行验证的。
下面再另行介绍通过mysql数据库验证的方式。
一、CAS认证之mysql数据库认证
1、在mysql中新建一个cas数据库并创建user表
CREATE DATABASE /*!32312 IF NOT EXISTS*/`cas` /*!40100 DEFAULT CHARACTER SET gbk */;USE `cas`;/*Table structure for table `user` */DROP TABLE IF EXISTS `user`;CREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) NOT NULL,`password` varchar(255) NOT NULL,`used` tinyint(2) NOT NULL,PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;/*Data for the table `user` */insert into `user`(`id`,`name`,`password`,`used`) values (1,'casuser','9414f9301cdb492b4dcd83f8c711d8bb',1);
2、CAS的HTTP模式与HTTPS设置(可省略)
1)cas\WEB-INF\deployerConfigContext.xml,新增p:requireSecure="false"<bean id="proxyAuthenticationHandler" class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"p:httpClient-ref="httpClient" p:requireSecure="false"/>2)cas\WEB-INF\spring-configurationticketGrantingTicketCookieGenerator.xml设置p:cookieSecure="false"warnCookieGenerator.xml设置p:cookieSecure="false"
http://localhost:8080/cas/login,进入登录页面。
默认用户为casuser/Mellon,登录成功即配置完成。
3、设置利用数据库来验证用户
依赖包:
c3p0-0.9.1.2.jar
mysql-connector-java-5.1.21.jar
cas-server-support-jdbc-4.0.0.jar
cas\WEB-INF\deployerConfigContext.xml1)更换验证方式 <!--<bean id="primaryAuthenticationHandler"class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler"><property name="users"><map><entry key="casuser" value="Mellon"/></map></property></bean>--><!-- Define the DB Connection --><bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"p:driverClass="com.mysql.jdbc.Driver"p:jdbcUrl="jdbc:mysql://127.0.0.1:3306/cas?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"p:user="root"p:password="root" /><!-- Define the encode method--><!--<bean id="passwordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName"> <constructor-arg value="MD5"/> </bean> --><bean id="passwordEncoder"class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"c:encodingAlgorithm="MD5"p:characterEncoding="UTF-8" /><bean id="dbAuthHandler"class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"p:dataSource-ref="dataSource"p:sql="select password from user where name=? and used=1"p:passwordEncoder-ref="passwordEncoder"/><!-- p:passwordEncoder-ref="passwordEncoder" -->2)更换验证Handle <bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager"><constructor-arg><map><!--| IMPORTANT| Every handler requires a unique name.| If more than one instance of the same handler class is configured, you must explicitly| set its name to something other than its default name (typically the simple class name).--><entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" /><entry key-ref="dbAuthHandler" value-ref="primaryPrincipalResolver" /><!-- <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" /> --></map></constructor-arg>