2017-04-06 58 views
0

为什么BCryptPasswordEncoder的强度会影响服务器的启动时间?在启动时没有生成哈希,所以我想知道为什么这对启动有任何影响。BCryptPasswordEncoder影响服务器的启动时间

当然,我知道检查密码是否匹配需要时间,但在启动时很奇怪。

代码如下所示:

@Bean 
public BCryptPasswordEncoder passwordEncoder() { 
    return new BCryptPasswordEncoder(17); // Affects startup time tremendously 
} 

@Autowired 
BCryptPasswordEncoder bcryptEncoder; 

@Autowired 
CustomUserDetailsService userDetailsService; 

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(userDetailsService) 
     .passwordEncoder(bcryptEncoder); 
} 
+0

取决于我定义的力度。例如17,启动服务器需要花费+ -5分钟。 – Kramer

回答

-1

BCryptPasswordEncoder的构造是不是做取决于密码强度上启动任何东西:

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

public BCryptPasswordEncoder(int strength, SecureRandom random) { 
    if (strength != -1 && (strength < BCrypt.MIN_LOG_ROUNDS || strength > BCrypt.MAX_LOG_ROUNDS)) { 
     throw new IllegalArgumentException("Bad strength"); 
    } 
    this.strength = strength; 
    this.random = random; 
} 

已经看到了这一点,我不认为只有改变如上所述,强度参数可以增加启动时间。

但是当你实际使用加密器时,'强度'肯定会影响性能。那么你可能在启动时在某处加密了很多密码?

1

(根据您的配置)
看一看春DaoAuthenticationProvider 下面的方法被调用在启动时:

private void setPasswordEncoder(PasswordEncoder passwordEncoder) { 
    Assert.notNull(passwordEncoder, "passwordEncoder cannot be null"); 
    this.userNotFoundEncodedPassword = passwordEncoder.encodePassword(
     USER_NOT_FOUND_PASSWORD, null); 
    this.passwordEncoder = passwordEncoder; 
} 

这被引入,服务器具有编码的密码,以验证对密码从试图进行身份验证的用户名不存在。

/** 
* The plaintext password used to perform 
* {@link PasswordEncoder#isPasswordValid(String, String, Object)} on when the user is 
* not found to avoid SEC-2056. 
*/ 
private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword"; 

参考:
https://github.com/spring-projects/spring-security/issues/2280
https://jira.spring.io/browse/SEC-2056

注意:如果你设置的力量17,你的服务器占用额外的5分钟开始,它将把你的服务器大约5分钟验证每个用户密码进行身份验证时。