Spring Ldap - multipe base names

放肆的年华 提交于 2019-12-11 09:46:18

问题


I am just trying out spring ldap /odmnow. Seems interesting and cool. sorry if the below question is too dumb!

I am trying to use spring ldap /odm to retreive some attributes from out ldap.Is there is a way to configure multiple base names in

or in

@Entry(objectClasses = { "person"} base={..CAN I GIVE MULTIPLE BASENames here..}) public class LdapUser {

@Id
private Name dn;

The app I am developing has users defined under one OU and internal TESTERs defined in another ou in our AD. So I am trying to see if I can use the same ldap entry class for looking up everyone.


回答1:


The ContextSource base is intended to specify the base of all operations on the ContextSource, and is typically set to the domain controller DN.

You can use ODM without specifying a base on the @Entry (or using a base DN higher up in the tree), but in that case you will typically use the @DnAttribute annotation in order to have the framework automatically build DNs for you (mainly needed when persisting entries back to LDAP).

If we assume your users are in the following structure:

dc=example,dc=com,ou=USERS

dc=example,dc=com,ou=TESTERS

Now, if you specify base dc=example,dc=com on the ContextSource you can have ODM handle this automatically as described briefly below:

@Entry(objectclasses={"person"})
public class Person {
  @Id
  private Name dn;

  @DnAttribute(name="ou", index=0)
  @Transient // Indicates that this is not an attribute on the entry
  private String userType;

  @Attribute(name="cn")
  private String name;

  // More attributes here
}

The above will handle automatic mapping of LDAP entries to and from the Person class. Now, if you want to find all persons, do:

List<Person> allPersons = ldapTemplate.findAll(Person.class);

If you want to find all testers you would do:

List<Person> testers = ldapTemplate.find(
                            query().base("ou=TESTERS"), 
                            Person.class);



回答2:


I am not very familiar with Spring LDAP but (IIRC) LDAP itself can only search from a single node (base). So, looking at the documentation, you might have to do a search from the organization (o=xx) with an LDAPQueryBuilder, adding conditions for the ous. See the javadocs.




回答3:


No expert here, mind you. With XML config at least, you can wire an LdapTemplate instance. One suggestion might be to make a new implementation called something like DelegatingLdapTemplate that gets injected with two regular templates (one per basename) and then delegates to them appropriately (or just calls one, then the other if the first one return 0 results), and use this in place of a normal template instance. This of course makes sense only if your use case really warrants this behavior (e.g. if you never know where to search for the user and have to check both locations). Otherwise, just make two separate beans.



来源:https://stackoverflow.com/questions/25633364/spring-ldap-multipe-base-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!