CAS 定制登录验证

1、在%CATALINA_HOME%/webapps/cas/WEB-INF/deployerConfigContext.xml文件中添加

  1. <bean id="casDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  2.     <property name="driverClassName">
  3.         <value>oracle.jdbc.driver.OracleDriver</value>
  4.     </property>
  5.     <property name="url">
  6.         <value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
  7.     </property>
  8.     <property name="username">
  9.         <value>stock</value>
  10.     </property>
  11.     <property name="password">
  12.         <value>111111</value>
  13.     </property>
  14. </bean> 

2、在 cas-server-support-jdbc-3.3.jar 包中,提供了 3 个基于 JDBC 的 AuthenticationHandler,分别为 BindModeSearchDatabaseAuthenticationHandler, QueryDatabaseAuthenticationHandler, SearchModeSearchDatabaseAuthenticationHandler。其中 BindModeSearchDatabaseAuthenticationHandler 是用所给的用户名和密码去建立数据库连接,根据连接建立是否成功来判断验证成功与否;QueryDatabaseAuthenticationHandler 通过配置一个 SQL 语句查出密码,与所给密码匹配;SearchModeSearchDatabaseAuthenticationHandler 通过配置存放用户验证信息的表、用户名字段和密码字段,构造查询语句来验证。使用哪个 AuthenticationHandler,需要在 deployerConfigContext.xml 中设置,默认情况下,CAS 使用一个简单的 username=password 的 AuthenticationHandler,在文件中可以找到如下一行:

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

我们可以将其注释掉,换成我们希望的一个 AuthenticationHandler,比如,使用QueryDatabaseAuthenticationHandler 或 SearchModeSearchDatabaseAuthenticationHandler 可以分别选取

  1. <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
  2.     <property name="dataSource" ref="casDataSource" />
  3.     <property name="sql" value="select password from xt_yhxx where lower(YHMC) = lower(?)" />
  4.     <property name="passwordEncoder"  ref="myPasswordEncoder"/>
  5. </bean>

或者

  1. <bean id="SearchModeSearchDatabaseAuthenticationHandler"  class="org.jasig.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler"
  2.       abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
  3.   <property  name="tableUsers">
  4.    <value>xt_yhxx</value>
  5.   </property>
  6.   <property name="fieldUser">
  7.    <value>YHMC</value>
  8.   </property>
  9.   <property name="fieldPassword">
  10.    <value>password</value>
  11.   </property>
  12.   <property name="dataSource" ref=" casDataSource " />
  13. </bean>

3、由于存放在数据库中的密码通常是加密过的,所以 AuthenticationHandler 在匹配时需要知道使用的加密方法,在 deployerConfigContext.xml 文件中我们可以为具体的 AuthenticationHandler 类配置一个 property,指定加密器类

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

CAS自带的DefaultPasswordEncoder默认有MD5和SHA1两种加密方式,我们这里指定加密方式为MD5。另外,这里 myPasswordEncoder 是根据实际情况自己定义的加密器,实现 PasswordEncoder 接口及其 encode() 方法。

4、将commons-pool-1.4.jar、commons-dbcp-1.2.2.jar、spring-jdbc-2.5.5.jar、commons-collections-3.2.jar、cas-server-support-jdbc-3.3.jar拷贝到%CATALINA_HOME%/webapps/cas/WEB-INF/lib中。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值