---
title: "CAS单点登录学习笔记五之CAS服务器数据源"
author: "IT-攻城师"
date: "2013-05-18T00:48:02Z"
tags: []
categories: ["开源软件"]
slug: "cas-cas"
draft: false
lastmod: "2013-05-18T00:49:12Z"
id: 1871341
url: "https://dylanxu.iteye.com/blog/1871341"
---

CAS服务器数据源的配置真实验证，在前面的示例中，使用的用户名和密码都是自己捏造出来的，而在实际的应用系统中，都需要从应用数据库中读取用户名和密码，下面就进行自定义CAS服务器的数据源的实验

 

1、**创建数据库表结构user\_info 用户表**

2、**cas服务器配置**

**打开cas-server的apache-tomcat-6.0.36\\webapps\\cas\\WEB-INF\\deployerConfigContext.xml文件，找到其中的authenticationManager的authenticationHandlers属性配置。默认的配置是这样的**

 

```
<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" 
					/>
```

 

 

```
 
```

**这里配置了一个SimpleTestUsernamePasswordAuthenticationHandler，这个用户名密码的认证器在认证用户时，只要用户名和密码保持一致，就认为是有效的用户，这也是为什么在前面的测试中，只要输入的用户名和密码一致就能登录的原因。在实际的生产环境中，应该将该认证器取替换为**

 

```
<bean
					class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
					<property name="dataSource" ref="casDataSource" />
					<property name="sql"
						value="select passwd from mall_user_info where lower(user_name) = lower(?)" />
					<property name="passwordEncoder" ref="myPasswordEncoder" />
				</bean>
```

 

 

 

 

```
<bean id="casDataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
			<value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
		</property>
		<property name="username">
			<value>system</value>
		</property>
		<property name="password">
			<value>password</value>
		</property>
	</bean>

	<bean id="myPasswordEncoder" class="org.jasig.cas.authentication.handler.MyPasswordEncoder" />
	<bean id="MD5PasswordEncoder"
		class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder">
		<constructor-arg index="0">
			<value>MD5</value>
		</constructor-arg>
	</bean>
```

    这样CAS Server服务器会采用QueryDatabaseAuthenticationHandler方式认证，该认证器位于cas-server-support-jdbc包中，需要在项目中增加相应的jar包、同时我们采用了ORACLE数据库oracle10g.jar 添加两个数据相关jar包。

 

重启cas-server服务器，进入登录页面 此时用户名或密码不正确会ERRO提示凭证有误

![](http://dl2.iteye.com/upload/attachment/0084/6538/2284a1cb-f51c-3311-9cd0-376fc2d85ed5.png)  
 

输入正确数据

![](http://dl2.iteye.com/upload/attachment/0084/6540/185ed243-8a34-357b-967a-8fa499cf88d2.png)