創建一個MySQL數據庫,新建一張users表,添加username和password字段

第七步、配置數據庫認證
參考官方文檔:https://apereo.github.io/cas/4.2.x/installation/Database-Authentication.html#database-components
首先拷貝MySQL驅動到cas-server-webapp項目的lib目錄下

然后到下載的CAS源碼的cas-server-support-jdbc目錄下gradle build編譯jdbc的支持jar包

編譯好之后,同樣到build》libs目錄下拷貝cas-server-support-jdbc-4.2.7.jar包到cas-server-webapp項目的lib目錄下


打開deployerConfigContext.xml配置文件

將<alias name="acceptUsersAuthenticationHandler" alias="primaryAuthenticationHandler" />這一行注釋掉
加入
<alias name="queryDatabaseAuthenticationHandler" alias="primaryAuthenticationHandler" />
<alias name="dataSource" alias="queryDatabaseDataSource" />
<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}" />


,然后打開cas.properties,加入數據庫的相關配置如下:(請修改為自己的數據庫地址,用戶名和密碼)
# == Basic database connection pool configuration == database.driverClass=com.mysql.jdbc.Driver database.url=jdbc:mysql://127.0.0.1:3306/sso_test?useUnicode=true&characterEncoding=UTF-8& database.user=root database.password=admin 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.propeities進行修改,去掉“cas.jdbc.authn.query.sql=”前的注釋符,改為適合項目的語句,比如:
cas.jdbc.authn.query.sql=select password from sso_user where username=?
4) 允許注銷后可重定向(可選)
修改cas.propeities,去掉“cas.logout.followServiceRedirects=false”前的注釋符,改為:
cas.logout.followServiceRedirects=true
5) 修改TGT為永不失效策略
修改deployerConfigContext.xml,注釋掉原來的grantingTicketExpirationPolicy,修改為:
<!--<alias name="ticketGrantingTicketExpirationPolicy" alias="grantingTicketExpirationPolicy" />--> <alias name="neverExpiresExpirationPolicy" alias="grantingTicketExpirationPolicy" />
然后重啟tomcat,驗證登錄
輸入數據庫的賬號密碼

參考:https://www.cnblogs.com/wggj/p/7550361.html 和 https://www.cnblogs.com/jay763190097/p/6492005.html

