简体   繁体   中英

Spring LDAP authentication bad credentials password in Salted SHA type

I have a project for LDAP authentication with REST service. My LDAP configuration have Salted SHA (SSHA) password hash method. In Spring's LDAP authentication best practice guide supporting SHA method when I used that I got bad credentials while crendentials are ok.

My configuration class reference:

@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");
    }
}

My ldif configuration;

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==

My original password is Test1234 . My pom.xml file ;

   <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>

How can I authenticate with my username/password to ldap server with SSHA password encryption?

Try this approach

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

And create a custom password encoder

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);
        }
    };
}

I had same problem and it worked for me.

Hope it helps you!

I encountered a similar issue using bcrypt backed by apache directory. I resolved it thanks to the solution suggested by @jrdalpra. Here is my solution for anyone in the same situation:

    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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM