我们在上一篇文章中已经讲解了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. JS如何设置元素样式的方法示例

    <div id="box"></div> <script> var box = document.getElementById("bo ...

  2. Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...

  3. CHAPTER 19 Ordering the World 第19章 分类世界

    CHAPTER 19 Ordering the World 第19章 分类世界 Our planet is home to a bewildering variety of plants and an ...

  4. CUDA、CUDNN在Mac Book Pro上安装的问题

    由于原版MacOS自带Nvidia驱动版本过低,导致最新版本CUDA安装后无法运行.具体症状为:在编译时一切正常,在运行CUDA相关程序时报错: CUDA driver version is insu ...

  5. 阿里云解析记录应对家里动态IP

    <?php #需要配置的项 define('ACCESSKEYID',''); #阿里云用户密钥ID 获取方法 https://help.aliyun.com/knowledge_detail/ ...

  6. LINUX开发使用的3个远程工具

    1,SecureCRT 2,SSH Secure Shell Client 3,VNC Viewer 如果想VNC Server启动时加载vncserver服务 需要修改/etc/rc.d/rc.lo ...

  7. PyCharm配置SFTP远程调试Django应用

    http://www.ithao123.cn/content-41747.html http://www.th7.cn/system/lin/201703/205998.shtml

  8. C#获取周一、周日的日期 函数类

    #region 得到一周的周一和周日的日期        /// <summary>         /// 计算本周的周一日期         /// </summary> ...

  9. Java操作百度身份证API

    网址:http://apistore.baidu.com/ 点击功能进行复制代码,就拿百度的身份证API 举例子: http://apistore.baidu.com/apiworks/service ...

  10. 小学四则运算练习(JAVA编写)

    源码在Github的仓库主页链接地址:https://github.com/rucr9/rucr 看到这个题目,大概很多人会发出“切,这也太简单了吧!有必要小题大做?”的感叹!是的,仅仅作为一道数学运 ...