CAS服务端,JAVA客户端,PHP客户端配置

1.CAS服务端


下载CAS服务端最新版本,如现在最新版本为cas-server-3.5.2

1.1解压cas-server-3.5.2-release.zip将modules目录下的cas-server-webapp-3.5.2.war改名称为cas.war复制到
tomcat的webapps下,启动tomcat,访问:http://localhost:8180/cas/login 就可以看到登录界面了:
(我这里CAS配置在单独的一个tomcat中,所以端口是8180)
1.2如果想添加SSL需要添加443端口的支持,请看接下来的配置,如果不用,直接跳过这一步,
配置和生成导入证书可以参考这个帖子 http://desert3.iteye.com/blog/1700335

1.3如果不采用443端口,则只需要关掉CAS的https,
关掉https
修改webapps\cas\WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml文件,修改后如下:

<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
 p:cookieSecure="false"
 p:cookieMaxAge="-1"
 p:cookieName="CASTGC"
 p:cookiePath="/cas" />

修改webapps\cas\WEB-INF\deployerConfigContext.xmlauthenticationHandlers下有一个org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler,修改后如下:
<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
 p:httpClient-ref="httpClient" 
 p:requireSecure="false"/>


比原来增加了一个属性配置p:requireSecure="false",这个属性默认值是true,代表cas-server在回调代理应用时使用https,以加强安全。
一定要注意,cas代理模式如果不用https的话,一定要配置requireSecure=false
1.4修改默认验证接口,默认CAS server只需要账号密码一致即可登录,这明显不符合我们需求,所以做了如下修改让其访问数据库
配置dataSource,
修改webapps\cas\WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/oa_mw</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>1234</value>
		</property>
	</bean>


找到authenticationHandlers,在list中删除默认验证bean,加一个查询bean如下
<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">  
					<property name="dataSource" ref="dataSource" />
					<property name="sql" value="select login_psw from ac_user where login_name=?" /> 
					<property name="passwordEncoder" ref="passwordEncoderBean"/>
				</bean>

可以增加自己的密码加密,如果需要定义自己的加密方式,可以自己把代码拷出去修改后打成class覆盖回去

<bean id="passwordEncoderBean" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder">
    <constructor-arg value="MD5" />
	</bean>

2.CAS Java客户端


这里有一些参数是代理模式需要用到的,可以让客户端A直接拿到客户端B的数据,配置A客户端web.xml添加如下代码

<!-- CAS Client Start -->
	<!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置 -->
	<listener>
		<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
	</listener>


	<!-- 该过滤器用于实现单点登出功能,可选配置。 -->
	<filter>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
	</filter>
	
	<!-- 该过滤器负责用户的认证工作,必须启用它 -->
	<filter>
		<filter-name>CASFilter</filter-name>
		<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
		<init-param>
			<param-name>casServerLoginUrl</param-name>
			<param-value>http://localhost:8180/cas/login</param-value>
			<!--这里的server是服务端的IP -->
		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<param-value>http://localhost:80</param-value>
		</init-param>
	</filter>
	
	<!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->
	<filter>
		<filter-name>CAS Validation Filter</filter-name>
		<filter-class>
			org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
		<init-param>
			<param-name>casServerUrlPrefix</param-name>
			<param-value>http://localhost:8180/cas</param-value>
		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<param-value>http://localhost:80</param-value>
		</init-param>
		<init-param>
        	<param-name>proxyCallbackUrl</param-name>
        	<param-value>http://localhost:80/mw-vrm-pb/index.jsp</param-value>
    	</init-param>
    	 
    	<init-param>
        	<param-name>proxyReceptorUrl</param-name>
        	<param-value>/index.jsp</param-value>
    	</init-param>
	</filter>
	
	<filter-mapping>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	
	
	<!-- 回调的地址,必须在AuthenticationFilter的URL-pattern之前单独的配出来 -->
	<filter-mapping>
    	<filter-name>CAS Validation Filter</filter-name>
    	<url-pattern>/index.jsp</url-pattern>
	</filter-mapping>
	
	<filter-mapping>
		<filter-name>CASFilter</filter-name>
		<url-pattern>/sso.ac</url-pattern>
	</filter-mapping>
	
	<filter-mapping>
		<filter-name>CAS Validation Filter</filter-name>
		<url-pattern>/sso.ac</url-pattern>
	</filter-mapping>


	<!-- 该过滤器负责实现HttpServletRequest请求的包裹, 比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。 -->
	<filter>
		<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
		<filter-class>
			org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
		<url-pattern>/sso.ac</url-pattern>
	</filter-mapping>


	<!-- 该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。 比如AssertionHolder.getAssertion().getPrincipal().getName()。 -->
	<filter>
		<filter-name>CAS Assertion Thread Local Filter</filter-name>
		<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>CAS Assertion Thread Local Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- CAS Client End -->


客户端B的web.xml
<!-- CAS Client Start -->
	<!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置 -->
	<listener>
		<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
	</listener>

	<!-- 该过滤器用于实现单点登出功能,可选配置。 -->
	<filter>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 该过滤器负责用户的认证工作,必须启用它 -->
	<filter>
		<filter-name>CASFilter</filter-name>
		<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
		<init-param>
			<param-name>casServerLoginUrl</param-name>
			<param-value>http://localhost:8180/cas/login</param-value>
			<!--这里的server是服务端的IP -->
		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<param-value>http://localhost:80</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CASFilter</filter-name>
		<url-pattern>/sso.ac</url-pattern>
	</filter-mapping>

	<!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->
	<filter>
		<filter-name>CAS Validation Filter</filter-name>
		<filter-class>
			org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
		<init-param>
			<param-name>casServerUrlPrefix</param-name>
			<param-value>http://localhost:8180/cas</param-value>
		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<param-value>http://localhost:80</param-value>
		</init-param>
		<init-param>
       		<param-name>acceptAnyProxy</param-name>
        	<param-value>true</param-value>
    	</init-param>
    	<init-param>
    	    <param-name>redirectAfterValidation</param-name>
	        <param-value>false</param-value>
    	</init-param> 
	</filter>
	<filter-mapping>
		<filter-name>CAS Validation Filter</filter-name>
		<url-pattern>/sso.ac</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>CAS Validation Filter</filter-name>
		<url-pattern>/oa/synUser/synUser.ac</url-pattern>
	</filter-mapping>

	<!-- 该过滤器负责实现HttpServletRequest请求的包裹, 比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。 -->
	<filter>
		<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
		<filter-class>
			org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。 比如AssertionHolder.getAssertion().getPrincipal().getName()。 -->
	<filter>
		<filter-name>CAS Assertion Thread Local Filter</filter-name>
		<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>CAS Assertion Thread Local Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- CAS Client End -->


然后这样再修改java客户端本身的登录功能,在后台取得CAS验证通过的用户名,取得本系统的用户,赋予权限放行即可。(这种做法需要各个系统用户名一致),如果用户名没办法一致,则需要新建用户关联表。


3.CAS PHP客户端


下载php端的客户端代码,http://downloads.jasig.org/cas-clients/php/ 

新建php工程:Phpcasclient,将CAS文件夹和CAS.php复制到工程中,修改CAS/client.php,将其中的https改为http,将docs/examples/example_simple.php、script_info.php复制到工程中,修改example_simple.php如下
<?php
//
// phpCAS simple client
//
// import phpCAS lib
include_once ('CAS.php');
// 可以不用,用于调试,可以通过服务端的cas.log看到验证过程
phpCAS::setDebug ();
// initialize phpCAS
phpCAS::client ( CAS_VERSION_2_0, 'localhost', 8180, 'cas' );
// 不使用SSL服务校验
phpCAS::setNoCasServerValidation ();
// 这里会检测服务器端java退出的通知,就能实现php和java间同步登出
phpCAS::handleLogoutRequests ();
// 访问CAS的验证
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/Phpcasclient/example_simple.php" 
	);
	phpCAS::logout ( $param );
}
// for this test, simply print that the authentication was successfull
?>
<html>
<head>
<title>phpCAS simple client</title>
</head>
<body>
	<h1>Successfull Authentication!</h1>
    <?php require 'script_info.php'?>
    <p>
		the user's login is <b><?php echo phpCAS::getUser(); ?></b>.
	</p>
	<p>
		phpCAS version is <b><?php echo phpCAS::getVersion(); ?></b>.
	</p>
	<p>
		<a href="?logout=">Logout</a>
	</p>
</body>
</html>

然后发布,打开网站验证是否已经指向CAS服务端,并且测试JAVA客户端,PHP客户端是否能同步登录状态

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值