1.找到tomcat/webapp/cas/WEB-INF/deployerConfigContext.xml
2.在此文件中找到
<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />
3.注释掉上面那句(这个Handler只是判断用户名和密码相同即可通过)
4.在后面加上:
<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"> <property name="dataSource" ref="dataSource"></property> <property name="sql" value="select password from u_user where useraccount=?"></property> <property name="passwordEncoder" ref="MD5PasswordEncoder"></property> </bean>
5.上面配置的说明:
QueryDatabaseAuthenticationHandler是cas-server-support-jdbc提供的查询接口其中一个是通过配置一个 SQL 语句查出密码,与所给密码匹配;
dataSource是使用JDBC查询时的数据源;
sql语句就是查询哪一张表,本例根据u_user表的useraccount字段查询密码,CAS会匹配用户输入的密码,如果匹配则通过;
passwordEncoder这个就算是自己加的盐巴了,意思很明显就是处理密码的加密,看你的应用中数据库保存的是明码还是加密过的,比如本例是使用MD5加密的,所以配置了MD5PasswordEncoder这个Handler,cas内置了MD5的功能所以只需要配置一下就可以了;如果在实际应用中使用的是公司自己的加密算法那么就需要自己写一个Handler来处理密码,实现方式也比较简单,创建一个类继承org.jasig.cas.authentication.handler.PasswordEncoder然后在encode方法中加密用户输入的密码然后返回即可。
6.在deployerConfigContext.xml文件末尾加入:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property> <property name="url"><value>jdbc:oracle:thin:@192.168.12.114:1521:ora11g</value></property> <property name="username"><value>uacs</value></property> <property name="password"><value>uacs</value></property> </bean> <bean id="MD5PasswordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"> <constructor-arg index="0"> <value>MD5</value> </constructor-arg> </bean>
7.修改上面的数据源即可配置完成