Reputation: 51
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?
Upvotes: 5
Views: 7776
Reputation: 41
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));
}
};
}
Upvotes: 2
Reputation: 1
Stick with your initial code, but this time try to make .userSearchFilter("uid={0}")
into .userSearchFilter("uid={0},ou=people")
or .userDnPatterns("uid={0},ou=people")
.
Upvotes: 0
Reputation: 41
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!
Upvotes: 4