代码之家  ›  专栏  ›  技术社区  ›  JuanZe Nirbhay Tandon

如何在通过Java更新LDAP时指定散列算法?

  •  3
  • JuanZe Nirbhay Tandon  · 技术社区  · 15 年前

    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);
    }
    
    2 回复  |  直到 15 年前
        1
  •  5
  •   jwilleke    15 年前

    沿着这条线的一些东西应该做它:(MD5)显示

    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;
    }
    

        2
  •  1
  •   ring bearer    15 年前

    我想你必须用SHA1来散列你的密码( http://java.sun.com/javase/6/docs/technotes/guides/security/ ),使用