簡體   English   中英

Salted SHA類型中的Spring LDAP身份驗證錯誤憑據密碼

[英]Spring LDAP authentication bad credentials password in Salted SHA type

我有一個使用REST服務進行LDAP身份驗證的項目。 我的LDAP配置具有Salted SHA(SSHA)密碼哈希方法。 在Spring的LDAP身份驗證最佳實踐指南中支持SHA方法,當我使用它時我得到了錯誤的憑據,而crendentials是可以的。

我的配置類參考:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
                .userSearchFilter("uid={0}")
                .contextSource(contextSource())
                .passwordCompare()
                    .passwordEncoder(new LdapShaPasswordEncoder())
                    .passwordAttribute("userPassword");
    }

    @Bean
    public DefaultSpringSecurityContextSource contextSource() {
        return new DefaultSpringSecurityContextSource(Arrays.asList("ldap://localhost:8389/"), "dc=springframework,dc=org");
    }
}

我的ldif配置;

dn: uid=ben,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Ben Alex
sn: Alex
uid: ben
userPassword: {SSHA}pcFdFhO/NS98EhTRup60PMkHMWFRDkJ3jUu1Zg==

我的原始密碼是Test1234 我的pom.xml文件;

   <dependency>
       <groupId>org.springframework.ldap</groupId>
       <artifactId>spring-ldap-core</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.security</groupId>
       <artifactId>spring-security-ldap</artifactId>
   </dependency>
   <dependency>
       <groupId>com.unboundid</groupId>
       <artifactId>unboundid-ldapsdk</artifactId>
   </dependency>

如何使用我的用戶名/密碼對使用SSHA密碼加密的ldap服務器進行身份驗證?

試試這種方法

auth
        .ldapAuthentication()
            .userSearchFilter("uid={0}")
            .contextSource(contextSource())
            .passwordCompare().passwordAttribute("userPassword")
            .and()
            .passwordEncoder(passwordEncoder());

並創建一個自定義密碼編碼器

private PasswordEncoder passwordEncoder() {
    final LdapShaPasswordEncoder sha = new LdapShaPasswordEncoder();
    return new PasswordEncoder() {
        @Override
        public String encode(CharSequence rawPassword) {
            return sha.encodePassword(rawPassword.toString(), null);
        }
        @Override
        public boolean matches(CharSequence rawPassword, String encodedPassword) {
            return sha.isPasswordValid(encodedPassword, rawPassword.toString(), null);
        }
    };
}

我有同樣的問題,它對我有用。

希望它能幫到你!

我使用apache目錄支持的bcrypt遇到了類似的問題。 由於@jrdalpra提出的解決方案,我解決了這個問題。 對於處於相同情況的任何人,這是我的解決方案:

    private PasswordEncoder newPasswordEncoder() {
      final BCryptPasswordEncoder crypt = new BCryptPasswordEncoder();
      return new PasswordEncoder() {
        @Override
        public String encode(CharSequence rawPassword) {
          // Prefix so that apache directory understands that bcrypt has been used.
          // Without this, it assumes SSHA and fails during authentication.
          return "{CRYPT}" + crypt.encode(rawPassword);
        }
        @Override
        public boolean matches(CharSequence rawPassword, String encodedPassword) {
          // remove {CRYPT} prefix
          return crypt.matches(rawPassword, encodedPassword.substring(7));
        }
    };
  }

堅持使用您的初始代碼,但這次嘗試將.userSearchFilter("uid={0}")轉換為.userSearchFilter("uid={0},ou=people").userDnPatterns("uid={0},ou=people")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM