单点登陆cas实现3之cas服务器配置数据源

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yjaspire/article/details/82876218

  之前的时候登陆用户和密码都是写死在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地址是否正确.

启动,登陆

猜你喜欢

转载自blog.csdn.net/yjaspire/article/details/82876218
今日推荐