QUESTION:Spring Security使用出现 Encoded password does not look like BCrypt异常的解决以及Spring Securit加密方式的学习?

目录

QUESTION:Spring Security使用出现 Encoded password does not look like BCrypt异常的解决以及Spring Securit加密方式的学习?

ANWSER:

一:问题提出

二:解决方法:

三:Spring Securit加密方式


ANWSER:

一:问题提出

 

博主在做一个SSM整合企业权限管理系统case时,第一次使用spring security框架进行权限验证,在经过一系列bug的修改,最后逻辑通了,却出现 Encoded password does not look like BCrypt。查询半天无果,特此写下关于spring security框架的学习心得。

之所以出现以上异常,是因为版本升级5.x后,密码格式不匹配产生。

我们可以看下抛出异常的源代码:

package org.springframework.security.crypto.bcrypt;

import java.security.SecureRandom;
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() {
        this(-1);
    }

    public BCryptPasswordEncoder(int strength) {
        this(strength, (SecureRandom)null);
    }

    public BCryptPasswordEncoder(int strength, SecureRandom random) {
        this.BCRYPT_PATTERN = Pattern.compile("\\A\\$2a?\\$\\d\\d\\$[./0-9A-Za-z]{53}");
        this.logger = LogFactory.getLog(this.getClass());
        if (strength == -1 || strength >= 4 && strength <= 31) {
            this.strength = strength;
            this.random = random;
        } else {
            throw new IllegalArgumentException("Bad strength");
        }
    }

    public String encode(CharSequence rawPassword) {
        String salt;
        if (this.strength > 0) {
            if (this.random != null) {
                salt = BCrypt.gensalt(this.strength, this.random);
            } else {
                salt = BCrypt.gensalt(this.strength);
            }
        } else {
            salt = BCrypt.gensalt();
        }

        return BCrypt.hashpw(rawPassword.toString(), salt);
    }

    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        if (encodedPassword != null && encodedPassword.length() != 0) {
            if (!this.BCRYPT_PATTERN.matcher(encodedPassword).matches()) {
                this.logger.warn("Encoded password does not look like BCrypt");
                return false;
            } else {
                return BCrypt.checkpw(rawPassword.toString(), encodedPassword);
            }
        } else {
            this.logger.warn("Empty encoded password");
            return false;
        }
    }
}
public boolean matches()方法中进行判定密码是否匹配。

二:解决方法:

修改密码加密的方式:

一开始我的代码:

 User user=new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(),userInfo.getStatus()==0?false:true,true,true,true,getAuthority(userInfo.getRoles()));

改变成:

 BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
//        User user=new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(), getAuthority(userInfo.getRoles()));
        User user=new User(userInfo.getUsername(),"{noop}"+bCryptPasswordEncoder.encode(userInfo.getPassword()),userInfo.getStatus()==0?false:true,true,true,true,getAuthority(userInfo.getRoles()));

三:Spring Securit加密方式

BCrypt 算法与 MD5/SHA 算法有一个很大的区别,每次生成的 hash 值都是不同的,就可以免除存储 salt,暴力破解起来也更困难。BCrypt 加密后的字符长度比较长,有60位,所以用户表中密码字段的长度,如果打算采用 BCrypt 加密存储,字段长度不得低于 68(需要前缀 {bcrypt})。

对于Spring Security的学习,本博主后续会写。

专栏:Spring Security

更多推荐