Spring安全性:将4.0迁移到5.0 - 错误 - 没有为id“null”映射PasswordEncoder

时间:2018-04-02 10:43:49

标签: spring spring-security

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
    org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:236)
    org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:196)
    org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$LazyPasswordEncoder.matches(WebSecurityConfigurerAdapter.java:593)

我的代码工作正常,现在我已将spring安全版本从4.0更改为5.0,它无法正常工作

2 个答案:

答案 0 :(得分:8)

对于基于Java的,在代码中进行这些更改并将其更新为

@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
    BCryptPasswordEncoder encoder = passwordEncoder();
    auth.inMemoryAuthentication().withUser("admin").password(encoder.encode("admin")).roles("ADMIN");
}

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

,或者

 @Bean
public static NoOpPasswordEncoder passwordEncoder() {
 return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}

用于XML配置,

<bean id ="passwordEncoder" class = "org.springframework.security.crypto.NoOpPasswordEncoder" factory-method = "getInstance" />

答案 1 :(得分:0)

请仔细阅读她的链接,了解您需要为此做的更新: -

https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-format

您可能需要在班级中为密码编码器添加bean,这将再次取决于您的要求

例如,如果可以在配置类中使用NoOpPasswordEncoder,如下所示: -

@Bean
public NoOpPasswordEncoder noopPasswordEncoder(){
    return new NoOpPasswordEncoder();
}

对于基于XML的配置,如下所示: -

<bean id ="passwordEncoder" class = "org.springframework.security.crypto.NoOpPasswordEncoder" factory-method = "getInstance" />