1. 程式人生 > >單點登陸cas實現3之cas伺服器配置資料來源

單點登陸cas實現3之cas伺服器配置資料來源

  之前的時候登陸使用者和密碼都是寫死在cas.properties檔案中,可實際上是需要從資料庫查詢,如下

 1.新增jar

       4.2.7版本我們需要新增的jar只有這兩 cas-server-support-jdbc-4.2.7.jar  和mysql-connector-java-5.1.34.jar

         複製到webapp/lib中即可.

 2.建立資料庫 

      e10adc3949ba59abbe56e057f20f883e為123456 的32位 小寫MD5加密


CREATE TABLE `fs_user` (
  `id` bigint(15) NOT NULL,
  `user_name` varchar(30) DEFAULT NULL,
  `user_password` varchar(255) DEFAULT NULL,
  `is_effective` varchar(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `fs_user` */

insert  into `fs_user`(`id`,`user_name`,`user_password`,`is_effective`) values (1,'junjun','e10adc3949ba59abbe56e057f20f883e','Y'),(2,'jun','e10adc3949ba59abbe56e057f20f883e','Y');

3.配置檔案修改

         deployerConfigContext.xml  檔案修改如下

         1.註釋掉下面一句配置(預設匹配cas中的使用者名稱和密碼)

    <alias name="acceptUsersAuthenticationHandler" alias="primaryAuthenticationHandler" /> 

        2.新增如下配置,MD5加密和資料來源

    <!--begin 從資料庫中的使用者表中讀取MD5 32位小寫 -->
    
    <bean id="MD5PasswordEncoder"
	class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"
	autowire="byName">
		<constructor-arg value="MD5" />
	</bean>
 
	<bean id="queryDatabaseAuthenticationHandler" name="primaryAuthenticationHandler"
		class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
		<property name="passwordEncoder" ref="MD5PasswordEncoder" />
	</bean> 
   
	<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.initialPoolSize}"
	  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}" />

   <!--end 從資料庫中的使用者表中讀取 MD5 32位小寫-->

   cas.properties中新增如下配置(記得替換使用者名稱和密碼什麼的)


# == Basic database connection pool configuration ==
database.driverClass=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1:3306/hap_dev?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull
database.user=root
database.password=java
database.pool.initialPoolSize=6
database.pool.minSize=6
database.pool.maxSize=18



# Amount of time in seconds after which idle connections
# in excess of minimum size are pruned.
database.pool.maxIdleTime=120

# Maximum amount of time to wait in ms for a connection to become
# available when the pool is exhausted
database.pool.maxWait=10000

# 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 ==


# == 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

# 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 end=============================
cas.jdbc.authn.query.sql=SELECT  user_password FROM fs_user WHERE user_name=? AND is_effective='Y'

      如上操作,配置什麼的都已完成,在啟動之前檢視下propertyFileConfigurer.xml中cas.cas.properties地址是否正確.

啟動,登陸