cas默认是读取cas-server-webapp下的cas.properties文件中的
accept.authn.users=casuser::Mellon
作为登录的用户名和密码的
下面我们修改使cas从数据库中获取用户名和密码
1.cas-server-webapp下的cas.properties
添加
# == Basic database connection pool configuration ==
database.driverClass=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
database.user=root
database.password=root
database.pool.minSize=6
database.pool.maxSize=18
# Maximum amount of time to wait in ms for a connection to become
# available when the pool is exhausted
database.pool.maxWait=10000
# Amount of time in seconds after which idle connections
# in excess of minimum size are pruned.
database.pool.maxIdleTime=120
# Number of connections to obtain on pool exhaustion condition.
# The maximum pool size is always respected when acquiring
# new connections.
database.pool.acquireIncrement=6
# == Connection testing settings ==
# Period in s at which a health query will be issued on idle
# connections to determine connection liveliness.
database.pool.idleConnectionTestPeriod=30
# Query executed periodically to test health
database.pool.connectionHealthQuery=select 1
# == Database recovery settings ==
# Number of times to retry acquiring a _new_ connection
# when an error is encountered during acquisition.
database.pool.acquireRetryAttempts=5
# Amount of time in ms to wait between successive aquire retry attempts.
database.pool.acquireRetryDelay=2000
cas.jdbc.authn.query.sql=select password from test_a where name=?
注:查询的sql固定格式为cas.jdbc.authn.query.sql=select 密码字段 from 表名 where 用户名字段=?
2.cas-server-webapp下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="queryDatabaseAuthenticationHandler" alias="primaryAuthenticationHandler" />
<alias name="dataSource" alias="queryDatabaseDataSource" />
<alias name="dataSource" alias="queryEncodeDatabaseDataSource" />
注释或删除
<!--<alias name="acceptUsersAuthenticationHandler" alias="primaryAuthenticationHandler" />-->
3.主目录下的build.gradle文件的dependencies属性中添加
compile ("mysql:mysql-connector-java:5.1.24")
这里使用的是mysql数据库
4.cas-server-webapp下的build.gradle的dependencies添加
compile project(':cas-server-support-jdbc')
因为deployerConfigContext.xml中添加的queryDatabaseAuthenticationHandler是属于cas-server-support-jdbc模块的,因此要在cas-server-webapp中添加对cas-server-support-jdbc的依赖