1、打开tomcat/webapps/cas/WEB-INF/deployerConfigContext.xml
2、找到如下代码:
<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler"/>
这个就是cas默认使用的登录验证方式,用户名=密码即可登录系统
3、使用以下两种方式之一,替换上面的代码:
<bean id="SearchModeSearchDatabaseAuthenticationHandler"
class="org.jasig.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler"
abstract="false" lazy-init="default" autowire="default">
<property name="tableUsers">
<value>user</value><!--用户表-->
</property>
<property name="fieldUser">
<value>name</value><!--数据表中的用户名字段-->
</property>
<property name="fieldPassword">
<value>password</value><!--数据表中的密码字段-->
</property>
<property name="dataSource" ref="dataSource" /><!--数据源-->
</bean>
或者
<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="dataSource" ref="dataSource" /><!--数据源-->
<property name="sql" value="select password from some_table where lower(username) = lower(?)" /><!--通过用户名从数据库中查找密码-->
</bean>
4、配置数据源(以mysql为例)
在配置文件的最下面加入如下bean信息:
<!-- MySQL connector -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mydb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
5、需要添加的jar
cas-server-support-jdbc-3.5.2.jar
commons-collections-3.2.jar
commons-dbcp-1.2.1.jar
commons-pool-1.3.jar
可能会遇到的问题
The reference to entity "characterEncoding" must end with the ';' delimiter.
分析解决:
url="jdbc:mysql://127.0.0.1:3306/onlinefriend?useUnicode=true&characterEncoding=utf-8"
附:在xml文件中有以下几类字符要进行转义替换:
< | < | 小于 |
> | > | 大于 |
& | & | 与 |
' | ' | 单引号 |
" | " | 双引号 |