ClassCastException because in diffrent modules

41 views Asked by At

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

1

There are 1 answers

1
bilgin On

did you try ldapAttributes.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;


@Test
public 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()));
}