how to define DN for ldif file if DC is not provided while configuring with spring boot

Viewed 268

I am using embedded ldap server with spring boot for testing my ldif file and its credentials but as I found my ldif file format is bit different than normal ldif as dc is not privided, inside dn only o is given.

I have tried different configuration for the above said ldif format but it is still showing Bad Credentials screen but it is working fine with other ldif files.

ldif file

# id=00000001
dn: o=COMPANY
objectClass: organization
structuralObjectClass: organization
o: COMPANY
entryCSN: 20130409162114.626166Z#000000#000#000000
entryUUID: 3e7f8668-357d-1032-8a6b-c5bcf7f703f0
creatorsName: cn=Manager,o=COMPANY
createTimestamp: 20130409162114Z
modifiersName: cn=Manager,o=COMPANY
modifyTimestamp: 20130409162114Z
contextCSN: 20130702105648.506150Z#000000#000#000000
contextCSN: 20191018052018.692119Z#000000#001#000000
contextCSN: 20191018044350.858888Z#000000#002#000000
contextCSN: 20191018053729.621549Z#000000#003#000000

# id=00000002
dn: ou=department,o=COMPANY
objectClass: organizationalUnit
structuralObjectClass: organizationalUnit
ou: department
entryCSN: 20130409162455.623488Z#000000#000#000000
entryUUID: c2390a06-357d-1032-8a6c-c5bcf7f703f0
creatorsName: cn=Manager,o=COMPANY
createTimestamp: 20130409162455Z
modifiersName: cn=Manager,o=COMPANY
modifyTimestamp: 20130409162455Z

WebSecurityConfig

@Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
                .userDnPatterns("uid={0},ou=department")
                .contextSource()
                    .url("ldap://localhost:8389/o=COMPANY")
                    .and()
                .passwordCompare()
                    .passwordEncoder(new LdapShaPasswordEncoder())
                    .passwordAttribute("userPassword");
}

application.properties

spring.ldap.embedded.base-dn= o=COMPANY
spring.ldap.embedded.ldif=classpath:ldap-server-2.ldif
spring.ldap.embedded.port= 8389
spring.ldap.embedded.validation.enabled=false

After, configuring with all above details, application executes well and I get authentication screen but event after right credentials, I am getting Bad Credentials screen. I don't what's wrong, whether it is dn name for ldif or what?

Please provide your valuable suggestions. Thanks in advance!

1 Answers

It's not an issue per se to have o=COMPANY as the root entry instead of using a DC.

Your ldif seems correct, but what is shown here does not contain any user entry (just id=00000001 and id=00000002).

So, according to userDnPatterns("uid={0},ou=department"), user entries should be contained in ou=department and thus defined after in your ldif file, eg. :

# id=00000003
dn: uid=test,ou=department,o=COMPANY
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
objectClass: top
uid: test
mail: test@domain.com
cn: Firstname Lastname
givenName: Firstname
sn: Lastname

Also, most servers don't accept anonymous bindings and the authentication request itself may need to bind to the server in order to search for the given entry and test its credentials, just add managerDn() and managerPassword() in WebSecurityConfig's configure method :

.contextSource()
  .url('ldap://localhost:8389/o=COMPANY')
  .managerDn('admin')
  .managerPassword('password')
  .and()
...

You can also set these ContextSource params in application.properties :

spring.ldap.embedded.credential.username= uid=admin
spring.ldap.embedded.credential.password= password
Related