Table of Contents
登录成功,这是客户端1啊
Successfull Authentication!这是客户端1
Home Backend Development PHP Tutorial cas实现单点登录,登出(java跟php客户端) (转)

cas实现单点登录,登出(java跟php客户端) (转)

Jun 13, 2016 pm 12:29 PM
cas filter gt lt name

cas实现单点登录,登出(java和php客户端) (转)

最近项目中需要做单点登录,客户端包含java和php,java有几个应用程序,php是discuz+supesite+ucenter,需

?

要这几个客户端都要能单点登录和登出,在网上找了许多相关资料,今天终于配置成功,步骤如下:

?

1、cas服务端:下载地址:http://downloads.jasig.org/cas/cas的服务端和客户端有许多版本,最新版本和老版本

?

有很大的区别,目前服务端最新版本为:cas-server-3.4.4-release.zip

?

解压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

p:httpClient-ref="httpClient"/>

?

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

?

?p:httpClient-ref="httpClient"? p:requireSecure="false"/>

?

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

ticketGrantingTicketCookieGenerator.xml

????? 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配置

增加属性 p:followServiceRedirects="true"

?

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

?

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

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

?


????
????????? oracle.jdbc.driver.OracleDriver
????

????
????????? jdbc:oracle:thin:@192.168.18.26:1521:orcl
????

????
????????? test
????

????
????????? test
????

??

?

需要的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接口即可:

? ???? class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">?????
??? ?
??

?

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


?????
?????
????

?

?????
?????
????? ???????? value="select password from userinfo where lower(username) = lower(?)" />
?????
?????

???
??

?

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

?

?

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

?

将modules下的jar复制到java客户端Casclient1的lib下,在web.xml中配置过滤器,配置如下(详情见附件):


?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">
?
?
?

?
???????? org.jasig.cas.client.session.SingleSignOutHttpSessionListener
?

?

?
???????? CAS Single Sign Out Filter
???????? org.jasig.cas.client.session.SingleSignOutFilter
?

?
???????? CAS Single Sign Out Filter
???????? /*
?

?
?
?
???????? CASFilter
???????? org.jasig.cas.client.authentication.AuthenticationFilter
????????
???????????????? casServerLoginUrl
???????????????? http://192.168.18.8:8080/cas/login
????????????????
????????

????????
???????????????? serverName
???????????????? http://192.168.18.8:8989
????????

?

?
???????? CASFilter
???????? /*
?

?
?
?
???????? CAS Validation Filter
????????
???????????????? org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter

????????
???????????????? casServerUrlPrefix
???????????????? http://192.168.18.8:8080/cas
????????

????????
???????????????? serverName
???????????????? http://192.168.18.8:8989
????????

?

?
???????? CAS Validation Filter
???????? /*
?

?
?
?
???????? CAS HttpServletRequest Wrapper Filter
????????
???????????????? org.jasig.cas.client.util.HttpServletRequestWrapperFilter

?

?
???????? CAS HttpServletRequest Wrapper Filter
???????? /*
?


??????? CAS Assertion Thread Local Filter
??????? org.jasig.cas.client.util.AssertionThreadLocalFilter


??????? CAS Assertion Thread Local Filter
??????? /*

?
?
?
??? index.jsp
?

?

页面为:

AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();???
String username = principal.getName();
%>

----------------------------------------------------------

登录成功,这是客户端1啊



用户名:

http://localhost:8989/Casclient2/index.jsp">进入客户端2

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

?

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

?

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

?

?

3、配置php客户端,下载php客户端:http://downloads.jasig.org/cas-clients/php/?,目前最新版本为:CAS-1.2.0RC2

?

新建php工程:Phpcasclient1,将CAS文件夹和CAS.php复制到工程中,修改CAS/client.php,将其中的https改为http,将docs/examples/example_simple.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');

// no SSL validation for the CAS server
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
?>

?
??? phpCAS simple client
?
?
???

Successfull Authentication!这是客户端1


???

the user's login is .


???

phpCAS version is .


?????

http://192.168.18.8:8989/Casclient1/index.jsp

">去java客户端1


?????

退出


?

?

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.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1652
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to solve the '[Vue warn]: Failed to resolve filter' error How to solve the '[Vue warn]: Failed to resolve filter' error Aug 19, 2023 pm 03:33 PM

Methods to solve the "[Vuewarn]:Failedtoresolvefilter" error During the development process using Vue, we sometimes encounter an error message: "[Vuewarn]:Failedtoresolvefilter". This error message usually occurs when we use an undefined filter in the template. This article explains how to resolve this error and gives corresponding code examples. When we are in Vue

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Implement PHP security verification through CAS (Central Authentication Service) Implement PHP security verification through CAS (Central Authentication Service) Jul 24, 2023 pm 12:49 PM

PHP security verification through CAS (CentralAuthenticationService) With the rapid development of the Internet, user rights management and identity verification are becoming more and more important. When developing web applications, it is crucial to protect user data and prevent unauthorized access. In order to achieve this goal, we can use CAS (CentralAuthenticationService) for PHP security verification. CAS

What is the concept of java CAS What is the concept of java CAS May 03, 2023 pm 09:34 PM

1. Explain that when multiple threads perform CAS operations on a resource at the same time, only one thread succeeds, but it will not block other threads, and other threads will only receive a signal that the operation failed. It can be seen that CAS is actually an optimistic lock. 2. Following the AtomInteger code, we can find that sum.misc.Unsafe is finally called. Look at the name Unsafe, it's an unsafe class that exploits just the right holes in Java's class and visibility rules. For the sake of speed, Unsafe makes some compromises on Java's security standards. publicfinalnativebooleancompareAndSwapInt(Objec

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

See all articles