最近项目中需要做单点登录,客户端包含Java、ruby和PHP,java有几个应用程序,php是discuz+supesite+ucenter,配置步骤如下:

1、cas服务端:下载地址:http://downloads.jasig.org/cas/cas的服务端和客户端有许多版本,最新版本和老版本有很大的区别,解压cas-server-3.4.4-release.zip将modules目录下的cas-server-webapp-3.4.4.war改名称为cas.war复制到tomcat的webapps下,启动tomcat,访问:http://localhost:8080/cas/login 就可以看到登录界面;

cas服务端默认采用的是 用户名=密码的验证,并且采用的是https验证,需要给tomact配置证书,本系统没有采用https验证,若采用https验证可参考:

http://blog.csdn.net/haydenwang8287/archive/2010/07/26/5765941.aspx

1.1、若不采用https验证,服务器端需要配置

1、cas\WEB-INF\deployerConfigContext.xml

<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient"/>

增加参数p:requireSecure="false",是否需要安全验证,即HTTPS,false为不采用,加上去之后如下:

<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
 p:httpClient-ref="httpClient"  p:requireSecure="false"/>

2、cas\WEB-INF\spring-configuration\

ticketGrantingTicketCookieGenerator.xml

<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"

p:cookieSecure="true"

p:cookieMaxAge="-1"

p:cookieName="CASTGC"

p:cookiePath="/cas" />

参数p:cookieSecure="true",同理为HTTPS验证相关,TRUE为采用HTTPS验证,FALSE为不采用https验证。

参数p:cookieMaxAge="-1",简单说是COOKIE的最大生命周期,-1为无生命周期,即只在当前打开的IE窗口有效,IE关闭或重新打开其它窗口,仍会要求验证。可以根据需要修改为大于0的数字,比如3600等,意思是在3600秒内,打开任意IE窗口,都不需要验证

1.2、服务器端退出访问:http://localhost:8080/cas/logout,
若希望退出后能返回则需要配置

服务端cas-servlet.xml配置

<bean id="logoutController" class="org.jasig.cas.web.LogoutController" ... .../>

增加属性 p:followServiceRedirects="true"

退出链接为:http://localhost:8080/cas/logout?service=http://localhost:8080/Casclient/index.jsp

1.3、更改服务器端验证方式,采用数据库验证:

修改配置文件deployerConfigContext.xml,加dbcp连接池:(以Oracle为例)

<bean id="casDataSource" class="org.apache.commons.dbcp.BasicDataSource">
     <property name="driverClassName">
          <value>oracle.jdbc.driver.OracleDriver</value>
     </property>
     <property name="url">
          <value>jdbc:oracle:thin:@192.168.18.26:1521:orcl</value>
     </property>
     <property name="username">
          <value>test</value>
     </property>
     <property name="password">
          <value>test</value>
     </property>
   </bean>

需要的jar包有:(见附件:cas-server-support-jdbc-3.4.4.jar,commons-dbcp-1.2.1.jar,commons-pool-1.3.jar,ojdbc14_g.jar)

配置加密方式,cas内置的有MD5加密,也可以写自己的加密类,实现org.jasig.cas.authentication.handler.PasswordEncoder接口即可:

<bean id="passwordEncoder"  
    class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">     
    <constructor-arg value="MD5"/>  
   </bean>

注释掉默认的验证方式,采用数据库查询验证:

<property name="authenticationHandlers">
     <list>
     <!----注释掉这里的默认验证方式,采用以下验证QueryDatabaseAuthenticationHandler-->
    <!--
    <bean
     class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" /> -->

<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
      <property name="dataSource" ref="casDataSource" />
      <property name="sql" 
         value="select password from userinfo where lower(username) = lower(?)" />
      <property  name="passwordEncoder"  ref="passwordEncoder"/>
     </bean>
   </list>
  </property>

---------------到这里cas服务端的配置就完成了。

2、java客户端配置,下载客户端:http://downloads.jasig.org/cas-clients/,目前最新版本为:cas-client-3.2.0

将modules下的jar复制到java客户端Casclient1的lib下,在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">
 
 <!-- 用于单点退出,该过滤器用于实现单点登出功能,通知其他应用单点登出-->

<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>/*</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>http://192.168.18.8:8080/cas/login</param-value>
                 <!--这里的server是服务端的IP-->
         </init-param>
         <init-param>
                 <param-name>serverName</param-name>
                 <param-value>http://192.168.18.8:8989</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>http://192.168.18.8:8080/cas</param-value>
         </init-param>
         <init-param>
                 <param-name>serverName</param-name>
                 <param-value>http://192.168.18.8:8989</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>

<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>
 
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

页面为:

<%
AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();   
String username = principal.getName(); 
%>
<br/>----------------------------------------------------------<br/>
<h1>登录成功,这是客户端1啊</h1><br/>
用户名:<%=username %><br/>
<a href="http://localhost:8989/Casclient2/index.jsp">进入客户端2</a><br/>

<a href="http://localhost:8080/cas/logout?service=http://localhost:8989/Casclient1/index.jsp">退出</a><br/>

-----------到这里java客户端配置成功,发布到tomcat,复制Casclient1改名为Casclient2,启动tomcat,

访问Casclient1,跳转到登录页面,登录成功后成功转向登录成功页面,这时访问Casclient2发现不需要登录即显示登录成功页面,java单点登录成功。

3、配置php客户端,下载php客户端:http://downloads.jasig.org/cas-clients/php/ ,

新建php工程:Phpcasclient1,将CAS文件夹和CAS.php复制到工程中,将docs/examples/example_simple.php

拷贝到apache目录下,修改如下:

<?php

//
// phpCAS simple client
//

// import phpCAS lib
include_once('CAS.php');

phpCAS::setDebug();

// initialize phpCAS
phpCAS::client(CAS_VERSION_2_0,'192.168.18.8',8080,'cas');   //注意端口号没有引号
phpCAS::setNoCasServerValidation();

// force CAS authentication
phpCAS::forceAuthentication();

// at this step, the user has been authenticated by the CAS server
// and the user's login name can be read with phpCAS::getUser().

// logout if desired
if (isset($_REQUEST['logout'])) {

$param=array("service"=>"http://localhost/Phpcasclient1/example_simple.php");//退出登录后返回

phpCAS::logout($param);
}

// for this test, simply print that the authentication was successfull
?>

注意的地方:如果服务器配置为http方式,则需要将CAS/Client.php文件中的https替换为http(大概有四个地方,记不清了),否则会报错:

CAS Authentication failed!

You were not authenticated.

You may submit your request again by clicking here.

If the problem persists, you may contact the administrator of this site.

............................

<html>
  <head>
    <title>phpCAS simple client</title>
  </head>
  <body>
    <h1>Successfull Authentication!这是客户端1</h1>
    <p>the user's login is <b><?php echo phpCAS::getUser(); ?></b>.</p>
    <p>phpCAS version is <b><?php echo phpCAS::getVersion(); ?></b>.</p>
     <p><a href="http://192.168.18.8:8989/Casclient1/index.jsp">去java客户端1</a></p>
     <p><a href="?logout=">退出</a></p>
  </body>
</html>

php配置需要开启php_curl,可以复制Phpcasclient1为Phpcasclient2

访问:http://localhost/Phpcasclient1/example_simple.php,跳转到登录页面,登录成功后访问Phpcasclient2,不需要登录,

php单点登录成功,这时再访问java客户端发现也不需要登录,php和java应用之间单点登录成功。

注:php的phpCAS::client(CAS_VERSION_2_0,'192.168.18.8',8080,'cas');地址需要和java的web.xml中的cas服务器地址一致,我开始一个写的ip:192.168.18.8,一个写的localhost,

php和java总是不能同步登录,郁闷了好久

----------------到这里java和php的客户端已经配置完成,现在你会发现php和java之间不能单点登出,php端退出java客户端也退出,反之java退出但是php却没有同步退出

这里需要做一个配置,在

phpCAS::setNoCasServerValidation();

// force CAS authentication
phpCAS::forceAuthentication();

这里加上

phpCAS::setNoCasServerValidation();

// force CAS authentication

phpCAS::handleLogoutRequests();  这里会检测服务器端java退出的通知,就能实现php和java间同步登出了。

phpCAS::forceAuthentication();

至于discuz+supesite的单点登录,再了解了php单点登录的原理后就需要改造discuz+supesite的登录代码了,discuz的为logging.php

supersite的为batch.login.php,俺是做java开发的,对php不是很熟悉,所以改造的觉得不是很靠谱,大致是先让discuz单点登录,获取用户名,根据用户

获取数据库中的密码再交给discuz系统自己的登录系统登录。discuz是采用cookie验证的,所以在java端退出后,discuz不会退出。

若谁有改造很成功的可以交流下。

参考网址:

http://blog.csdn.net/DL88250/archive/2008/08/20/2799522.aspx

http://www.wsria.com/archives/1349

http://tonrenyuye.blog.163.com/blog/static/30012576200922925820471/

http://www.discuz.net/thread-1416206-1-1.ht

用cas来实现php的单点登陆的更多相关文章

  1. 集成基于CAS协议的单点登陆

    相信大家对单点登陆(SSO,Single Sign On)这个名词并不感到陌生吧?简单地说,单点登陆允许多个应用使用同一个登陆服务.一旦一个用户登陆了一个支持单点登陆的应用,那么在进入其它使用同一单点 ...

  2. CAS 单点登陆

    一.Tomcat配置SSL 1. 生成 server key 以命令方式换到目录%TOMCAT_HOME%,在command命令行输入如下命令: keytool -genkey -alias tomc ...

  3. CAS单点登陆 SSO

    什么是单点登陆 SO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他应用中用于同一个用户的登录的机制.它是目前比较流行的企业业务整合的解决方 ...

  4. CAS单点登陆/oAuth2授权登陆

    单点登陆 CAS是一个单点登录框架,即Central Authentication Service(中心认证服务) ,开始是由耶鲁大学的一个组织开发,后来归到apereo去管,github地址:htt ...

  5. cas单点登陆。就这一篇就够了!!!!!

    前言: cas是什么我就不累赘说了.就简单说下大致的流程.首先,cas是一个独立的项目.就是一个war包,部署在tomcat上面启动就ok.然后我们要实现单点登陆,无疑是访问系统1,如果没有登录,就跳 ...

  6. cas单点登陆系统-casServer搭建

    最近工作比较忙,空闲的时间在搞单点登陆系统,自己写了一套SSO在GitHub上,过程走通了.通过这个例子,自己熟悉了流程,而且破天荒的使用了抽象设计模式,并且熟悉了cookies和session的使用 ...

  7. 在多点环境下使用cas实现单点登陆及登出

    CAS 介绍 CAS 是 Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目.CAS 具有以下特 ...

  8. 集成基于OAuth协议的单点登陆

    在之前的一篇文章中,我们已经介绍了如何为一个应用添加对CAS协议的支持,进而使得我们的应用可以与所有基于CAS协议的单点登陆服务通讯.但是现在的单点登陆服务实际上并不全是通过实现CAS协议来完成的.例 ...

  9. 单点登陆(SSO)

    一.背景 在企业发展初期,企业使用的系统很少,通常一个或者两个,每个系统都有自己的登录模块,运营人员每天用自己的账号登录,很方便.但随着企业的发展,用到的系统随之增多,运营人员在操作不同的系统时,需要 ...

随机推荐

  1. 3. 编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,统计鱼的数量

    //Animal 类 package d922B; public class Animal { private String kind; private int legs,count; public ...

  2. Linux openvswitch性能调优

    Increasing the flow-eviction threshold The threshold is a type of limit on the number of flows that ...

  3. webservice简介及客户端搭建

    在环境变量中,CLASSPATH添加D:\apache-cxf-2.4.2\lib 新建CXF_HOME   D:\apache-cxf-2.4.2 在PATH中添加   D:\apache-cxf- ...

  4. Javaweb 第4 天xml 课程

    XML课程 今日大纲 ● XML概念 ● XML基本语法 ● XML约束 ● dom4j技术 ● xpath技术 ******************************************* ...

  5. C相关的图书(链接不可用)

    Visual C++视频技术方案宝典.pdf: http://www.t00y.com/file/17628500 Windows 图形编程.pdf: http://www.t00y.com/file ...

  6. Sikuli:创新的图形化编程技术

    Sikuli是一种使用截图进行UI自动化测试的技术.Sikuli包括sikul脚本,基于Jython的API以及sikuli IDE.Sikuli可以实现任何你可以在显示器上看到ui对象的自动化,你可 ...

  7. HDU1686:Oulipo

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  8. 3.请问配置JDK时环境变量path和JAVA_HOME的作用是什么?

    1.PATH环境变量.作用是指定命令搜索路径,在i命令行下面执行命令如javac编译java程序时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序.需要把jdk安装目录下的bin目录增 ...

  9. LNMP环境的安装配置

    0.安装必要的依赖软件 如果已经安装了可能会进行升级,版本完全一致则不会进行任何操作. yum -y install bzip2-devel curl-devel freetype-devel gcc ...

  10. MD5 32位加密算法源码(测试通过)(系转载 飞扬天下)

    供自己学习使用 md5.h文件 #ifndef MD5_H #define MD5_H #include <string> #include <fstream> /* Type ...