为什么我的Spring Security哈希密码不能进行身份验证?

时间:2016-01-20 22:54:44

标签: java spring hash

如果我删除散列密码,它会像魅力一样进行身份验证,但是当我使用散列保存密码然后将其设置为Spring进行比较时,它只是不起作用,它不会进行身份验证。

这是我的Hashpassword代码:

@Autowired
protected void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(springUserService);
    auth.authenticationProvider(authenticationProvider());
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

@Bean
public DaoAuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
    authenticationProvider.setUserDetailsService(springUserService);
    authenticationProvider.setPasswordEncoder(passwordEncoder());
    return authenticationProvider;
}

正如预期的那样,Spring Encrypter工作正常,所以我想这只是我配置的问题。这是测试:

public class TestBCrypt {
public static void main(String[] args) {
    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    //String encodedPassword = passwordEncoder.encode("123");
    //System.out.println(encodedPassword);
    System.out.println(passwordEncoder.matches("123","$2a$10$KZkrMlK2dnyKfKa3xvRSMukQzmul.DUgOvdv7GFezqGFmoBeFDkt.")); // prints true
}

}

0 个答案:

没有答案