我有以下 JNDI 代码来将新用户的密码生成到 Apache DS 中:
private String digest(String algorithm,String password) throws NoSuchAlgorithmException {
String r = null;
byte [] b = null;
MessageDigest md = MessageDigest.getInstance(algorithm);
BASE64Encoder encoder;
md.update(password.getBytes());
b = md.digest();
encoder = new BASE64Encoder();
System.out.println(encoder.encode(b));
r = encoder.encode(b);
return r;
}
此代码添加新用户:
public User create(User t) throws PersistenceException {
NamingEnumeration answer = null;
Attributes matchAttrs = null;
Attribute objectClass = new BasicAttribute("objectClass");
try {
matchAttrs = new BasicAttributes(true); // ignore attribute name case
matchAttrs.put(new BasicAttribute("uid",t.getCommonId()));
answer = getConnection().search(userContext, matchAttrs);
if( ! answer.hasMore() )
{
matchAttrs = new BasicAttributes(true);
objectClass.add("inetOrgPerson");
objectClass.add("organizationalPerson");
objectClass.add("person");
objectClass.add("top");
matchAttrs.put(objectClass);
matchAttrs.put(new BasicAttribute("cn", t.getFirstName()));
matchAttrs.put(new BasicAttribute("sn", t.getLastName()));
matchAttrs.put(new BasicAttribute("givenName", t.getFirstName()));
matchAttrs.put(new BasicAttribute("mail", t.getCommonId()));
matchAttrs.put(new BasicAttribute("userPassword", diggest("MD5",t.getPassword())));
getConnection().createSubcontext("uid="+t.getCommonId()+","+userContext,matchAttrs);
}
else
throw new PersistenceException("This user already exists.");
} catch (NoSuchAlgorithmException ex) {
throw new PersistenceException("LDAP exception creating user - Hash algorithm not found.");
} catch (NamingException ex) {
ex.printStackTrace();
throw new PersistenceException("LDAP exception creating user.");
}
return t;
}
当我调用此代码时,它会生成一个哈希 MD5(我将“MD5”作为算法传递),然后它在 Base64 中编码并返回要用于 LDAP(apacheds)服务器的新用户的密码。
然而,服务器总是创建用户并将“SSHA”作为创建用户的算法。我该如何解决?我尝试了很多选项都没有成功,现在我决定问一下。有没有办法告诉 LDAP 服务器密码是用特定的哈希编码的?