[英]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,但都没有奏效并给出以下异常
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))) )]
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]*)$))) )]
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.