我有以下JNDI代碼來生成一個新的用戶密碼進入Apache的DS:ApacheDS - 如何使用Java JNDI創建新用戶並設置密碼?
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服務器說密碼是用特定的散列編碼的?
您可能想要添加一個代碼片段,顯示如何將用戶對象存儲到Ap中疼痛DS。 – Perception 2012-02-04 14:07:54