Spring Security 5:没有为 id“null”映射 PasswordEncoder

新手上路,请多包涵

我正在从 Spring Boot 1.4.9 迁移到 Spring Boot 2.0 以及 Spring Security 5,并且我正在尝试通过 OAuth 2 进行身份验证。但我收到此错误:

java.lang.IllegalArgumentException:没有为 id “null”映射 PasswordEncoder

Spring Security 5 的文档中,我了解到密码的存储格式已更改。

在我当前的代码中,我将密码编码器 bean 创建为:

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

但是它给了我以下错误:

编码密码看起来不像 BCrypt

因此,我根据 Spring Security 5 文档将编码器更新为:

 @Bean
public PasswordEncoder passwordEncoder() {
    return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

现在,如果我可以在数据库中看到密码,它将存储为

{bcrypt}$2a$10$LoV/3z36G86x6Gn101aekuz3q9d7yfBp3jFn7dzNN/AL5630FyUQ

随着第一个错误消失,现在当我尝试进行身份验证时,我遇到了以下错误:

java.lang.IllegalArgumentException:没有为 id “null”映射 PasswordEncoder

为了解决这个问题,我尝试了 Stackoverflow 中的以下所有问题:

这是一个与我类似但未回答的问题:

注意:我已经将加密密码存储在数据库中,因此无需在 UserDetailsService 中再次编码。

Spring security 5 文档中,他们建议您可以使用以下方法处理此异常:

DelegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(PasswordEncoder)

如果这是修复,那么我应该把它放在哪里?我试图把它放在 PasswordEncoder bean 中,如下所示,但它不起作用:

 DelegatingPasswordEncoder def = new DelegatingPasswordEncoder(idForEncode, encoders);
def.setDefaultPasswordEncoderForMatches(passwordEncoder);

MyWebSecurity 类

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {

        web
                .ignoring()
                .antMatchers(HttpMethod.OPTIONS)
                .antMatchers("/api/user/add");
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

MyOauth2 配置

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore();
    }

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Bean
    public TokenEnhancer tokenEnhancer() {
        return new CustomTokenEnhancer();
    }

    @Bean
    public DefaultAccessTokenConverter accessTokenConverter() {
        return new DefaultAccessTokenConverter();
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
            throws Exception {
        endpoints
                .tokenStore(tokenStore())
                .tokenEnhancer(tokenEnhancer())
                .accessTokenConverter(accessTokenConverter())
                .authenticationManager(authenticationManager);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("test")
                .scopes("read", "write")
                .authorities(Roles.ADMIN.name(), Roles.USER.name())
                .authorizedGrantTypes("password", "refresh_token")
                .secret("secret")
                .accessTokenValiditySeconds(1800);
    }
}

请指导我解决这个问题。我花了几个小时来解决这个问题,但无法解决。

原文由 Jimmy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答

当您配置 ClientDetailsServiceConfigurer 时,您还必须将新 密码存储格式 应用于客户端密码。

 .secret("{noop}secret")

原文由 Edwin Diaz 发布,翻译遵循 CC BY-SA 3.0 许可协议

添加 .password("{noop}password") 到安全配置文件。

例如 :

 auth.inMemoryAuthentication()
        .withUser("admin").roles("ADMIN").password("{noop}password");

原文由 Sailokesh Aithagoni 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题