依托CAS已经实现的功能进行扩张。此实现方式是用自己编写dao层(非官方的实现方式),以便更容易的对CAS的登录流程有更深刻的了解。也可以使用官方提供的配置方式进行配置(只需要修改配置文件即可修改)
1.源码增加依赖(pom.xml)
<!-- 加入阿里的druid数据库连接池配置,可配置自己的喜欢的数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.5</version>
</dependency>
<!-- 加入ORCALE 的jdbc包 -->
<dependency>
<groupId>ojdbc6</groupId>
<artifactId>ojdbc6</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/ojdbc6.jar</systemPath>
</dependency>
<!-- cas-server-support-jdbc -->
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-support-jdbc</artifactId>
<version>${project.version}</version>
</dependency>
2.增加DAO 层
public interface AccountDao {
public Account getAccountInfo(String name);
}
public class AccountDaoImpl implements AccountDao {
public JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public Account getAccountInfo(String name) {
return jdbcTemplate.queryForObject("select * from sys_account where username = "+name,Account.class);
}
}
3.增加SERCICE
public interface AccountService {
boolean checkAccount(String username, String password);
}
public class AccountServiceImpl implements AccountService {
public AccountDao accountDao;
/**
* 加密器
*/
public PasswordEncoder passwordEncoder;
@Override
public boolean checkAccount(String username, String password) {
Account account = accountDao.getAccountInfo(username);
if(account != null)
{
password = passwordEncoder.encode(password);
if(password.equals(account.getPassWord()))
{
return true;
}
}
return false;
}
public PasswordEncoder getPasswordEncoder() {
return passwordEncoder;
}
public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder;
}
public AccountDao getAccountDao() {
return accountDao;
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
}
4.增加用户登录校验处理器
public class UsersAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {
private AccountService accountService;
public UsersAuthenticationHandler() {
}
protected final HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential) throws GeneralSecurityException, PreventedException {
String username = credential.getUsername();
String password = credential.getPassword();
if(password == null) {
this.logger.debug("{} was not found in the map.", username);
throw new AccountNotFoundException(username + " not found in backing map.");
}
else {
boolean flag = accountService.checkAccount(username, password);
if (!flag) {
throw new FailedLoginException();
}
else {
return this.createHandlerResult(credential, this.principalFactory.createPrincipal(username), (List)null);
}
}
}
public AccountService getAccountService() {
return accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}
5.增加数据库配置文件(\webapp\WEB-INF\spring-configuration\applicationContext-datasource.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<description>datasource</description>
<bean id="casDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="driverClassName" value="${driverClassName}" />
<property name="maxActive" value="${maxActive}" />
<property name="initialSize" value="${initialSize}" />
<property name="maxWait" value="${maxWait}" />
<property name="minIdle" value="${minIdle}" />
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
<property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打开removeAbandoned功能 -->
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分钟 -->
<property name="logAbandoned" value="${logAbandoned}" /> <!-- 关闭abanded连接时输出错误日志 -->
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="casDataSource" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="casDataSource" />
<!-- 通过AOP配置提供事务增强,让AccountService下所有Bean的所有方法拥有事务 -->
<aop:config>
<aop:pointcut id="serviceMethod" expression=" execution(* com.ucap.igsd.cas.service.impl..*(..))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 注入相关的beans -->
<bean id="accountService" class="com.ucap.igsd.cas.service.impl.AccountServiceImpl" p:accountDao-ref="accountDao" p:passwordEncoder-ref="MD5PasswordEncoder"/>
<bean id="accountDao" class="com.ucap.igsd.cas.dao.impl.AccountDaoImpl" p:jdbcTemplate-ref="jdbcTemplate"/>
</beans>
6.增加数据库属性文件(\webapp\WEB-INF\dbconfig.properties)
driverClassName:oracle.jdbc.driver.OracleDriver
url:jdbc:oracle:thin:@localhost:1521:ORCL
username:test
password:123456
filters:stat
maxActive:20
initialSize:1
maxWait:60000
minIdle:10
maxIdle:15
timeBetweenEvictionRunsMillis:60000
minEvictableIdleTimeMillis:300000
validationQuery:SELECT 'x'
testWhileIdle:true
testOnBorrow:false
testOnReturn:false
maxOpenPreparedStatements:20
removeAbandoned:true
removeAbandonedTimeout:1800
logAbandoned:true
7.修改属性文件读取配置(propertyFileConfigurer.xml),不然数据库的属性读取不到
<!--
<util:properties id="casProperties" location="${cas.properties.filepath:/WEB-INF/cas.properties}"/>
<context:property-placeholder properties-ref="casProperties"/>
-->
<util:properties id="casProperties" location="${cas.properties.filepath:/WEB-INF/*.properties}"/>
<context:property-placeholder properties-ref="casProperties"/>
8.修改用户验证配置deployerConfigContext.xml
<!-- 注释原有的固定配置 -->
<!--
<bean id="primaryAuthenticationHandler"
class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">
<property name="users">
<map>
<entry key="casuser" value="Mellon"/>
</map>
</property>
</bean>
-->
<!-- 注入密码加密beans -->
<bean id="MD5PasswordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">
<constructor-arg value="MD5"/>
</bean>
<!-- 添加自定义用户校验方法 -->
<bean id="primaryAuthenticationHandler" class="com.ucap.igsd.cas.handler.UsersAuthenticationHandler">
<property name="accountService" ref="accountService" />
</bean>