如何使用samAccountName修改LDAP术语?
我正在尝试更改Java LDAP帐户的到期日期。
我有一个servlet加载一个潜在帐户列表,用户将选择一个,选择一个日期,然后单击“更新”,
然后我将执行一些检查,例如验证上一张新日期是否在上一个之后设置。 因此,我加载所选帐户的属性。
现在,当一切正常时,我想执行更新,但是我不明白修改方法的“名称”属性。 我想使用samaccountname。
这是代码:
// Récupération des infos sur le compte a modifier :
Hashtable env = new Hashtable();
env.put(InitialDirContext.SECURITY_AUTHENTICATION,"simple");
env.put(InitialDirContext.SECURITY_PRINCIPAL, userLDAP);
env.put(InitialDirContext.SECURITY_CREDENTIALS, passwordLDAP);
env.put(InitialDirContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(InitialDirContext.PROVIDER_URL, urlLDAP);
DirContext ldapCtx = new InitialDirContext(env);
SearchControls searchCtls = new SearchControls();
// Paramétrage des critères de recherche et des attributs a retourner
searchCtls.setReturningObjFlag(false);
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = {"displayName", "sAMAccountName", "accountExpires", "memberof","crnrDomainassoc01position"};
searchCtls.setReturningAttributes(attrIDs);
String searchBase = "OU=VPN,OU=Exterieurs,DC=chu,DC=lan";
// searchFilter based on the selected user on the liste compteVPN
String searchFilter = "(&(objectCategory=User)(sAMAccountName=" + compteVPN + "))";
NamingEnumeration<SearchResult> answer = ldapCtx.search(searchBase, searchFilter, searchCtls);
while (answer.hasMoreElements())
{
SearchResult sr = (SearchResult)answer.next();
Attributes attrs = sr.getAttributes();
displayName = attributeToString(attrs.get("displayName"));
System.out.println(" --> displayName : " + displayName);
sAMAccountName = attributeToString(attrs.get("sAMAccountName"));
memberof = attributeToString(attrs.get("memberof"));
crnrDomainassoc01position = attributeToString(attrs.get("crnrDomainassoc01position"));
accountExpires = attributeToString(attrs.get("accountExpires"));
java.util.Date expiracy = timeToDate(accountExpires);
accountExpiresFormat = new SimpleDateFormat("'le' dd/MM/yyyy 'à' kk:mm:ss").format(expiracy) ;
}
// retrieve the new date, perform some checks ... then
// Attribut a modifier
Attribute attribute = new BasicAttribute("accountExpires", dateToTime(dateExpirationMarge));
// array of modified iteams
ModificationItem[] item = new ModificationItem[1];
// replacing the value
item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute);
// changing the value of the attribute
ctx.modifyAttributes(?????????, item);
我应该放什么而不是放置? ? 感谢您的帮助,这种LDAP的事情使我发疯:)
编辑: 遵循Ericlavault的输入后,我使用 sr.getName(),但我会收到以下错误:
javax.naming.NamingException:[ldap:错误代码1- 000020D6:svcerr:svcerr:dsid -03100836,问题50122 (dir_error),数据0];其余名称“ CN =维护Axians'
这些是上下文搜索的结果:
samaccountName :axians
显示名称:维护轴
成员: Cn = PSO10-DSI,OU = PSO,OU = CHUSTE,DC = CHU,DC = LAN | CN = vpn_tout,OU = vpn,dc = chu,dc = lan i
我试图将显示名称修改为“维护” emaintenance_axians自发删除空间,因为我研究指出了LDAP无法正确处理空间的问题,但并不能解决我的问题
javax.Naming.NamingException:[LDAP:错误代码1- 000020D6:SVCERR:SVCERR:DSID -03100836,问题5012(dir_error),数据0,数据0 这是给出的 Javax.Naming.NamingException:[LDAP:错误代码1- 000020D6:SVCERR:DSID -03100836,问题5012(dir_error),数据0 ];其余名称'cn =维护_axians'
I'm trying to change the expiracy date of a LDAP account in Java.
I have a servlet which load a list of potential accounts and the user will select one, choose a date and click on 'update'
Then I will perform some checks, for example verify if the new date is set after the previous one.
So I load the attributes of the selected account.
Now when everything is OK I want to perform the update but I don't get what should be the 'name' attribute of the modify method.
I wanted to use the sAMAccountName.
Here is the code :
// Récupération des infos sur le compte a modifier :
Hashtable env = new Hashtable();
env.put(InitialDirContext.SECURITY_AUTHENTICATION,"simple");
env.put(InitialDirContext.SECURITY_PRINCIPAL, userLDAP);
env.put(InitialDirContext.SECURITY_CREDENTIALS, passwordLDAP);
env.put(InitialDirContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(InitialDirContext.PROVIDER_URL, urlLDAP);
DirContext ldapCtx = new InitialDirContext(env);
SearchControls searchCtls = new SearchControls();
// Paramétrage des critères de recherche et des attributs a retourner
searchCtls.setReturningObjFlag(false);
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = {"displayName", "sAMAccountName", "accountExpires", "memberof","crnrDomainassoc01position"};
searchCtls.setReturningAttributes(attrIDs);
String searchBase = "OU=VPN,OU=Exterieurs,DC=chu,DC=lan";
// searchFilter based on the selected user on the liste compteVPN
String searchFilter = "(&(objectCategory=User)(sAMAccountName=" + compteVPN + "))";
NamingEnumeration<SearchResult> answer = ldapCtx.search(searchBase, searchFilter, searchCtls);
while (answer.hasMoreElements())
{
SearchResult sr = (SearchResult)answer.next();
Attributes attrs = sr.getAttributes();
displayName = attributeToString(attrs.get("displayName"));
System.out.println(" --> displayName : " + displayName);
sAMAccountName = attributeToString(attrs.get("sAMAccountName"));
memberof = attributeToString(attrs.get("memberof"));
crnrDomainassoc01position = attributeToString(attrs.get("crnrDomainassoc01position"));
accountExpires = attributeToString(attrs.get("accountExpires"));
java.util.Date expiracy = timeToDate(accountExpires);
accountExpiresFormat = new SimpleDateFormat("'le' dd/MM/yyyy 'à' kk:mm:ss").format(expiracy) ;
}
// retrieve the new date, perform some checks ... then
// Attribut a modifier
Attribute attribute = new BasicAttribute("accountExpires", dateToTime(dateExpirationMarge));
// array of modified iteams
ModificationItem[] item = new ModificationItem[1];
// replacing the value
item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute);
// changing the value of the attribute
ctx.modifyAttributes(?????????, item);
What should I put instead of ???????? ?
Thanks for your help, this LDAP thing is driving me crazy :)
EDIT:
Following EricLavault's input I used sr.getName() but I get the following error :
javax.naming.NamingException: [LDAP: error code 1 - 000020D6: SvcErr: DSID-03100836, problem 5012 (DIR_ERROR), data 0 ]; remaining name 'CN=Maintenance Axians'
Those are the results of the context search done just before :
sAMAccountName : axians
displayName : Maintenance Axians
memberof :
CN=Pso10-DSI,OU=PSO,OU=CHUSTE,DC=chu,DC=lan|CN=Vpn_Tout,OU=VPN,DC=chu,DC=lan
I tried to modify the displayName to Maintenance_Axians removing the space since my research pointed an issue with LDAP not handling spaces correctly but it didn't solve my issue
javax.naming.NamingException: [LDAP: error code 1 - 000020D6: SvcErr: DSID-03100836, problem 5012 (DIR_ERROR), data 0
]
javax.naming.NamingException: [LDAP: error code 1 - 000020D6: SvcErr: DSID-03100836, problem 5012 (DIR_ERROR), data 0
]; remaining name 'CN=Maintenance_Axians'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论