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
- 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()Mathieu Thauvoye– Mathieu Thauvoye2023-10-03 16:01:26 +00:00CommentedOct 3, 2023 at 16:01
- It isnot because of different modules. It is because
LdapNamecannot be cast toString. This is an error in your code.user207421– user2074212023-10-04 09:05:30 +00:00CommentedOct 4, 2023 at 9:05
1 Answer1
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()));}1 Comment
Explore related questions
See similar questions with these tags.