CAS 单点登录(代码部分)

资料下载地址:https://download.csdn.net/download/u011072258/11964484

一、CAS简介

CAS是一个单点登录框架,开始是由耶鲁大学的一个组织开发,后来归到apereo管理。同时CAS也是开源,遵循着apache 2.0协议,代码目前是在github上管理。

此次使用的CAS版本:cas-server: 4.0.0cas-client: 3.2.1

二、资料清单

1、casDemo.zip》:包含3Tomcat,其中apache-tomcat-7.0.75 - service用于部署cas 服务端程序,另外2Tomcat个模拟cas 客户端程序。

2、hosts》: 将文件中的映射添加到C:WindowsSystem32driversetc 目录hosts文件中,用于模拟跨域请求下的CAS单点登录。

127.0.0.1  cas.server.com   

127.0.0.1  cas.client1.com

127.0.0.1  cas.client2.com

3、《cas.sql》: 新建mysql 数据库 cas ,导入cas.sql生成user 表,用于服务端用户名密码验证(adminadmin)。说明:密码MD5加密。

4、cas-server-4.0.0.zip》:cas-service-4.0.0 的源码,可以根据此工程做定制开发。

5、《cas-client 所需jar.zip》:包含cas客户端所需jar包,共3个。

6、cas.war》:cas-server-4.0.0服务端生成的 war包,放在Tomcat中即可访问(apache-tomcat-7.0.75 - servicewebappscas.war)。

三、本地配置文件说明

  1. 服务端配置说明
  • deployerConfigContext.xmlapache-tomcat-7.0.75 - servicewebappscas.warWEB-INF)
  •  另外cas默认是HTTPS方式访问,我们可以改为http访问。需要修改三个文件四处地方的配置(针对4.0.0版本,其他版本的话第一处必改,其他的看看还有没有带有cookie的文件名),详细配置修改如下:
  • (1)、WEB-INF/deployerConfigContext.xml

  • 第一处:增加参数 p:requireSecure=”false” ,是否需要安全验证,即 HTTPS , false 为不采用,添加以后如下:
  • <!-- 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-INFspring-configurationwarnCookieGenerator.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" />

  1. 客户端配置说明
  • 解压《cas-client 所需jar.zip》,将解压所得jar包放入客户端工程(Tomcat 自带的 examples工程)WEB-INFlib下。
  • 配置web.xmlapache-tomcat-7.0.75 - client1webappsexamplesWEB-INFweb.xml

四、Demo演示

请确保已经创建mysql 数据库cas,并导入cas.sql,已修改deployerConfigContext.xml文件中的数据库配置,并配置hosts文件。

1、解压casDemo.zip,进入apache-tomcat-7.0.75 - servicein,点击startup.bat启动cas 服务端程序。端口:8080,访问路径:http://cas.server.com:8080/cas/login

   

2、再分别启动apache-tomcat-7.0.75 - client1apache-tomcat-7.0.75 - client1,端口分别为80818082

访问路径:

http://cas.client1.com:8081/examples

http://cas.client2.com:8082/examples

3、分别访问http://cas.client1.com:8081/exampleshttp://cas.client2.com:8082/examples,因为未登录,都重定向到了cas-service的登录页面。

 4、在第一个窗口输入用户名、密码(adminadmin),点击登录,跳转到了cas.client1.com的首页。

此时,直接刷新第二个页面,因为cas.client1.com已经登录,无需再次登录,可直接跳转到cas.client2.com 的首页。

至此,cas单点登录功能已经实现。

参考文章:https://blog.csdn.net/u011872945/article/details/81045608

                  http://www.pianshen.com/article/2882278229/

原文地址:https://www.cnblogs.com/Garnett-Boy/p/11812961.html