我们在上一篇文章中已经讲解了cas4.2.X登录启用mongodb验证方式

单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程

但是密码是明文存储的,也就是说 数据库里password存的是什么,跟用户填写的密码是一样的。

但是一般来说 我们需要对用户的密码进行加密后才存储入库。

登录时对照密码 就需要对用户填写的密码进行同种类型的加密之后再对照。

cas加密方式分析

cas5.0.X默认提供了4种配置

# cas.authn.mongo.passwordEncoder.type=NONE|DEFAULT|STANDARD|BCRYPT

可以参考文档

http://static.javadoc.io/org.apereo.cas/cas-server/5.1.0-RC1/org/apereo/cas/configuration/model/core/authentication/PasswordEncoderProperties.PasswordEncoderTypes.html

这四种方式其实脱胎于spring security中的加密方式。

spring security先后提供了MD5PasswordEncoder和SHAPasswordEncoder加密以及比较新的StandardPasswordEncoder和BCryptPasswordEncoder。

MD5PasswordEncoder和SHAPasswordEncoder加密是编码算法加密。现在cas把他们归属于DefaultPasswordEncoder。

我们来看看配置

NONE
public static final PasswordEncoderProperties.PasswordEncoderTypes NONE
No password encoding will take place.

说明对密码不做任何加密,也就是保留明文。

DEFAULT
public static final PasswordEncoderProperties.PasswordEncoderTypes DEFAULT
Uses an encoding algorithm and a char encoding algorithm.

说明启用DefaultPasswordEncoder,但是DefaultPasswordEncoder需要带参数encodingAlgorithm,如下

# cas.authn.accept.passwordEncoder.encodingAlgorithm=MD5或者SHA或者SHA1(需要看看版本中支持哪些)

STANDARD
public static final PasswordEncoderProperties.PasswordEncoderTypes STANDARD
Uses StandardPasswordEncoder.

说明启用了StandardPasswordEncoder加密方式

BCRYPT
public static final PasswordEncoderProperties.PasswordEncoderTypes BCRYPT
Uses BCryptPasswordEncoder.

说明启用了BCryptPasswordEncoder加密方式

cas加密方式使用

环境配置和密码生成

如果我们要启用某种加密方式,那cas server就会把用户填写的密码根据这种加密方式去加密之后才跟数据库中的 密码进行对比。

所以我们在 写注册功能时 就需要把用户密码 根据加密方式 加密之后 再保存入库。

例如 密码为123456,启用不同的加密方式,分别需要填入如下密码:

(我们在cas-client的其中一个client2中写main方法来测试注册密码)

如图DefaultPasswordEncoder只支持MD5和SHA1。

DefaultPasswordEncoder在cas-server-core-authentication包中:

org.jasig.cas.authentication.handler.DefaultPasswordEncoder

StandardPasswordEncoder和BCryptPasswordEncoder 在cas-server-core-configuration包中可以看到它们来源于:

org.springframework.security.crypto。

也就是spring-security-crypto包。

查找类的方法可以ctrl+shift+T输入项目名查找本地项目的包。

如果本地没有可以在github中查找。

如图:

所以我们在client2导入cas-server-core-authentication和spring-security-crypto包即可。

我们在maven网站查找包名

http://www.mvnrepository.com/artifact/org.jasig.cas/cas-server-core-authentication/4.2.7

所以我们需要添加包

<!-- https://mvnrepository.com/artifact/org.jasig.cas/cas-server-core-authentication -->
<dependency>
    <groupId>org.jasig.cas</groupId>
    <artifactId>cas-server-core-authentication</artifactId>
    <version>4.2.7</version>

</dependency>

<dependency>  
            <groupId>org.springframework.security</groupId>  
            <artifactId>spring-security-crypto</artifactId>  
            <version>3.1.3.RELEASE</version>  
        </dependency> 
    <dependency>

所以我们的代码为:

package client2;

import org.jasig.cas.authentication.handler.DefaultPasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;

public class testPassword {
	public static void main(String[] args) {

		DefaultPasswordEncoder defaultPasswordEncoderMD5 = new DefaultPasswordEncoder(
				"MD5");
		System.out.println(defaultPasswordEncoderMD5.encode("123456"));

		DefaultPasswordEncoder defaultPasswordEncoderSHA = new DefaultPasswordEncoder(
				"SHA1");
		System.out.println(defaultPasswordEncoderSHA.encode("123456"));

		StandardPasswordEncoder standardPasswordEncoder = new StandardPasswordEncoder();
		System.out.println(standardPasswordEncoder.encode("123456"));

		BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
		System.out.println(bCryptPasswordEncoder.encode("123456"));

	}

}

运行main方法后得到 加密后的密码如下:

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
659b5c8dd3c2d4e8fcbeb4f329e9e4cadf18409a31abc1dbeb303c66463935bf6791bbc79507ac82
$2a$10$ulE.UTx./XZNnEK1rNTmCuurlMOhKxQRzM0i2PKAAbkTBH9BCLQw2

使用NONE模式

NONE是对密码不加密,跟不配置是一样的效果,验证时明文对比。

cas.properties
中新增cas.authn.mongo.passwordEncoder.type=NONE

如图:

cas.authn.mongo.collection.name=dataManager
cas.authn.mongo.db.host=mongodb://192.168.30.249:27017/testCrm
#cas.authn.mongo.attributes=username,password,permissionList,roleList,createtime,ower
cas.authn.mongo.username.attribute=username
cas.authn.mongo.password.attribute=password
cas.authn.mongo.passwordEncoder.type=NONE

数据库中的密码明文存储:

username是crm,密码是123456

{
	"_id": ObjectId('5743bf4e0cf2b3488bad9c98'),
	"_class": "com.test.domain.entity.DataManager",
	"username": "crm",
	"password": "123456",
	"permissionList": [
		"parseResultAdd",
		"parseResultAddMulti",
		"resultlist"
	],
	"roleList": [
		"normal"
	],
	"createtime": "May 24, 2016 10:41:18 AM",
	"ower": "crm"
}

使用crm,123456登录成功。

说明密码确实没有加密认证。

使用DEFAULT的MD5模式

cas.properties
中新增

cas.authn.mongo.passwordEncoder.type=DEFAULT

而且DEFAULT模式有细分,所以这里需要指定,我们指定为MD5模式。

新增

cas.authn.mongo.passwordEncoder.encodingAlgorithm=MD5

cas.authn.mongo.collection.name=dataManager
cas.authn.mongo.db.host=mongodb://192.168.30.249:27017/testCrm
#cas.authn.mongo.attributes=username,password,permissionList,roleList,createtime,ower
cas.authn.mongo.username.attribute=username
cas.authn.mongo.password.attribute=password
cas.authn.mongo.passwordEncoder.type=DEFAULT
cas.authn.mongo.passwordEncoder.encodingAlgorithm=MD5

数据库中的密码需要new DefaultPasswordEncoder("MD5")encode出来的密码存储:

根据我们之前的encode当密码是123456时数据库存储如下:

username是crm,密码是e10adc3949ba59abbe56e057f20f883e

{
	"_id": ObjectId('5743bf4e0cf2b3488bad9c98'),
	"_class": "com.test.domain.entity.DataManager",
	"username": "crm",
	"password": "e10adc3949ba59abbe56e057f20f883e",
	"permissionList": [
		"parseResultAdd",
		"parseResultAddMulti",
		"resultlist"
	],
	"roleList": [
		"normal"
	],
	"createtime": "May 24, 2016 10:41:18 AM",
	"ower": "crm"
}

然后使用crm和123456登录即可。

思考和分析

在cas server 5.X的版本以上其他DEFAULT的SHA1模式和STANDARD模式以及BCRYPT模式根据上面的情况配置即可。

但是我现在用的是cas 4.2.X,看了下源码发现 cas 4.2.X并不支持这四种参数的配置。也就是说cas server 5.X中这些关于mongo的加密方式是不适合cas4.2.x的版本使用的。

cas4.2.x中使用的配置参数是mongoPac4jPasswordEncoder,只支持BasicSaltedSha512PasswordEncoder和PasswordEncoder这两种方式。

那么我们如果要实现跟5.X版本一样的几种加密方式或者  自定义的加密方式,则需要对这部分的代码和配置进行修改。

单点登录(十四)-----实战-----cas5.0.x登录mongodb验证方式常规的四种加密的思考和分析的更多相关文章

  1. 单点登录(十五)-----实战-----cas4.2.x登录mongodb验证方式实现自定义加密

    我们在前一篇文章中实现了cas4.2.x登录使用mongodb验证方式. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程 也学习参考了cas5.0.x版 ...

  2. 单点登录(十七)----cas4.2.x登录mongodb验证方式成功后返回更多信息更多属性到客户端

    我们在之前已经完成了cas4.2.x登录使用mongodb验证方式登录成功了.也解决了登录名中使用中文乱码的问题. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方 ...

  3. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程

    我们在之前的文章中中已经讲到了正确部署运行cas server 和 在cas client中配置. 在此基础上 我们去掉了https的验证,启用了http访问的模式. 单点登录(七)-----实战-- ...

  4. 单点登录(十二)-----遇到问题-----cas启用mongodb验证方式登录后没反应-pac4j-mongo包中的MongoAuthenticatInvocationTargetException

    cas启用mongodb验证方式登录后没反应 控制台输出 2017-02-09 20:27:15,766 INFO [org.jasig.cas.authentication.MongoAuthent ...

  5. 单点登录(十)-----遇到问题-----cas启用mongodb验证方式报错com.mongodb.CommandFailureException---Authentication failed

    cas启用mongodb验证方式报错com.mongodb.CommandFailureException---Authentication failed. 完整报错信息: 二月 08, 2017 5 ...

  6. 单点登录(十一)-----遇到问题-----cas启用mongodb验证方式报错--Unable to locate Spring NamespaceHandler for XML schema na

    cas启用mongodb验证方式报错--Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.sp ...

  7. 单点登录(十六)-----遇到问题-----cas4.2.x登录成功后报错No principal was found---cas中文乱码问题完美解决

    情况 我们之前已经完成了cas4.2.x登录使用mongodb验证方式并且自定义了加密. 单点登录(十五)-----实战-----cas4.2.x登录mongodb验证方式实现自定义加密 但是悲剧的是 ...

  8. 单点登录(十八)----cas4.2.x客户端增加权限控制shiro

    我们在上面章节已经完成了cas4.2.x登录启用mongodb的验证方式. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程 也完成了获取管理员身份属性 ...

  9. OAuth2简易实战(四)-Github社交联合登录

    1. OAuth2简易实战(四)-Github社交联合登录 1.1. 用到的第三方插件 https://github.com/spring-projects/spring-social-github ...

随机推荐

  1. 【Docker】第一篇 Docker的初始化安装部署

    一.Docker基础 Dacker倡导的理念:一个容器一个进程 Docker的版本了解: Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE. 社区版是免费提供给个人 ...

  2. 06-matplotlib-饼状图

    import numpy as np import matplotlib.pyplot as plt ''' 饼状图显示一个数据系列中各项总和的比例: 饼状图中的数据点显示为整个饼状图的百分比: 如: ...

  3. 04-matplotlib-柱形图

    import numpy as np import matplotlib.pyplot as plt # 柱形图 # 例一 N =5 y = [15,28,10,30,25] index = np.a ...

  4. iOS 开发中,关于xxx.xcodeproj 文件冲突的解决方案 (以后谁不会了,直接将连接给他)

    iOS 开发中,关于xxx.xcodeproj 文件冲突的解决方案 (一有冲突要手把手教一遍,太麻烦了,现在总结下,以后谁不会了,连接直接发他). 关于xxx.xcodeproj 文件冲突的话,是比较 ...

  5. Scrum Meeting 11.06

    成员 今日任务 明日计划 用时 徐越 学习ListView+simpleAdapter,actionBar.阅读并修改前端代码 继续修改前端代码.完善数据库 4h 赵庶宏  构建后端数据库,进行完善 ...

  6. 20172324《Java程序设计》第3周学习总结

    20172324<Java程序设计>第3周学习总结 教材学习内容总结 随机数,记住要返回的是指定的字符前一个. String类型的一些用法,例如concat(连接),toUpperCase ...

  7. stateful openflow------整理openstate原理以及具体应用

    openstate基本思想就是控制器下放一部分功能,交换机不再是简单的dumb,而是保留一些简单的wise. 论文中以端口锁定为例,提出了米粒型状态机在交换机内部的应用从而可以大大减少交换机和控制器之 ...

  8. HDU 1003 最大连续子段和

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)M ...

  9. python learning IO.py

    f = open('test.txt', 'r') # 'r' 表示只读 s = f.read() # 调用read()方法可以一次读取文件的全部内容,Python把内容读到内存,用一个str对象表示 ...

  10. unique STL讲解和模板

    unique()是C++标准库函数里面的函数,其功能是去除相邻的重复元素(只保留一个),所以使用前需要对数组进行排序. 代码: #include<bits/stdc++.h> using ...