单点登录(十四)-----实战-----cas5.0.x登录mongodb验证方式常规的四种加密的思考和分析
我们在上一篇文章中已经讲解了cas4.2.X登录启用mongodb验证方式
单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程
但是密码是明文存储的,也就是说 数据库里password存的是什么,跟用户填写的密码是一样的。
但是一般来说 我们需要对用户的密码进行加密后才存储入库。
登录时对照密码 就需要对用户填写的密码进行同种类型的加密之后再对照。
cas加密方式分析
cas5.0.X默认提供了4种配置
# cas.authn.mongo.passwordEncoder.type=NONE|DEFAULT|STANDARD|BCRYPT
可以参考文档
这四种方式其实脱胎于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验证方式常规的四种加密的思考和分析的更多相关文章
- 单点登录(十五)-----实战-----cas4.2.x登录mongodb验证方式实现自定义加密
我们在前一篇文章中实现了cas4.2.x登录使用mongodb验证方式. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程 也学习参考了cas5.0.x版 ...
- 单点登录(十七)----cas4.2.x登录mongodb验证方式成功后返回更多信息更多属性到客户端
我们在之前已经完成了cas4.2.x登录使用mongodb验证方式登录成功了.也解决了登录名中使用中文乱码的问题. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方 ...
- 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程
我们在之前的文章中中已经讲到了正确部署运行cas server 和 在cas client中配置. 在此基础上 我们去掉了https的验证,启用了http访问的模式. 单点登录(七)-----实战-- ...
- 单点登录(十二)-----遇到问题-----cas启用mongodb验证方式登录后没反应-pac4j-mongo包中的MongoAuthenticatInvocationTargetException
cas启用mongodb验证方式登录后没反应 控制台输出 2017-02-09 20:27:15,766 INFO [org.jasig.cas.authentication.MongoAuthent ...
- 单点登录(十)-----遇到问题-----cas启用mongodb验证方式报错com.mongodb.CommandFailureException---Authentication failed
cas启用mongodb验证方式报错com.mongodb.CommandFailureException---Authentication failed. 完整报错信息: 二月 08, 2017 5 ...
- 单点登录(十一)-----遇到问题-----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 ...
- 单点登录(十六)-----遇到问题-----cas4.2.x登录成功后报错No principal was found---cas中文乱码问题完美解决
情况 我们之前已经完成了cas4.2.x登录使用mongodb验证方式并且自定义了加密. 单点登录(十五)-----实战-----cas4.2.x登录mongodb验证方式实现自定义加密 但是悲剧的是 ...
- 单点登录(十八)----cas4.2.x客户端增加权限控制shiro
我们在上面章节已经完成了cas4.2.x登录启用mongodb的验证方式. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程 也完成了获取管理员身份属性 ...
- OAuth2简易实战(四)-Github社交联合登录
1. OAuth2简易实战(四)-Github社交联合登录 1.1. 用到的第三方插件 https://github.com/spring-projects/spring-social-github ...
随机推荐
- python虚拟环境管理之virtualenv,virtualenvwrapper,pipenv,conda
虚拟环境的作用 使python环境拥有独立的包,避免污染原本的python环境.为不同的项目创建不同的环境可以避免安装的库过于庞大和相互干扰. 例如你想在同一台机器上开发用python2和python ...
- C++ 单例模式总结与剖析
目录 C++ 单例模式总结与剖析 一.什么是单例 二.C++单例的实现 2.1 基础要点 2.2 C++ 实现单例的几种方式 2.3 单例的模板 三.何时应该使用或者不使用单例 反对单例的理由 参考文 ...
- phpcms 容许英文目录有空格
在PHPCMS添加栏目里面,有个选项是 英文目录,这里目录可以用作伪静态功能.这么英文不能有空格等特殊字符.但是如果页面中需要引用包含空格的字符呢,例如,关于我们页面,我要显示英文about us.那 ...
- 如何使用g++编译调用dll的c++代码
本文将有以下4个部分来讲如何使用g++编译调用dll的c++代码. 1.如何调用dll 2.动态链接和静态链接的区别 3.g++的编译参数以及如何编译调用dll的c++代码 4.总结 1.如何调用dl ...
- cnblogs.com用户体验
一.是否提供了良好的体验给用户(同时提供价值)? 首先我觉得博客园给我们这些用户提供了良好的用户体验,博客园提供了一个纯净的技术交流空间,在这里我们可以找到几乎所有与IT技术有关的博文,而且可以在这里 ...
- 第二阶段Sprint冲刺会议3
进展:讨论视频录制的具体功能,查看有关资料,开始着手编写有关代码.
- spring冲刺第五天
昨天进行了地图的初步编写,上网查找了错误的原因,改进了源代码,使程序可以执行. 今天继续编写地图代码,完善地图界面,使其变得美观. 遇到的问题:地图的完善比较难.
- 【图论】POJ-3169 差分约束系统
一.题目 Description Like everyone else, cows like to stand close to their friends when queuing for feed ...
- DocX插件
DocX是一个用C#编写的.NET库,它允许开发人员以简单直观的方式操作Word文件.
- 如何在服务器(centOS系统)上运行JavaWeb项目
在上次的结对作业中(如果您对这几句话一头雾水的话就请忽视掉吧),因为我们小组制作的是Web版本的项目,需要布置在服务器上才算完成.故申请了一个腾讯云服务器用于运行项目,在这个过程中了解了如何在服务器上 ...