1、 jar包准备,因为习惯使用bonecp数据源:
mysql jdbc驱动:mysql-connector-java-5.1.13-bin.jar
cas jdbc支持:cas-server-support-jdbc-3.4.11.jar
数据源:bonecp-0.7.1.RELEASE.jar、guava-r09.jar
2、编辑:WEB-INF\deployerConfigContext.xml,加入数据源:
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root"/> <property name="password" value="root"/> <property name="idleConnectionTestPeriod" value="60"/> <property name="idleMaxAge" value="240"/> <property name="maxConnectionsPerPartition" value="30"/> <property name="minConnectionsPerPartition" value="10"/> <property name="partitionCount" value="3"/> <property name="acquireIncrement" value="5"/> <property name="statementsCacheSize" value="100"/> <property name="releaseHelperThreads" value="3"/> </bean>将默认的规则验证方式注释,并加入新的验证:
<!-- <bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" /> --> <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"> <property name="dataSource" ref="dataSource" /> <property name="sql" value="select password from gg_user where loginname = ?" /> <property name="passwordEncoder" ref="passwordEncoder"/> </bean>其中passwordEncoder,我使用的是自定义的encoder:
<bean id="passwordEncoder" class="xx.xx.security.PassWordEncoder"/>
import org.jasig.cas.authentication.handler.PasswordEncoder; import org.springframework.security.authentication.encoding.ShaPasswordEncoder; public class PassWordEncoder implements PasswordEncoder { @Override public String encode(String args) { ShaPasswordEncoder encoder = new ShaPasswordEncoder(); encoder.setEncodeHashAsBase64(true); return encoder.encodePassword(args, null); } }
至此即实现了cas_server通过查询数据库验证用户名、密码正确性