spring security3.x学习(18)_salt以及Bcrypt加密
来源:互联网 发布:淘宝女装模特拍摄教程 编辑:程序博客网 时间:2024/05/21 10:33
其实,对于基本的权限基础操作我们已经学的差不多了,接下来应该都是一堆复杂或深入的知识了,。呵呵。不过越学越有点意思了。这3天假期也没有浪费啊。
这次看一下登录的加密:
- <authentication-manager alias="authenticationManager">
- <authentication-provider user-service-ref="jdbcUserService">
- <password-encoder hash="sha"/>
- </authentication-provider>
- </authentication-manager>
这里在authentication-provider中配置了一个password-encoder标签,其中有个属性叫做hash,就是要加密密码所用的加密方法。看看都可以选用哪些系统默认提供的呢?:
看一看书中的介绍:
看到这里,我又去查了一下最新的官方文档,文档中这样写:
他建议我们使用BCryptPasswordEncoder类。上面我们提供了一种方式是使用hash的方式进行配置,其实还有一种方式,通过ref进行配置:
< bean class = "org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" id = "passwordEncoder"/>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="jdbcUserService">
<password-encoder ref="passwordEncoder"/>
</authentication-provider>
</authentication-manager>
<authentication-provider user-service-ref="jdbcUserService">
<password-encoder ref="passwordEncoder"/>
</authentication-provider>
</authentication-manager>
虽然这样配置spring security文档中也说可以了,但是它还是建议我们加一些"盐值"(salt),加上这个以后就更难破解了。
- <authentication-manager alias="authenticationManager" >
- <authentication-provider user-service-ref="jdbcUserService" >
- <password-encoder ref="passwordEncoder" >
- <salt-source ref="saltSource" />
- </password-encoder>
- </authentication-provider>
- </authentication-manager >
。那么这个salt有什么用呢。看如下这个图:
好的,那流程我们就应该知道了,用户通过输入密码然后通过和salt值连接进行计算加密保存到用户数据库中,如果salt越过于随机就越不好破解。
注意:“需要记住的是salt被添加到明文的密码上,所以salt不能进行单向的加密,因为应用要查找用户对应的salt值以完成对用户的认证。”
Spring Security为我们提供了一个接口o.s.s.authentication.dao.SaltSource,它定义了一个方法根据UserDetails来返回salt值,并提供了两个内置的实现:
1.SystemWideSaltSource为所有的密码定义了一个静态的salt值。这与不使用salt的密码相比并没有提高多少安全性; (那这个就应该是不推荐使用呗!)
2. ReflectionSaltSource使用UserDetails对象的一个bean属性得到用户密码的salt值。 鉴于salt 值应该能够根据用户数据得到或者与用户数据一起存储,ReflectionSaltSource作为内置的实现被广泛使用。
2. ReflectionSaltSource使用UserDetails对象的一个bean属性得到用户密码的salt值。 鉴于salt 值应该能够根据用户数据得到或者与用户数据一起存储,ReflectionSaltSource作为内置的实现被广泛使用。
看一下配置方法:
- <bean class="org.springframework.security.authentication.dao.ReflectionSaltSource" id="saltSource">
- <property name="userPropertyToUse" value="username"/>
- </bean>
userPropertyToUser是UserDetails对象的一个属性值(ReflectionSaltSource类是通过反射获取属性值的)
- <authentication-manager alias="authenticationManager">
- <authentication-provider user-service-ref="jdbcUserService">
- <password-encoder ref="passwordEncoder">
- <salt-source ref="saltSource"/>
- </password-encoder>
- </authentication-provider>
- </authentication-manager>
那么现在有一个问题,就是salt值是通过什么来获取的呢。
应该是依赖UserDetails来获取的、
突然想到了spring security文档中给我们的建议。他不是希望我们能使用Bcrpt加密方式吗。然后我就去查了文档中,关于Bcrpt的介绍,发现,我不用像书中写的那样复杂,要完成很多的salt过程,这些都不用,我们看一下文档:
多有意思哈。 看看。多查文档好啊。。那么我们看到了它的便利,我们怎么使用它呢。 我写了一段代码:
- BCryptPasswordEncoder util = new BCryptPasswordEncoder();
- String password = util.encode("admin" );
- System. out.println(util.matches("admin" , password));
这样以后,输出的是true.这个Bcrypt很有意思,他每次加密的值是不一样的。可能是因为他内部有随机salt的语言吧。 给大家看一下我的测试代码:
- BCryptPasswordEncoder util = new BCryptPasswordEncoder();
- for(int i = 0 ; i < 10; i ++){
- System. out.println(util.encode("admin" ));
- }
输出结果是:
- $2a$10$zYH/GUMQ1reN.OfyMg2Rh.TYdKwerqF0iSzEDtUcu1eZl6uP5wtuC
- $2a$10$z7ksuaRgAgSnbgftvI9lGevhxX0qliy90m5f0e6jUs8mYGL1b6MYO
- $2a$10$pjA1zF0T5uaiwnNPVOIpjuHYpdRdWnCJnVBOi2IrFzUv8S9cmh4qC
- $2a$10$5sT9FVFiWDPUbdIy/hKzVO7fHWbw.C30PPm5BaZ2Aj5PZTzJXUHK2
- $2a$10$xjAGP2GdHCtCs8MZp/YqcOsPohLP1vK46A2kDak0rAkvUqF7HQTN2
- $2a$10$gzpMfQdGVmrDaYbPUOSwee8JSON7DyC5Ix8roR/YFNrnHEOT9E.V.
- $2a$10$27FasVFxg2u84oX0XATmo.JuwdBQaW6RYUYqD6fLa5zvE0rVOCL3m
- $2a$10$2J4McbDdQXi6OhtCFBaF0ezISxII33ECVDWhl/q6p1EznGmsUSIeK
- $2a$10$QZNF4fH7qh5c684aiGuc3.6vsLa7VYMlWd1PYNxli4zabVxrF8JxS
- $2a$10$2P78iMXhmFfNlUNKhn/ZPeJfhGzgIEBbSWpYPhkCQ8qAr60Z6lmS.
看到了没?、 都是不一样的。 呵呵。 我们看一下他的源码
- /* */ package org.springframework.security.crypto.bcrypt;
- /* */
- /* */ import java.security.SecureRandom;
- /* */ import java.util.regex.Matcher;
- /* */ import java.util.regex.Pattern;
- /* */ import org.apache.commons.logging.Log;
- /* */ import org.apache.commons.logging.LogFactory;
- /* */ import org.springframework.security.crypto.password.PasswordEncoder;
- /* */
- /* */ public class BCryptPasswordEncoder
- /* */ implements PasswordEncoder
- /* */ {
- /* */ private Pattern BCRYPT_PATTERN;
- /* */ private final Log logger ;
- /* */ private final int strength ;
- /* */ private final SecureRandom random;
- /* */
- /* */ public BCryptPasswordEncoder ()
- /* */ {
- /* 42 */ this(-1);
- /* */ }
- /* */
- /* */ public BCryptPasswordEncoder( int strength)
- /* */ {
- /* 49 */ this(strength, null);
- /* */ }
- /* */
- /* */ public BCryptPasswordEncoder( int strength, SecureRandom random)
- /* */ {
- /* 34 */ this.BCRYPT_PATTERN = Pattern.compile("\\A\\$2a?\\$\\d\\d\\$[./0-9A-Za-z]{53}");
- /* 35 */ this.logger = LogFactory.getLog(super.getClass());
- /* */
- /* 58 */ this.strength = strength;
- /* 59 */ this.random = random;
- /* */ }
- /* */
- /* */ public String encode(CharSequence rawPassword)
- /* */ {
- /* */ String salt;
- /* */ String salt;
- /* 64 */ if (this. strength > 0)
- /* */ {
- /* */ String salt;
- /* 65 */ if (this. random != null) {
- /* 66 */ salt = BCrypt.gensalt(this. strength, this.random );
- /* */ }
- /* */ else
- /* 69 */ salt = BCrypt.gensalt(this. strength);
- /* */ }
- /* */ else
- /* */ {
- /* 73 */ salt = BCrypt.gensalt ();
- /* */ }
- /* 75 */ return BCrypt.hashpw(rawPassword.toString(), salt);
- /* */ }
- /* */
- /* */ public boolean matches(CharSequence rawPassword, String encodedPassword) {
- /* 79 */ if ((encodedPassword == null) || (encodedPassword.length() == 0)) {
- /* 80 */ this.logger.warn( "Empty encoded password");
- /* 81 */ return false;
- /* */ }
- /* */
- /* 84 */ if (!(this.BCRYPT_PATTERN .matcher(encodedPassword).matches())) {
- /* 85 */ this.logger.warn( "Encoded password does not look like BCrypt");
- /* 86 */ return false;
- /* */ }
- /* */
- /* 89 */ return BCrypt.checkpw(rawPassword.toString(), encodedPassword);
- /* */ }
- /* */ }
看一下,他内部真的使用了SecureRandom 这个随机类。然后加密的时候也使用了自己计算出来的一个salt然后进行加密的,这个要比我们自己配置salt要好的多。所以建议我们以后都使用Bcrypt加密方式了。
0 0
- spring security3.x学习(18)_salt以及Bcrypt加密
- spring security3.x学习(18)_salt以及Bcrypt加密
- Spring Security 4.x 启用BCrypt加密
- spring security3.x学习笔记
- spring security3.x学习(1)
- spring security3.x学习(11)_退出
- spring security3.x学习(12)_remember me
- spring security3.x学习(17)_GBAC
- spring security3.x学习(11)_退出
- spring security3.x学习(12)_remember me
- spring security3.x学习(17)_GBAC
- Spring Security3.x 入门教程
- spring security3.x学习(1)_必备资料&开始学习
- spring security3.x学习(4)_认证管理的过程
- spring security3.x学习(5)_如何拦截用户请求
- spring security3.x学习(7)_访问决策管理
- spring security3.x学习(10)_自定义的登录页面
- spring security3.x学习(15)_扩展JdbcDaoImpl。
- 获取ip地址及网卡信息
- spring security3.x学习(17)_GBAC
- Linux内核可加载模块基础(1)
- 如何彻底卸载百度卫士?
- iOS 汉字转拼音
- spring security3.x学习(18)_salt以及Bcrypt加密
- Linux时钟
- 在eclipse里面新建service项目
- 对于这位老人他也是有着不小的好感
- appium for windows 环境搭建
- 当LogCat上面的日志不能输出怎么办?
- linux如何复制文件夹和移动文件夹
- (1) git/github原理与基本操作ABC中的ABC
- Kafka分布式消息系统介绍