It's complicated. You cannot set passwords unencrypted, and if you have not configured your entire cryptographic structure, you cannot use LDAPS, so you need to use Kerberos.
:
- AD
-
- , )
.
System.setProperty("java.security.krb5.kdc", "domain.com");
String username = "admin@DOMAIN.COM";
String realm = "DOMAIN.COM";
System.setProperty("java.security.krb5.realm", realm);
System.setProperty("java.security.krb5.debug", "true");
System.setProperty("sun.security.krb5.debug", "true");
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.PROVIDER_URL, "ldap://dc01.domain.com:389");
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, "abcd1234");
ctxt = new InitialLdapContext(env, new Control [0]);
LoginModule module;
module = (LoginModule) Class.forName("com.sun.security.auth.module.Krb5LoginModule").newInstance();
Subject subject = new Subject();
Map<String, String> options = new HashMap<String, String>();
Map<String, Object> sharedState = new HashMap<String, Object>();
sharedState.put("javax.security.auth.login.password", properties.getProperty("ad.passwd").toCharArray());
sharedState.put("javax.security.auth.login.name", username);
options.put("principal", username);
options.put("storeKey", "true");
options.put("useFirstPass", "true");
options.put("debug", "true");
module.initialize(subject, null, sharedState, options);
module.login();
module.commit();
Subject.doAs(svc.getSubject(), new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
try {
String password = "\"Password1\"";
final Hashtable<String, String> env = svc.getEnvironment();
env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI");
LdapContext ctxt = new InitialLdapContext(env, new Control[0]);
ModificationItem[] mods = new ModificationItem[1];
mods[0] = new
ModificationItem(DirContext.REPLACE_ATTRIBUTE, new
BasicAttribute("userPassword", password.getBytes("UTF-16LE")));
ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWORD_EXPIRED)));
ctxt.modifyAttributes(dn, mods);
}
catch (NamingException e) {
System.out.println("Failed to set password.");
e.printStackTrace();
}
return null;
}
});
.
, .