简体   繁体   English

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

[英]How to search user in active directory with sAMaccountName, where sAMaccountName is a concat of (firstinitial+lastName)+(regex)ending with any digits?

I want to search a user in Active Directory with sAMaccountName,where the sAMaccountName is firstName.substring(0,1)+lastName+ending with any digit.我想用 sAMaccountName 在 Active Directory 中搜索用户,其中 sAMaccountName 是 firstName.substring(0,1)+lastName+以任何数字结尾。 Code Snippet:代码片段:

 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++;
      }`

Search filter used is使用的搜索过滤器是

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

I have tried with following search filters for sAMaccountName,but none worked and gives following exceptions我已经尝试使用以下搜索过滤器来搜索 sAMaccountName,但都没有奏效并给出以下异常

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

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

    after passing values searchfileter looks like:传递值后,searchfileter 看起来像:

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

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

    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+"\\d*"; String sAMAccountNameRegex=sAMAccountName+"\\d*";

    Exception:[[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\\d*))) )]

So is it possible to query Ldap where the searchfilter is combination of string and regex?那么是否可以在搜索过滤器是字符串和正则表达式的组合的情况下查询 Ldap?

This is how i tried and its working:这是我尝试的方式及其工作方式:

I am querying active directory with sAMaccountName*,i get the user and then handling the regex operation in my code locally我正在使用 sAMaccountName* 查询活动目录,我获取用户,然后在本地处理我的代码中的正则表达式操作

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LDAP:如何使用sAMAccountName对用户进行身份验证? - LDAP: How to authenticate user with sAMAccountName? 如何从Java查询LDAP以从Active Directory中的“ netbiosDomain \\ samAccountName”中获取对象的DN - How to query LDAP from Java to get an object's DN from the “netbiosDomain\samAccountName” from Active Directory 使用Java查询活动目录以将samaccountname绑定到特定组 - Query active directory Using Java for samaccountname's Tied to a Specific Group 如何获取LastName,FirstName的字符串并返回FirstInitial.LastName - How to take a String of LastName, FirstName and return FirstInitial.LastName 如何使用Java DirContext搜索具有下级登录名(NETBIOS \\ sAMAccountName)的用户 - How to use Java DirContext to search for users with down-level logon name (NETBIOS\sAMAccountName) 如何使用Apache Shiro在LDAP中使用sAMAccountName或其他任何参数支持登录? - How to support login using sAMAccountName or any other parameter in LDAP with Apache Shiro? LDAP searchFilter samAccountName来自用户输入定义的变量? Java JNDI。 有人知道怎么做吗? - LDAP searchFilter samAccountName from variable defined by user input ? java JNDI. DOes anybody know how to do it? 检索 LDAP 组中用户的 sAMAccountName - Retrieve sAMAccountName of users in LDAP group 使用sAMAccountName更改LDAP中的密码 - Change password in LDAP using sAMAccountName 如何使用Spring Ldap在Active Directory中对用户进行身份验证和搜索 - How authenticate and search user in Active Directory using Spring Ldap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM