JNDI:如何使用Logical not query来搜索LDAP

时间:2016-05-03 04:58:28

标签: java ldap jndi ldap-query

我在应用逻辑非查询时遇到问题。

例如,

public class MultiThreading {
  public static void main(String[] args) throws Exception{
    // Use fair queuing to prevent an out-of-order task
    // from jumping to the head of the line again
    // try setting this to false - you'll see far more re-queuing calls
    BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1, true);
    for (int i = 5; i >= 1; i--) {
      Thread.sleep(100); // not necessary, just helps demonstrate the queuing behavior
      new MyThread(i, queue).start();
    }
    queue.add(1); // work starts now
  }

  static class MyThread extends Thread {
    int var;
    BlockingQueue<Integer> queue;

    public MyThread(int var, BlockingQueue<Integer> queue) {
      this.var = var;
      this.queue = queue;
    }

    @Override
    public void run() {
      System.out.println("Task " + var + " is now pending...");
      try {
        while (true) {
          int task = queue.take();
          if (task != var) {
            System.out.println(
                "Task " + var + " got task " + task + " instead - re-queuing");
            queue.add(task);
          } else {
            break;
          }
        }
      } catch (InterruptedException e) {
        // If a thread is interrupted, re-mark the thread interrupted and terminate
        Thread.currentThread().interrupt();
        return;
      }

      System.out.println("Finished task " + var);

      System.out.println("Registering task " + (var + 1) + " to run next");
      queue.add(var + 1);
    }
  }
}

上面的声明工作得非常好,它返回了所有人的条目,其中sn是Kumaran。

假设我更改了语句以获取sn Kumaran的所有人员条目。

Task 5 is now pending...
Task 4 is now pending...
Task 3 is now pending...
Task 2 is now pending...
Task 1 is now pending...
Task 5 got task 1 instead - re-queuing
Task 4 got task 1 instead - re-queuing
Task 3 got task 1 instead - re-queuing
Task 2 got task 1 instead - re-queuing
Finished task 1
Registering task 2 to run next
Task 5 got task 2 instead - re-queuing
Task 4 got task 2 instead - re-queuing
Task 3 got task 2 instead - re-queuing
Finished task 2
Registering task 3 to run next
Task 5 got task 3 instead - re-queuing
Task 4 got task 3 instead - re-queuing
Finished task 3
Registering task 4 to run next
Task 5 got task 4 instead - re-queuing
Finished task 4
Registering task 5 to run next
Finished task 5
Registering task 6 to run next

我收到以下错误:

NamingEnumeration<SearchResult> results =
    context.search("ou=people,dc=example,dc=com", "sn=Kumaran", searcCon);

以下是我的代码

NamingEnumeration<SearchResult> results =
   context.search("ou=people,dc=example,dc=com", "(!sn=Kumaran)", searcCon);

参考

https://technet.microsoft.com/en-us/library/aa996205(v=exchg.65).aspx

2 个答案:

答案 0 :(得分:3)

尝试添加括号(!(sn=Kumaran))

<强>更新

试试这个

...
SearchControls searcCon = new SearchControls();
String[] attributeFilter = { "cn", "description" };
searcCon.setReturningAttributes(attributeFilter);
searcCon.setSearchScope(SearchControls.SUBTREE_SCOPE);
...

答案 1 :(得分:0)

参见RFC 2254:

  

filter :: =“(”filtercomp“)”

     

not :: =“!”过滤

您的代码应该像

context.search("ou=people,dc=example,dc=com", "((!sn=Kumaran))", searcCon);

它应该适合你。