Maison php教程 php手册 php 用CAS实现SSO单点登陆及登出功能

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

Jun 06, 2016 pm 08:02 PM
cas php sso 功能 实现 se déconnecter 登陆

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/ 

解压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"></bean> 
Copier après la connexion

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

<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler" p:httpclient-ref="httpClient" p:requiresecure="false"></bean>
Copier après la connexion
找到文件: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"></bean>
Copier après la connexion
参数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>
Copier après la connexion

需要的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"></constructor-arg>    
   </bean>
Copier après la connexion
注释掉默认的验证方式,采用数据库查询验证:
<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>  
      <property name="sql" value="select password from userinfo where lower(username) = lower(?)"></property>  
      <property name="passwordEncoder" ref="passwordEncoder"></property>  
     </bean>  
   </list>  
  </property> 
Copier après la connexion
服务器配置完成


二.配置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,此文件用于处理单点登陆,内容如下:

Copier après la connexion
Copier après la connexion

<pre name="code" class="php"><?php class user
{
	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function logout()
	{
		session_start();
		//启用单点登录的时候还要到统一认证中心注销  && isset($_SESSION[&#39;phpCAS&#39;]) && $_SESSION[&#39;phpCAS&#39;][&#39;auth_checked&#39;] == &#39;1&#39; 
		
		//引人cas
		include_once &#39;/CAS-1.2.0/CAS.php&#39;;
		
		// initialize phpCAS			
		//phpCAS::client(CAS_VERSION_2_0,&#39;服务地址&#39;,端口号,&#39;cas的访问地址&#39;);
		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&amp;cssUrl=http://localhost/phpCasClient/style/login.css&amp;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=&#39;http://localhost/phpCasClient/home.php&#39;;</script>";
		}else{
			// 访问CAS的验证
			phpCAS::forceAuthentication();
		}
		exit;
	}
}
?&gt;
Copier après la connexion


Copier après la connexion
Copier après la connexion

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



<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title>单点登陆界面</title>


<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>
  <span style="white-space:pre">	</span><td><a href="http://localhost/phpCasClient/user.php?a=logout">登出</a></td>
  </tr>
</table>
</div>

Copier après la connexion
注意:php配置文件php.ini需要开启php_curl,即 找到 ;extension=php_curl.dll ,将该句前面的分号去掉即可,改为 extension=php_curl.dll

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

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


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





Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian

Configuration du projet CakePHP Configuration du projet CakePHP Sep 10, 2024 pm 05:25 PM

Configuration du projet CakePHP

Date et heure de CakePHP Date et heure de CakePHP Sep 10, 2024 pm 05:27 PM

Date et heure de CakePHP

Téléchargement de fichiers CakePHP Téléchargement de fichiers CakePHP Sep 10, 2024 pm 05:27 PM

Téléchargement de fichiers CakePHP

Routage CakePHP Routage CakePHP Sep 10, 2024 pm 05:25 PM

Routage CakePHP

Discuter de CakePHP Discuter de CakePHP Sep 10, 2024 pm 05:28 PM

Discuter de CakePHP

Comment configurer Visual Studio Code (VS Code) pour le développement PHP Comment configurer Visual Studio Code (VS Code) pour le développement PHP Dec 20, 2024 am 11:31 AM

Comment configurer Visual Studio Code (VS Code) pour le développement PHP

Guide rapide CakePHP Guide rapide CakePHP Sep 10, 2024 pm 05:27 PM

Guide rapide CakePHP

See all articles