php java 单点登录_用cas来实现php的单点登陆

本文介绍了如何通过CAS(Central Authentication Service)来实现PHP和Java应用的单点登录。首先,详细讲述了配置CAS服务端的过程,包括修改配置文件以适应HTTP验证和数据库验证。接着,展示了Java客户端的配置,包括添加过滤器和设置相关参数。最后,讲解了PHP客户端的设置,使用phpCAS库进行CAS客户端初始化和强制认证。文章还提到了同步登出的实现和注意事项。

最近项目中需要做单点登录,客户端包含Java、ruby和PHP,java有几个应用程序,php是discuz+supesite+ucenter,配置步骤如下:

1、cas服务端:下载地址:http://downloads.jasig.org/cas/,cas的服务端和客户端有许多版本,最新版本和老版本有很大的区别,解压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验证可参考:

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窗口,都不需要验证

服务端cas-servlet.xml配置

增加属性 p:followServiceRedirects="true"

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啊

用户名:

进入客户端2

退出

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

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

新建php工程:Phpcasclient1,将CAS文件夹和CAS.php复制到工程中,将docs/examples/example_simple.php

拷贝到apache目录下,修改如下:

//

// 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');   //注意端口号没有引号

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'])) {

phpCAS::logout($param);

}

// for this test, simply print that the authentication was successfull

?>

注意的地方:如果服务器配置为http方式,则需要将CAS/Client.php文件中的https替换为http(大概有四个地方,记不清了),否则会报错:

CAS Authentication failed!

You were not authenticated.

You may submit your request again by clicking here.

If the problem persists, you may contact the administrator of this site.

............................

phpCAS simple client

Successfull Authentication!这是客户端1

the user's login is <?php echo phpCAS::getUser(); ?>.

phpCAS version is <?php echo phpCAS::getVersion(); ?>.

去java客户端1

退出

php配置需要开启php_curl,可以复制Phpcasclient1为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不会退出。

若谁有改造很成功的可以交流下。

参考网址:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值