您的位置:首页 > 编程语言 > PHP开发

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

2014-08-04 11:21 489 查看
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 就可以看到登录界面了:



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,如图:



若希望退出后能返回则需要配置服务端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

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><p><pre name="code" class="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,该页面即为单点登陆页面,内容如下:

<!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>
  <span style="white-space:pre">	</span><td><a href="http://localhost/phpCasClient/user.php?a=logout">登出</a></td>
  </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的单点登陆,登出已经操作完毕.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: