设为首页 收藏本站
查看: 1801|回复: 0

[经验分享] 单点登录(十四)

[复制链接]

尚未签到

发表于 2017-12-16 18:37:27 | 显示全部楼层 |阅读模式
  我们在上一篇文章中已经讲解了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
DSC0000.jpg

  这四种方式其实脱胎于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。
DSC0001.jpg

  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中查找。
  如图:
DSC0002.jpg

DSC0003.jpg

DSC0004.jpg

  所以我们在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>  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
DSC0005.jpg

使用NONE模式
  NONE是对密码不加密,跟不配置是一样的效果,验证时明文对比。
  cas.properties
  中新增cas.authn.mongo.passwordEncoder.type=NONE
  如图:
DSC0006.jpg

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
DSC0007.jpg

{  "_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
DSC0008.jpg

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
DSC0009.jpg

{  "_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这两种方式。
DSC00010.jpg

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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-424782-1-1.html 上篇帖子: Windows 下安装和配置 MongoDB(二) 下篇帖子: Java MongoDB下根据数组大小进行查询的方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表