There is no PasswordEncoder mapped for the id “null”

1、问题描述:在写基于Spring cloud微服务的OAuth2认证服务时,因为Spring-Security从4+升级到5+,导致There is no PasswordEncoder mapped for the id “null”错误。

2、解决方案:

可在密码验证类中添加

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

或者在xml配置文件中添加

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

Spring官方文

There is no PasswordEncoder mapped for the id "null"

3、拓展:源码解读:Class PasswordEncoderFactories,Spring Security 5 新增加了多种密码加密方式。

当前版本5新增支持加密方式:

bcrypt - BCryptPasswordEncoder (Also used for encoding)
ldap - LdapShaPasswordEncoder
MD4 - Md4PasswordEncoder
MD5 - new MessageDigestPasswordEncoder("MD5")
noop - NoOpPasswordEncoder
pbkdf2 - Pbkdf2PasswordEncoder
scrypt - SCryptPasswordEncoder
SHA-1 - new MessageDigestPasswordEncoder("SHA-1")
SHA-256 - new MessageDigestPasswordEncoder("SHA-256")
sha256 - StandardPasswordEncoder
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐