【问题标题】:Java Spring Security PasswordEncoderJava Spring 安全密码编码器
【发布时间】:2012-11-29 01:59:29
【问题描述】:

我注意到 Spring Security 的 Md4PasswordEncoder 中有以下代码:

/**
 * Takes a previously encoded password and compares it with a raw password after mixing in the salt and
 * encoding that value.
 *
 * @param encPass previously encoded password
 * @param rawPass plain text password
 * @param salt salt to mix into password
 * @return true or false
 */
public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
    String pass1 = "" + encPass;
    String pass2 = encodePassword(rawPass, salt);
    return PasswordEncoderUtils.equals(pass1,pass2);
}

我目前正在开发自定义 PasswordEncoder。谁能解释一下为什么spring开发人员通过向传入的对象添加一个空字符串来处理null?

提前致谢

【问题讨论】:

    标签: java spring-security


    【解决方案1】:

    我不认为这样做是出于特定原因。我认为这更多是因为开发人员不想在以后的版本中对其进行更改。

    在 3.0.3 版之前,代码是这样的 (Source):

    78    public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
    79        String pass1 = "" + encPass;
    80        String pass2 = encodePassword(rawPass, salt);
    81        return pass1.equals(pass2);
    82    }
    

    在这个版本中,如果 encPass 为空,并且如果第 79 行的语句应该是 String pass1 = encPass; 而不是原来的,第 81 行会抛出一个 NPE

    但是,在更高版本(您正在查看的版本)中,使用了来自 PasswordEncoderUtils 的等于,它已经处理了 encPass 可能为空的情况。

    因此,我认为"" + 在当前版本中是多余的,并且没有特殊原因留在那里。 (也许是因为它没有破坏任何东西,也不是造成重大性能损失的原因)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2019-12-16
      • 1970-01-01
      • 2017-05-17
      • 2011-08-27
      • 1970-01-01
      • 2020-05-19
      相关资源
      最近更新 更多