【问题标题】:There is no PasswordEncoder mapped for the id "null" in spring 5 after using - PasswordEncoderFactories.createDelegatingPasswordEncoder() [duplicate]使用后,在 Spring 5 中没有为 ID“null”映射 PasswordEncoder - PasswordEncoderFactories.createDelegatingPasswordEncoder() [重复]
【发布时间】:2019-08-28 09:36:28
【问题描述】:

申请后我在 Spring 安全中收到以下错误:

PasswordEncoderFactories.createDelegatingPasswordEncoder()

项目依赖:

  • Spring Boot2.1.3
  • Spring-cloud-starter-oauth2

我尝试使用以下两种方法:

第一种方法:

@Override
 protected void configure(AuthenticationManagerBuilder auth) throws Exception {

final PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

        auth.inMemoryAuthentication()
            .withUser("user")
            .password(encoder.encode("password"))
            .roles("USER");

    }

错误:

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches

第二:接近-

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {

final BCryptPasswordEncoder bcrypt = new BCryptPasswordEncoder();
        auth.inMemoryAuthentication()
            .withUser("user")
            .password(encoder.encode("password"))
            .roles("USER");

    }

错误:

java.lang.IllegalArgumentException: password is not bcrypt type

我没有尝试使用 {noop} 密码,因为它已经从 Spring5 中弃用了。 在网站上看到了很多解决方案,但无法解决我的问题。

【问题讨论】:

  • 对于第一种方法,您是否费心阅读文档,即DelegatingPasswordEncoder 的javadoc,以了解它是如何工作的? --- 你知道“委托”是什么意思吗?这意味着“移交”给某人/某事。由于您没有指定其他任何内容,您认为它将委派给什么?
  • 是的,我阅读了委派密码编码器的文档。在许多开发人员建议堆栈溢出后,我尝试使用相同的方法。但是没有一个解决方案对我有用。你有什么解决方案可以解决这个问题而不是让我知道..?
  • Andreas - 这个问题是针对 Spring 5 安全性提出的,之前的所有问题都已过时,并且没有一个解决方案有效。在指定 Spring security5 时,我已经提出了不同的问题。这是我们在 Spring 5 中面临的问题,而不是在以前的版本中。所以我请求你打开这个问题,让其他开发者做出见解。
  • 您需要显示这两种情况的堆栈跟踪,以便我们可以看到错误实际发生的位置。 ---此外,您的代码没有按照各种示例显示的方式完成,例如您不是在 @Bean 方法中创建编码器。

标签: java spring-security-oauth2


【解决方案1】:

我有同样的错误信息,刚刚解决了。

你的第一种方法

   PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

    auth.inMemoryAuthentication()
        .withUser("user")
        .password(encoder.encode("password"))
        .roles("USER");

只是解决方案的第一步

我认为您还实现了 AuthorizationServerConfigurerAdapter。在此适配器实现中,您指定了导致错误的 ClientSecret

clients.inMemory()
        .withClient("ClientIdValue")
        .secret("ClientSecretValue")
        .authorizedGrantTypes("password", "refresh_token")

您应该通过添加 {noop}PasswordEncoder 来解决问题,就像您在方法中所做的那样。

clients.inMemory()
        .withClient("ClientIdValue")
        .secret("{noop}ClientSecretValue")
        .authorizedGrantTypes("password", "refresh_token")

【讨论】:

    猜你喜欢
    • 2018-09-14
    • 2019-06-02
    • 2021-06-02
    • 2020-10-27
    • 2018-10-30
    • 2018-11-14
    • 2020-06-24
    • 2020-08-11
    相关资源
    最近更新 更多