gpt4 book ai didi

java - 通过Java更新LDAP时如何指定哈希算法?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:57:46 25 4
gpt4 key购买 nike

当您使用 Java API 更新 Open LDAP 目录时,是否有一种方法可以指定用于存储密码的哈希算法(MD5、SHA1 等),代码如下:

private void resetPassword(String principal, String newPassword) throws NamingException {
InitialDirContext ctxAdmin = null;
Hashtable<String, String> ctxData = new Hashtable<String, String>();
ctxData.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ctxData.put(Context.PROVIDER_URL, "ldap://myserver:389");
ctxData.put(Context.SECURITY_AUTHENTICATION, "simple");
ctxData.put(Context.SECURITY_PRINCIPAL, "admin_dn");
ctxData.put(Context.SECURITY_CREDENTIALS, "admin_passwd");
InitialDirContext ctxAdmin = new InitialDirContext(ctxData);
if (newPassword == null || newPassword.equals("")) {
String msg = "Password can't be null";
throw new NamingException(msg);
} else {
if (principal == null || principal.equals("")) {
String msg = "Principal can't be null";
throw new NamingException(msg);
} else {
if (ctxAdmin == null) {
String errCtx = "Can't get LDAP context";
throw new NamingException(errCtx);
}
}
}
BasicAttribute attr = new BasicAttribute("userpassword", newPassword);
ModificationItem modItem = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
ModificationItem[] items = new ModificationItem[1];
items[0] = modItem;
ctxAdmin.modifyAttributes("cn=" + principal + ",ou=Users,dc=com", items);
}

最佳答案

按照这些思路应该可以做到:(MD5) Shown

context.setAttributeValue("userPassword", digestMd5("newPassword));

private String digestMd5(final String password) {
String base64;
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(password.getBytes());
base64 = new BASE64Encoder().encode(digest.digest());
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return "{MD5}" + base64;
}

-吉姆

关于java - 通过Java更新LDAP时如何指定哈希算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2639167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com