gpt4 book ai didi

java - 通过 ldap 和 SSL 连接更改 AD 中的密码时,未创建 InitialLdapContext ,异常 :connection reset

转载 作者:行者123 更新时间:2023-12-01 18:05:57 26 4
gpt4 key购买 nike

我正在尝试使用 LDAP 在我的网站中实现更改密码功能。问题:-通过 ldap 和 SSl 连接更改 AD(服务器)中的密码时,它没有创建 InitialLdapContext ,出现异常:连接重置。附加了异常的屏幕截图。还添加了认证。

我也检查了端口。它正在通过 telnet 连接。

感谢任何帮助。

public String changePassword(String username,String currentPassword, String newPassword, String 
confirmPassword) {

String retVal="failed";
String domain = "edw.obc.co.in";
Properties prop = new Properties();
String LdapUserName="CN="+username ; //g

//+ "ou=users" + "dc=edw,dc=obc,dc=co,dc=in" ; g


String DomainUseName = username+"@" + domain;
prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
prop.put(Context.PROVIDER_URL, "ldap://172.xx.xx.xx:636");
prop.put(Context.SECURITY_AUTHENTICATION, "simple");
prop.put(Context.SECURITY_PROTOCOL,"ssl");
//prop.put("javax.net.ssl.truststore","C:\\Program Files\\Java\\jdk-12.0.1\\lib\\security\\cacerts");
//prop.put("javax.net.ssl.truststorePassword","changeit");
System.setProperty("javax.net.ssl.truststore", "C:\\Program Files\\Java\\jdk-12.0.1\\lib\\security\\cacerts");
System.setProperty("javax.net.ssl.truststorePassword", "changeit");


//prop.put(Context.SECURITY_PRINCIPAL,LdapUserName);
prop.put(Context.SECURITY_PRINCIPAL,"winadmin");
//prop.put("LDAP_BASEDN","ou=edw,dc=obc,dc=co.in");
prop.put(Context.SECURITY_CREDENTIALS,"wipro@123");
//prop.put(Context.SECURITY_CREDENTIALS,"wipro@123");

prop.put(Context.REFERRAL,"follow");
try
{
LdapContext ctx =new InitialLdapContext(prop,null);

System.out.print("XXXXXXXXXXXXXXXXXXXXXXXXXXX");

SearchControls searchControls = new SearchControls();

searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

NamingEnumeration objects = null;

try {

objects=ctx.search("cn="+username+",ou=Users,"+"dc=edw"+","+"dc=obc"+","+"dc=co"+","+"dc=in", String.format("(&(objectClass=person)(sAMAccounName=%s))", LdapUserName),searchControls);

}
catch(NamingException e) {
e.printStackTrace();
}


String theUserName="cn="+username+",ou=Users";


// Perform the update


ModificationItem[] mods = new ModificationItem[1];
String newQuotedPassword = "\"" + newPassword + "\"";
byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("userpassword", newUnicodePassword));

ctx.modifyAttributes(theUserName, mods);
retVal="success";
System.out.println("Changed Password for user successfull");
ctx.close();
}
catch (Exception e) {
e.printStackTrace();
System.err.println("Problem changing password: " + e);
}



return retVal;
}

private static byte[] getPasswordByteArray(String password)

{

String quotedPassword = "\"" + password + "\"";

try

{

return quotedPassword.getBytes("UTF-16LE");

}

catch(UnsupportedEncodingException e)

{

e.printStackTrace();

return null;

}

}

}

最佳答案

更改密码的协议(protocol)必须是 ldaps 而不是 ldap:

prop.put(Context.PROVIDER_URL, "ldap://172.xx.xx.xx:636");
^^^^
<小时/>

此外,考虑替换:

ModificationItem[] mods = new ModificationItem[1];
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("userpassword", newUnicodePassword));

与:

ModificationItem[] mods = {new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("unicodePwd", oldPasswordBytes)),
new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("unicodePwd", newPasswordBytes))};

无需 Active Directory 管理员权限即可更改用户密码。

关于java - 通过 ldap 和 SSL 连接更改 AD 中的密码时,未创建 InitialLdapContext ,异常 :connection reset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60559075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com