Spring Tests. I cannot use posixAccount objectClass in ldap as its use is disabled in the schema

huangapple go评论94阅读模式
英文:

Spring Tests. I cannot use posixAccount objectClass in ldap as its use is disabled in the schema

问题

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.TestContextSourceFactoryBean;
import org.springframework.context.annotation.TestConfiguration;
import org.springframework.context.annotation.TestPropertySource;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

@TestConfiguration
@TestPropertySource("classpath:application.yml")
@EnableAutoConfiguration(exclude = {
    ConsulAutoConfiguration.class,
    AutoServiceRegistrationAutoConfiguration.class,
    ConsulServiceRegistryAutoConfiguration.class,
    ConsulCatalogWatchAutoConfiguration.class,
    ConsulAutoServiceRegistrationAutoConfiguration.class
})
public class TestConfigurations {

    @Autowired
    private Environment env;
    @Autowired
    private ResourceLoader resourceLoader;

    @Primary
    @Bean
    public TestContextSourceFactoryBean testContextSource() {
        TestContextSourceFactoryBean contextSource = new TestContextSourceFactoryBean();
        contextSource.setDefaultPartitionName(env.getRequiredProperty("ldap.partition"));
        contextSource.setDefaultPartitionSuffix(env.getRequiredProperty("ldap.partitionSuffix"));
        contextSource.setPrincipal(env.getRequiredProperty("ldap.principal"));
        contextSource.setPassword(env.getRequiredProperty("ldap.password"));
        contextSource.setLdifFile(resourceLoader.getResource(env.getRequiredProperty("ldap.ldiffile")));
        contextSource.setPort(Integer.parseInt(env.getRequiredProperty("ldap.port")));
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate() throws Exception {
        return new LdapTemplate((ContextSource) testContextSource().getObject());
    }
}
ldap:
  partitionSuffix: dc=example,dc=com
  partition: example
  principal: uid=admin,ou=system
  password: secret
  ldiffile: classpath:/test.ldif
  port: 18888
  url: ldap://localhost:18888
@Entry(objectClasses = {"inetOrgPerson","top","posixAccount"})
public final class Person {
    // ... (other attributes and methods)
}

Error:

objectClass posixaccount w/ OID 1.3.6.1.1.1.2.0 not registered!
Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 54 - LOOP_DETECT
英文:

I use spring-ldap-test(2.3.3) in tests. Configurations:

@TestConfiguration
@TestPropertySource("classpath:application.yml")
@EnableAutoConfiguration(exclude = {ConsulAutoConfiguration.class,
AutoServiceRegistrationAutoConfiguration.class,
ConsulServiceRegistryAutoConfiguration.class,
ConsulCatalogWatchAutoConfiguration.class,
ConsulAutoServiceRegistrationAutoConfiguration.class,
})
public class TestConfigurations {
@Autowired
private Environment env;
@Autowired
private ResourceLoader resourceLoader;
@Primary
@Bean
public TestContextSourceFactoryBean testContextSource() {
TestContextSourceFactoryBean contextSource = new TestContextSourceFactoryBean();
contextSource.setDefaultPartitionName(env.getRequiredProperty("ldap.partition"));
contextSource.setDefaultPartitionSuffix(env.getRequiredProperty("ldap.partitionSuffix"));
contextSource.setPrincipal(env.getRequiredProperty("ldap.principal"));
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
contextSource.setLdifFile(resourceLoader.getResource(env.getRequiredProperty("ldap.ldiffile")));
contextSource.setPort(Integer.parseInt(env.getRequiredProperty("ldap.port")));
return contextSource;
}
@Bean
public LdapTemplate ldapTemplate() throws Exception {
return new LdapTemplate((ContextSource) testContextSource().getObject());
}
}

this is appication.yml file:

ldap:
partitionSuffix: dc=example,dc=com
partition: example
principal: uid=admin,ou=system
password: secret
ldiffile: classpath:/test.ldif
port: 18888
url: ldap://localhost:18888

So when I try to save the entity using the "posixAccount" object class, an error occurs because the "m-disabled" parameter is set to TRUE in cn = nis, ou = schema, which is not defined for me and I cannot access to changing this setting via Spring.

How can I dynamically change it?(((

entity:

@Entry(objectClasses = {"inetOrgPerson","top","posixAccount"})
public final class Person {
...................................
}

error, by the way:

 objectClass posixaccount w/ OID 1.3.6.1.1.1.2.0 not registered!
Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 54 - LOOP_DETECT

答案1

得分: 0

我只是覆盖了TestContextSourceFactoryBean类,并在createInstance()方法的末尾添加了以下代码:

Hashtable env = new Hashtable(2);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:" + port);
DirContext ctx = new InitialDirContext(env);
ctx.modifyAttributes("cn=nis,ou=schema", new ModificationItem[]{new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("m-disabled", "FALSE"))});
ctx.close();

这些代码将必要的属性m-disabled设置为FALSE,以便启用posixAccount

英文:

I simply override class TestContextSourceFactoryBean and write this lines to the end of createInstance() method:

Hashtable env = new Hashtable(2);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:" + port);
DirContext ctx = new InitialDirContext(env);
ctx.modifyAttributes("cn=nis,ou=schema", new ModificationItem[]{new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("m-disabled", "FALSE"))});
ctx.close();

which set necessary attribute m-disabled to FALSE in order to enable posixAccount,

huangapple
  • 本文由 发表于 2020年10月24日 22:56:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64514709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定