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

2024-05-10Java开发问题
5

本文介绍了通过 Java 更新 LDAP 时如何指定哈希算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

当您使用 Java API 更新 Open LDAP 目录时,有没有办法指定哈希算法(MD5、SHA1 等)用于存储密码,代码如下:

Is there a way to specify the hash algorithm (MD5, SHA1, etc.) to use for storing the passwords when you update an Open LDAP directory using Java APIs with code like this:

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)显示

Something along these lines should do it: (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 更新 LDAP 时如何指定哈希算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13