活动目录更改密码 - Java

时间:2014-10-09 05:31:23

标签: active-directory

我正在尝试更改Active Directory用户的密码并获取错误代码65.请参阅下面我使用的源代码。

public class ADConnection {
    DirContext ldapContext;
    String baseName = ",CN=Users,DC=wso2,DC=test";

    public ADConnection() {
        try {
            Hashtable ldapEnv = new Hashtable(11);
            ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY,
                    "com.sun.jndi.ldap.LdapCtxFactory");
            ldapEnv.put(Context.PROVIDER_URL, "ldaps://192.168.18.xx:636");
            ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
            ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=administrator"
                    + baseName);
            ldapEnv.put(Context.SECURITY_CREDENTIALS, "xxxxxx");
            ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl");
            ldapContext = new InitialDirContext(ldapEnv);
        } catch (Exception e) {
            System.out.println(" bind error: " + e);
            e.printStackTrace();
            System.exit(-1);
        }
    }

    public void updatePassword(String username, String password) {
        try {
            String quotedPassword = "\"" + password + "\"";
            char unicodePwd[] = quotedPassword.toCharArray();
            byte pwdArray[] = new byte[unicodePwd.length * 2];
            for (int i = 0; i < unicodePwd.length; i++) {
                pwdArray[i * 2 + 1] = (byte) (unicodePwd[i] >>> 8);
                pwdArray[i * 2 + 0] = (byte) (unicodePwd[i] & 0xff);
            }
            ModificationItem[] mods = new ModificationItem[1];
            mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                    new BasicAttribute("UnicodePwd", pwdArray));
            ldapContext.modifyAttributes("cn=" + username + baseName, mods);
            //ldapContext.modifyAttributes("cn=" + username, mods);
        } catch (Exception e) {
            System.out.println("update password error: " + e);
            System.exit(-1);
        }
    }

    public static void main(String[] args) {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        // the keystore that holds trusted root certificates
        System.setProperty("javax.net.ssl.trustStore", "client-truststore.jks");
        //System.setProperty("javax.net.debug", "all");
        ADConnection adc = new ADConnection();
        adc.updatePassword("admin", "pass@word3");
    }
}

我使用了以下链接中的代码。 http://blogs.msdn.com/b/alextch/archive/2012/05/15/how-to-set-active-directory-password-from-java-application.aspx 这有什么不对?

由于

0 个答案:

没有答案