CAS使用数据库进行用户认证

9月 21, 2016 |

相关的视频教程观看地址:单点登录系列视频教程

1、新建一张用户表

需要保护用户名和密码字段,我下文使用的是cas_test数据库下的cas_user表

2、修改pom.xml,引入相关依赖

<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-support-jdbc</artifactId>
<version>4.2.2</version>
</dependency>

<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
这三个依赖分别是CAS对数据库支持jar,连接池jar, 和mysql驱动jar。 cas-server-support-jdbc的版本和你cas-overlay-template工程的版本一致

3、修改deployerConfigContext.xml 文件,添加如下配置

<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:driverClass="${database.driverClass}"
p:jdbcUrl="${database.url}"
p:user="${database.user}"
p:password="${database.password}"
p:initialPoolSize="${database.pool.minSize}"
p:minPoolSize="${database.pool.minSize}"
p:maxPoolSize="${database.pool.maxSize}"
p:maxIdleTimeExcessConnections="${database.pool.maxIdleTime}"
p:checkoutTimeout="${database.pool.maxWait}"
p:acquireIncrement="${database.pool.acquireIncrement}"
p:acquireRetryAttempts="${database.pool.acquireRetryAttempts}"
p:acquireRetryDelay="${database.pool.acquireRetryDelay}"
p:idleConnectionTestPeriod="${database.pool.idleConnectionTestPeriod}"
p:preferredTestQuery="${database.pool.connectionHealthQuery}" />

<!-- <alias name="acceptUsersAuthenticationHandler" alias="primaryAuthenticationHandler" /> -->
<alias name="queryDatabaseAuthenticationHandler" alias="primaryAuthenticationHandler" />
<alias name="defaultPasswordEncoder" alias="passwordEncoder" />
<alias name="dataSource" alias="queryDatabaseDataSource" />
对应的内容分别是声明了一个id为dataSource的数据源,对数据源添加了别名queryDatabaseDataSource,然后对id=defaultPasswordEncoder的密码加密器添加别名passwordEncoder,注释掉primaryAuthenticationHandler别名的声明,让别名primaryAuthenticationHandler其指向queryDatabaseAuthenticationHandler

4、修改cas.properties

database.driverClass=com.mysql.jdbc.Driver"
database.url=jdbc:mysql://localhost:3306/cas_test
database.user=root
database.password=123456
database.pool.minSize=1
database.pool.maxSize=5
database.pool.maxIdleTime=120
database.pool.maxWait=10000
database.pool.acquireIncrement=1
database.pool.acquireRetryAttempts=5
database.pool.acquireRetryDelay=200
database.pool.idleConnectionTestPeriod=30
database.pool.connectionHealthQuery=select 1
cas.jdbc.authn.query.sql=select password from cas_user where username= ?
cas.authn.password.encoding.alg=MD5
cas.authn.password.encoding.char=UTF-8

参考信息

https://apereo.github.io/cas/4.2.x/installation/Database-Authentication.html

Posted in: MySQL practise

Comments are closed.