资料下载地址:https://download.csdn.net/download/u011072258/11964484mysql
1、CAS简介git
CAS是一个单点登陆框架,开始是由耶鲁大学的一个组织开发,后来归到apereo管理。同时CAS也是开源,遵循着apache 2.0协议,代码目前是在github上管理。github
这次使用的CAS版本:cas-server: 4.0.0、cas-client: 3.2.1web
2、资料清单spring
一、《casDemo.zip》:包含3个Tomcat,其中apache-tomcat-7.0.75 - service用于部署cas 服务端程序,另外2个Tomcat个模拟cas 客户端程序。sql
二、《hosts》: 将文件中的映射添加到C:\Windows\System32\drivers\etc 目录hosts文件中,用于模拟跨域请求下的CAS单点登陆。数据库
127.0.0.1 cas.server.com apache
127.0.0.1 cas.client1.com跨域
127.0.0.1 cas.client2.comtomcat
三、《cas.sql》: 新建mysql 数据库 cas ,导入cas.sql生成user 表,用于服务端用户名密码验证(admin,admin)。说明:密码MD5加密。
四、《cas-server-4.0.0.zip》:cas-service-4.0.0 的源码,能够根据此工程作定制开发。
五、《cas-client 所需jar包.zip》:包含cas客户端所需jar包,共3个。
六、《cas.war》:cas-server-4.0.0服务端生成的 war包,放在Tomcat中便可访问(apache-tomcat-7.0.75 - service\webapps\cas.war)。
3、本地配置文件说明
(1)、WEB-INF/deployerConfigContext.xml
<!-- Required for proxy ticket mechanism. -->
<bean id="proxyAuthenticationHandler"
class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient" p:requireSecure="false"/>
该文件还有第二处须要修改,不然会出现来回从客户端到服务端来回跳转的问题,修改完成之后以下:
<util:list id="registeredServicesList">
<bean class="org.jasig.cas.services.RegexRegisteredService"
p:id="0" p:name="HTTP and IMAP" p:description="Allows HTTP(S) and IMAP(S) protocols"
p:serviceId="^(https?|imaps?|http?)://.*" p:evaluationOrder="10000001" p:enabled="true" p:allowedToProxy="true" p:ssoEnabled="true" />
(2) WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml
第三处:修改 p:cookieSecure=”true” 为 p:cookieSecure=” false ” , 即不须要安全 cookie,以下部分:
<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="CASTGC"
p:cookiePath="/cas" />
(3) WEB-INF\spring-configuration\warnCookieGenerator.xml
第四处:修改 p:cookieSecure=”true” 为 p:cookieSecure=” false ” ,即不须要安全 cookie,结果以下:
<bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="CASPRIVACY"
p:cookiePath="/cas" />
4、Demo演示
请确保已经建立mysql 数据库cas,并导入cas.sql,已修改deployerConfigContext.xml文件中的数据库配置,并配置hosts文件。
1、解压casDemo.zip,进入apache-tomcat-7.0.75 - service\bin,点击startup.bat启动cas 服务端程序。端口:8080,访问路径:http://cas.server.com:8080/cas/login
2、再分别启动apache-tomcat-7.0.75 - client1、apache-tomcat-7.0.75 - client1,端口分别为8081、8082。
访问路径:
http://cas.client1.com:8081/examples
http://cas.client2.com:8082/examples
3、分别访问http://cas.client1.com:8081/examples,http://cas.client2.com:8082/examples,由于未登陆,都重定向到了cas-service的登陆页面。
4、在第一个窗口输入用户名、密码(admin,admin),点击登陆,跳转到了cas.client1.com的首页。
此时,直接刷新第二个页面,由于cas.client1.com已经登陆,无需再次登陆,可直接跳转到cas.client2.com 的首页。
至此,cas单点登陆功能已经实现。
参考文章:https://blog.csdn.net/u011872945/article/details/81045608