-1

I encounter this issue with spring LDAP:

class javax.naming.ldap.LdapName cannot be cast to class java.lang.String (javax.naming.ldap.LdapName is in module java.naming of loader 'bootstrap'; java.lang.String is in module java.base of loader 'bootstrap')

Here is the piece of codes where the issue occurs

private void createUser(LdapAttributes ldapAttributes) {        LdapName dn = LdapNameBuilder                .newInstance()                .add("ou", "users")                .add("cn", ldapAttributes.getName())                .build();        DirContextAdapter context = new DirContextAdapter(dn);        context.setAttributeValues(                "objectclass",                new String[]                        { "top",                          "person",                          "organizationalPerson",                          "inetOrgPerson" });        context.setAttributeValue("cn", ldapAttributes.getName());        context.setAttributeValue("sn", ldapAttributes.getName());        context.setAttributeValue("userPassword", ldapAttributes.get("userPassword"));        ldapTemplate.bind(context);    }

I am using spring org.springframework.ldap:spring-ldap-core and org.springframework.ldap:spring-ldap-ldif-core both in version 2.4.1

Springboot is in version 2.7.12

N.F.'s user avatar
N.F.
4,2025 gold badges29 silver badges63 bronze badges
askedOct 3, 2023 at 14:57
Mathieu Thauvoye's user avatar
2
  • the exception occurs in LdapName dn = LdapNameBuilder .newInstance() .add("ou", "users") .add("cn", ldapAttributes.getName()) .build(); with message Method threw 'java.lang.ClassCastException' exception. Cannot evaluate javax.naming.ldap.LdapName.toString()CommentedOct 3, 2023 at 16:01
  • It isnot because of different modules. It is becauseLdapName cannot be cast toString. This is an error in your code.CommentedOct 4, 2023 at 9:05

1 Answer1

0

did you tryldapAttributes.getName().toString() ?

Here is the code that i test with Spring

import javax.naming.ldap.LdapName;import org.springframework.ldap.core.LdapAttributes;import org.springframework.ldap.support.LdapNameBuilder;import org.springframework.ldap.support.LdapUtils;@Testpublic void createUser() {    LdapAttributes ldapAttributes = new LdapAttributes();    ldapAttributes.setName(LdapUtils.newLdapName("cn=Mango,ou=Fruits,o=Food"));    String name = ldapAttributes.getName().toString();    LdapName dn = LdapNameBuilder            .newInstance()            .add("ou", "users")            .add("cn", ldapAttributes.getName())            .build();    assertEquals(name, ldapAttributes.getName().toString());    assertEquals(name, String.valueOf(ldapAttributes.getName()));}
answeredOct 3, 2023 at 15:18
bilgin's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

yes i did, unfortunately same issue

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.