php 用CAS实现SSO单点登陆及登出功能
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 就可以看到登录界面了:
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>
增加参数p:requireSecure="false",是否需要安全验证,即HTTPS,false为不采用,加上去之后,如下:
<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler" p:httpclient-ref="httpClient" p:requiresecure="false"></bean>
<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator" p:cookiesecure="true" p:cookiemaxage="-1" p:cookiename="CASTGC" p:cookiepath="/cas"></bean>
参数p:cookieMaxAge="-1",简单说是COOKIE的最大生命周期,-1为无生命周期,即只在当前打开的IE窗口有效,IE关闭或重新打开其它窗口,仍会要求验证。可以根据需要修改为大于0的数字,比如3600等,意思是在3600秒内,打开任意IE窗口,都不需要验证。
服务器端退出地址:http://localhost:8080/cas/logout,如图:
若希望退出后能返回则需要配置服务端cas-servlet.xml配置
增加属性 p:followServiceRedirects="true"
退出链接为:http://localhost:8080/cas/logout?service=http://localhost:8080/Casclient/index.jsp
修改配置文件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"></constructor-arg> </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> <property name="sql" value="select password from userinfo where lower(username) = lower(?)"></property> <property name="passwordEncoder" ref="passwordEncoder"></property> </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,该页面即为单点登陆页面,内容如下:
<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>
此时,访问login.html,便可以看到单点登陆的界面,登陆成功后,页面跳转到home.php中.
点击 登出,控制层请求user.php的logout方法,处理登出请求.登出成功后,页面跳转到login.html,提示用户登陆
至此,php使用CAS的单点登陆,登出已经操作完毕.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
