如何使用sAMaccountName搜索活动目录中的用户,其中sAMaccountName是(firstinitial + lastName)+(regex)以任何数字结尾的concat?

时间:2015-12-22 13:49:32

标签: java regex active-directory

我想使用sAMaccountName在Active Directory中搜索用户,其中sAMaccountName是firstName.substring(0,1)+ lastName +以任何数字结尾。 代码段:

 try {
      context=this.getADConnection();

      String returnedAtts[]={"givenName","sn"};
      String sAMAccountNameRegex=sAMAccountName+"\\d*";
      //String sAMAccountNameRegex=sAMAccountName+Pattern.quote("[0-9]*");
      SearchControls searchControls = new SearchControls();
  searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  searchControls.setReturningAttributes(returnedAtts);
  searchControls.setReturningAttributes(returnedAtts);
  searchControls.setReturningAttributes(returnedAtts);
  //String searchFilter = "(&(objectClass=user)(givenName=" +firstInitial+"*)(sn="+lastName+"))";
  String searchFilter = "(&(objectClass=user) (|(&(givenName=" +firstInitial+"*)(sn="+lastName+"))(sAMAccountName=("+sAMAccountNameRegex+"))) )";
  logger.info(className + privateMethodName + "  Searching User using filter : [" + searchFilter + "]");
  // Search for objects using the filter
  // Search for objects using the filter
      NamingEnumeration<SearchResult> results = context.search(SAMAccountNamePrePop.adSearchBase, searchFilter, searchControls);
      SearchResult searchResult = null;
      while(results.hasMoreElements()) {
      searchResult = (SearchResult) results.nextElement();
      logger.info(className + privateMethodName + " Search Result : [" + searchResult + "]");
      totalResults++;
      }`

使用的搜索过滤器是

String searchFilter = "(&(objectClass=user) (|(&(givenName=" +firstInitial+"*)(sn="+lastName+"))(sAMAccountName=("+sAMAccountNameRegex+"))) )";

我尝试使用以下搜索过滤器来搜索sAMaccountName,但没有一个能够工作并提供以下异常

  1. String sAMAccountNameRegex=sAMaccountName.Pattern.quote("\\d*");

    Exception is: [invalid escape sequence: [B@755c9b9c]

    传递值后,searchfileter看起来像:

  2. [(&(objectClass=user) (|(&(givenName=C*)(sn=BOND3))(sAMAccountName=(CBOND3\Q\d*\E))) )]

    1. String sAMAccountNameRegex = sAMAccountName +&#34;([0-9] *)$&#34 ;;

      Exception is:[[LDAP: error code 32 - 0000208D: NameErr: DSID-031001E5, problem 2001 (NO_OBJECT), data 0, best match of: '']]

      after passing values searchfileter looks like:

      [(&(objectClass=user) (|(&(givenName=C*)(sn=BOND3))(sAMAccountName=(CBOND3([0-9]*)$))) )]

    2. String sAMAccountNameRegex = sAMAccountName +&#34; \ d *&#34;;

      Exception:[[LDAP: error code 32 - 0000208D: NameErr: DSID-031001E5, problem 2001 (NO_OBJECT), data 0, best match of: '' ]]

      after passing values searchfileter looks like:

    3. [(&(objectClass=user) (|(&(givenName=C*)(sn=BOND3))(sAMAccountName=(CBOND3\d*))) )]

      那么可以查询Ldap,其中searchfilter是字符串和正则表达式的组合吗?

1 个答案:

答案 0 :(得分:1)

这就是我尝试和工作的方式:

我用sAMaccountName *查询活动目录,我得到用户然后在本地代码中处理正则表达式操作

Attributes attrs = ((SearchResult) answer.next());
String userId=attrs.get("sAMAccountNAme").toString();
if(userId.matches(sAMAccountNameRegex)){
    //business logic goes here
}