JAVA CAS单点登录之四:CAS服务器增加JDBC访问能力

简介:

现在的CAS服务器认证功能弱到爆了,使用模式自带的认证Handler,用在模拟测试玩玩上课,用在系统中,压根还不行。这一节,咱们就增加这一方面的功能。

主要内容

1.用户名和密码从数据库中读取(用户量有保障)

2.密码保障


前言

    咱们知道CAS默认提供的认证服务,只需要输入的用户名和密码相同,即可通过认证。咱们先要找到这一块的配置信息。

在deployerConfigContext.xml中找到代码片段如下

1
< bean   class = "org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public  final  class  SimpleTestUsernamePasswordAuthenticationHandler  extends
     AbstractUsernamePasswordAuthenticationHandler {
 
     public  SimpleTestUsernamePasswordAuthenticationHandler() {
         log
             .warn( this .getClass().getName()
                 " is only to be used in a testing environment.  NEVER enable this in a production environment." );
     }
 
     public  boolean  authenticateUsernamePasswordInternal( final  UsernamePasswordCredentials credentials) {
         final  String username = credentials.getUsername();
         final  String password = credentials.getPassword();
 
         if  (StringUtils.hasText(username) && StringUtils.hasText(password)
             && username.equals(getPasswordEncoder().encode(password))) {
             log
                 .debug( "User ["  + username
                     "] was successfully authenticated." );
             return  true ;
         }
 
         log.debug( "User ["  + username +  "] failed authentication" );
 
         return  false ;
     }
}

通过分析代码,应该很容易看明白。如果大家对面向接口编程有一定了解的话,就应该知道咱们下一步是做什么了。重点分析SimpleTestUsernamePasswordAuthenticationHandler 的结构树。

wKiom1bwAqjg3qrSAABsIh34ASU266.png

这时候应该留意到了QueryDatabaseAuthenticationHandler这个类,没错。接下来的这一章的过程实质上就是一步步将SimpleTestUsernamePasswordAuthenticationHandler替换为QueryDatabaseAuthenticationHandler的过程。


  1. 添加依赖(cas jdbc支持包,及数据库驱动)

    修改pom.xml,增加2个依赖。

  org.jasig.cas
  cas-server-support-jdbc
  3.5.3



  mysql
  mysql-connector-java
  5.1.38

如果要使用commons-dbcp的话,请自行添加以下依赖

  • commons-collections-3.2.jar

  • commons-dbcp-1.2.1.jar

  • commons-pool-1.3.jar

2.修改deployerConfigContext.xml文件

准备数据库环境

1
2
3
4
5
6
7
8
9
10
DROP  TABLE  IF EXISTS `t_users`;
CREATE  TABLE  `t_users` (
   `user_name`  varchar (32)  DEFAULT  NULL ,
   ` password varchar (64)  DEFAULT  NULL
) ENGINE=InnoDB  DEFAULT  CHARSET=utf8;
 
-- ----------------------------
-- Records of t_users
-- ----------------------------
INSERT  INTO  `t_users`  VALUES  ( 'jdbc_user' 'jdbc_password' );

密码暂时是明文


2.1 注释掉SimpleTestUsernamePasswordAuthenticationHandler

1
<!--<bean  class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler"/>-->

2.2增加QueryDatabaseAuthenticationHandler相关配置

1
2
3
4
5
6
7
8
9
10
11
12
< bean  id = "authenticationManager"
       class = "org.jasig.cas.authentication.AuthenticationManagerImpl" >
       ...
      < property  name = "authenticationHandlers"
      < list >
< bean  class = "org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" >
     < property  name = "dataSource"  ref = "dataSource"  />
     < property  name = "sql"  value = "select password from t_users where lower(user_name) = lower(?)"  />
</ bean >
</ list >
</ property >
</ bean >

其中,没有考虑加密处理,别急,一步步来。

到这一步就把JDBC集成到CAS服务了了。

2.3.验证,输入jdbc_user/jdbc_password,认证通过。

2.4增加密码认证功能,上面的结构树大家如果注意到了passwordEncoder属性,接下来就应该知道怎么了。没错为QueryDatabaseAuthenticationHandler的passwordEncoder属性赋值

2.5 完整配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
...
< bean  id = "authenticationManager"
       class = "org.jasig.cas.authentication.AuthenticationManagerImpl" >
 
 
     < property  name = "credentialsToPrincipalResolvers" >
         < list >
 
             < bean  class = "org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver" >
                 < property  name = "attributeRepository"  ref = "attributeRepository" />
             </ bean >
  
             < bean
                     class = "org.jasig.cas.authentication.principal.HttpBasedServiceCredentialsToPrincipalResolver" />
         </ list >
     </ property >
 
 
     < property  name = "authenticationHandlers" >
         < list >
   
             < bean  class = "org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
                   p:httpClient-ref = "httpClient"  p:requireSecure = "false" />
 
             <!--
             <bean  class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler"/>
             -->
             < bean  class = "org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" >
                 < property  name = "dataSource"  ref = "dataSource"  />
                 < property  name = "sql"  value = "select password from t_users where lower(user_name) = lower(?)"  />
                 < property  name = "passwordEncoder"  ref = "passwordEncoder" />
             </ bean >
         </ list >
     </ property >
</ bean >
< bean  id = "passwordEncoder"  class = "org.jasig.cas.authentication.handler.DefaultPasswordEncoder"  p:characterEncoding = "UTF-8"  >
     < constructor-arg  index = "0"  value = "MD5"  />
</ bean >
<!-- Data source definition -->
< bean  id = "dataSource"  class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
     < property  name = "driverClassName" >
         < value >com.mysql.jdbc.Driver</ value >
     </ property >
     < property  name = "url" >
         < value >jdbc:mysql://localhost:3306/exampledb</ value >
     </ property >
     < property  name = "username" >
         < value >root</ value >
     </ property >
     < property  name = "password" >
         < value >root</ value >
     </ property >
</ bean >
...

2.6 认证

这时候需要手动改一下数据库记录的密码了

apple 对应的MD5字符串是1f3870be274f6c49b3e31a0c6728957f。

也可以执行如下语句

1
INSERT  INTO  `t_users`  VALUES  ( 'jdbc_user' '1f3870be274f6c49b3e31a0c6728957f' );

2.7 重启服务器,输入jdbc_user/apple ,认证通过。


最后,其他数据库的话,可以如下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!-- Oracle connector -->
< bean  id = "dataSource"  class = "org.apache.commons.dbcp.BasicDataSource" >
  < property  name = "driverClassName" >
   < value >oracle.jdbc.driver.OracleDriver</ value >
  </ property >
  < property  name = "url" >
   < value >jdbc:oracle:thin:@database-server-name:1521:SID</ value >
  </ property >
  < property  name = "username" >
   < value >admusr</ value >
  </ property >
  < property  name = "password" >
   < value >admpwd</ value >
  </ property >
</ bean >
  
<!-- MySQL connector -->
< bean  id = "dataSource"  class = "org.apache.commons.dbcp.BasicDataSource" >
  < property  name = "driverClassName" >
   < value >com.mysql.jdbc.Driver</ value >
  </ property >
  < property  name = "url" >
   < value >jdbc:mysql://database-server-name:3306/db-name</ value >
  </ property >
  < property  name = "username" >
   < value >admusr</ value >
  </ property >
  < property  name = "password" >
   < value >admpwd</ value >
  </ property >
</ bean >
  
  
<!-- PostgreSQL connector -->
< bean  id = "dataSource"  class = "org.apache.commons.dbcp.BasicDataSource" >
  < property  name = "driverClassName" >
   < value >org.postgresql.Driver</ value >
  </ property >
  < property  name = "url" >
   < value >jdbc:postgresql://database-server-name:5432/db-name</ value >
  </ property >
  < property  name = "username" >
   < value >admusr</ value >
  </ property >
  < property  name = "password" >
   < value >admpwd</ value >
  </ property >
</ bean >



本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1753680,如需转载请自行联系原作者
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
10月前
|
SQL Java 数据库连接
除了JDBC,还有哪些常见的数据库访问技术?
除了JDBC,还有哪些常见的数据库访问技术?
792 2
|
11月前
|
存储 数据挖掘 Linux
服务器数据恢复—重装系统导致OceanStor存储上的分区无法访问的数据恢复案例
服务器存储数据恢复环境: 华为OceanStor某型号存储+扩展盘柜,存储中的硬盘组建了raid5磁盘阵列,上层分配了1个lun。 linux操作系统,划分了两个分区,分区一通过lvm扩容,分区二为xfs文件系统。 服务器存储故障: 工作人员重装系统操作失误导致磁盘分区变化,分区二无法访问,数据丢失。
|
NoSQL Java 数据库连接
实战|StarRocks 通过 JDBC Catalog 访问 MongoDB 的数据
本文章介绍如何通过 StarRocks 的 JDBC Catalog 功能,结合 MongoDB BI Connector,将 MongoDB 数据便捷接入 StarRocks,实现数据打通和 SQL 查询分析,以下是整体流程图。
|
安全 Java 程序员
【高薪程序员必看】万字长文拆解Java并发编程!(6-2):从CAS无锁机制到Atomic原子类实战指南
🌟 ​🌟今天给大家带来的是 ​💻⚡在这篇文章中,我们将一起探索:🔹 ​的底层原理,它是如何通过 ​实现无锁并发的?🔹 ​的终极对决,为什么高并发场景下CAS性能更优?🔹 ​的陷阱与解决方案——和实战演示!🔹 ​​(LongAdder等)的使用场景与性能对比🔹 危险的 ​黑魔法:为什么阿里禁止使用却又是并发库的基石?无论你是:✅ ​​(BATJ高频考点)✅ ​​(如何设计百万级计数器)✅ ​​(从Java代码到CPU指令的全链路分析)这篇文章都会让你收获满满!✨。
215 0
|
安全 Java 程序员
【高薪程序员必看】万字长文拆解Java并发编程!(6-1):从CAS无锁机制到Atomic原子类实战指南
🌟 ​🌟今天给大家带来的是 ​💻⚡在这篇文章中,我们将一起探索:🔹 ​的底层原理,它是如何通过 ​实现无锁并发的?🔹 ​的终极对决,为什么高并发场景下CAS性能更优?🔹 ​的陷阱与解决方案——和实战演示!🔹 ​​(LongAdder等)的使用场景与性能对比🔹 危险的 ​黑魔法:为什么阿里禁止使用却又是并发库的基石?无论你是:✅ ​​(BATJ高频考点)✅ ​​(如何设计百万级计数器)✅ ​​(从Java代码到CPU指令的全链路分析)这篇文章都会让你收获满满!✨。
234 0
|
安全 Linux
阿里云linux服务器使用脚本通过安全组屏蔽异常海外访问ip
公网网站可能会遭受黑客攻击导致访问异常,使用此脚本可以屏蔽掉异常IP 恢复访问。也可自行设置定时任务定期检测屏蔽。
960 28
|
Java Linux 定位技术
Minecraft配置文件参数说明(JAVA服务器篇)
Minecraft JAVA版服务器启动后会生成server.properties配置文件,位于minecraft_server/根目录下。该文件包含多项关键设置,如游戏模式(gamemode)、最大玩家数(max-players)、难度(difficulty)等。此文档详细说明了各配置项的功能与默认值,帮助用户高效管理服务器环境。
4780 62
|
前端开发 Cloud Native Java
Java||Springboot读取本地目录的文件和文件结构,读取服务器文档目录数据供前端渲染的API实现
博客不应该只有代码和解决方案,重点应该在于给出解决方案的同时分享思维模式,只有思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
Java||Springboot读取本地目录的文件和文件结构,读取服务器文档目录数据供前端渲染的API实现
|
域名解析 弹性计算 应用服务中间件
使用域名访问部署在ECS上的网站
本文为您介绍如何为网站配置域名并为域名配置HTTPS证书。
|
存储 算法 数据挖掘
服务器数据恢复—nas中raid6阵列失效,存储无法访问的数据恢复案例
一台nas上共有14块硬盘组建了一组raid6磁盘阵列。 该nas在工作过程中,raid6阵列中硬盘出现故障离线,导致raid6阵列失效,nas无法正常访问。