首页 > php教程 > php手册 > 正文

php 用CAS实现SSO单点登陆及登出功能

php中文网
发布: 2016-06-06 20:02:07
原创
2539人浏览过

php用cas实现sso单点登陆及登出功能 一..CAS服务器搭建 CAS服务器端下载地址:http://downloads.jasig.org/cas/ 解压cas-server-4.0.0-release.zip将modules目录下的cas-server-webapp-4.0.0.war改名称为cas.war复制到 tomcat的webapps下,启动tomcat,访问:

php用cas实现sso单点登陆及登出功能


一..CAS服务器搭建

CAS服务器端下载地址:http://downloads.jasig.org/cas/ 

天天团购系统
天天团购系统

天天团购系统是一套强大的开源团购程序,采用PHP+mysql开发,系统内置支付宝、财付通、GOOGLE地图等接口,支持短信发送团购券和实物团购快递发货等;另外可通过Ucenter模块,与网站已有系统无缝整合,实现用户同步注册、登陆、退出。 天天团购系统是一套创新的开源团购程序,拥有多达10项首创功能,同时支持虚拟和实物团购,内置类似淘宝的快递配送体系,并提供强大的抽奖、邀请返利等营销功能,让您轻松

天天团购系统 0
查看详情 天天团购系统

解压cas-server-4.0.0-release.zip将modules目录下的cas-server-webapp-4.0.0.war改名称为cas.war复制到tomcat的webapps下,启动tomcat,访问:http://localhost:8080/cas/login 就可以看到登录界面了:

php 用CAS实现SSO单点登陆及登出功能

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

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

1.若不采用http验证,服务器需配置如下:

找到文件cas/WEB-INF/deployerConfigContext.xml的如下内容:

<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
04.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"/>
登录后复制
找到文件: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窗口,都不需要验证。

服务器端退出地址:http://localhost:8080/cas/logout,如图:

php 用CAS实现SSO单点登陆及登出功能

若希望退出后能返回则需要配置服务端cas-servlet.xml配置

增加属性 p:followServiceRedirects="true"

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

2.更改服务器验证方式,采用数据库验证

修改配置文件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> 
登录后复制
服务器配置完成


二.配置PHP客户端

PHP客户端下载地址:http://downloads.jasig.org/cas-clients/php/,目前最新版本为CAS-1.2.0.ORC2

新建项目:phpCasClient.将CAS文件夹和CAS.php复制到工程中,修改CAS/client.php,将其中的https改为http,新建php文件:user.php,此文件用于处理单点登陆,内容如下:

登录后复制

<pre name="code" class="php"><?php
class user
{
	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function logout()
	{
		session_start();
		//启用单点登录的时候还要到统一认证中心注销  && isset($_SESSION['phpCAS']) && $_SESSION['phpCAS']['auth_checked'] == '1' 
		
		//引人cas
		include_once '/CAS-1.2.0/CAS.php';
		
		// initialize phpCAS			
		//phpCAS::client(CAS_VERSION_2_0,'服务地址',端口号,'cas的访问地址');
		phpCAS::client(CAS_VERSION_2_0,"192.168.142.1","80","/cas");
		
		//方法一:登出成功后跳转的地址 -- 登出方法中加此句
		/*
		phpCAS::setServerLoginUrl("https://192.168.142.1:80/cas/logout?embed=true&service=http://localhost/phpCasClient/user.php?a=login");
		//no SSL validation for the CAS server
		phpCAS::setNoCasServerValidation();
		phpCAS::logout();*/

		//方法二:退出登录后返回地址 -- 登出方法中加此句
		phpCAS::setNoCasServerValidation();$param=array("service"=>"http://localhost/phpCasClient/user.php?a=login");
		phpCAS::logout($param);
	}
	
	/** 
	 * @desc LoginCas()单点登陆 
	 */
	public function loginCas(){
		Header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
		//引人cas
		include 'CAS-1.2.0/CAS.php';
		// initialize phpCAS 
		//phpCAS::client(CAS_VERSION_2_0,'服务地址',端口号,'cas的访问地址');
		phpCAS::client(CAS_VERSION_2_0,"192.168.142.1","80","/cas",true);

		//可以不用,用于调试,可以通过服务端的cas.log看到验证过程。
		// phpCAS::setDebug();
		//登陆成功后跳转的地址 -- 登陆方法中加此句
		phpCAS::setServerLoginUrl("https://192.168.142.1:80/cas/login?embed=true&cssUrl=http://localhost/phpCasClient/style/login.css&service=http://localhost/phpCasClient/user.php?a=loginCas");
		//no SSL validation for the CAS server 不使用SSL服务校验
		phpCAS::setNoCasServerValidation();
		//这里会检测服务器端的退出的通知,就能实现php和其他语言平台间同步登出了
		phpCAS::handleLogoutRequests();
		
		if(phpCAS::checkAuthentication()){
			//获取登陆的用户名
			$username=phpCAS::getUser();
			//用户登陆成功后,采用js进行页面跳转
			echo "<script language=\"javascript\">parent.location.href='http://localhost/phpCasClient/home.php';</script>";
		}else{
			// 访问CAS的验证
			phpCAS::forceAuthentication();
		}
		exit;
	}
}
?>
登录后复制


登录后复制

新建视图层,login.html,该页面即为单点登陆页面,内容如下:

立即学习PHP免费学习笔记(深入)”;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>单点登陆界面</title>
</head>
<body>
<div class="view">
<table style="width:600; height:333; border:1; align:center; cellpadding:4; bordercolor:#CCCCCC;">
	<tr>
	<td style="width: 50%;align:center;">
			请输入登陆信息:
	</td>
		<td style="width: 50%;">
			<div id="maincol" style="border: 1px solid #ccc;float: left;width: 400px;height: 219px; overflow: hidden;">
				<iframe id="auth-login-iframe" name="auth-login-iframe" style="width:100%; height:100%; marginwidth:0; marginheight:0; frameborder:0; scrolling:no;" src="http://localhost/phpCasClient/user.php?a=loginCas"></iframe>
			</div>
		</td>
	</tr>
  <tr>
&#160; <span style="white-space:pre">	</span><td><a href="http://localhost/phpCasClient/user.php?a=logout">登出</a></td>
&#160; </tr>
</table>
</div>
</body>
</html>
登录后复制
注意:php配置文件php.ini需要开启php_curl,即 找到 ;extension=php_curl.dll ,将该句前面的分号去掉即可,改为 extension=php_curl.dll

此时,访问login.html,便可以看到单点登陆的界面,登陆成功后,页面跳转到home.php中.

点击 登出,控制层请求user.php的logout方法,处理登出请求.登出成功后,页面跳转到login.html,提示用户登陆


至此,php使用CAS的单点登陆,登出已经操作完毕.





相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号