我正在尝试使用spring ldap对活动目录进行身份验证,此代码完全适用于打开的ldap,但它不适用于Microsoft Active目录,这是验证时的异常:
Exception in thread "main" org.springframework.ldap.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A8, comment: AcceptSecurityContext error, data 52e, v1db1
码
public class Main {
private LdapTemplate ldapTemplate;
private ContextSource contextSource;
public static void main(String[] args) {
Main main = new Main();
main.initialize();
main.authenticate();
}
private void initialize() {
LdapContextSource lcs = new LdapContextSource();
lcs.setUrl("ldap://52.187.56.263:389/");
lcs.setUserDn("cn=shoukathmd,dc=xyz,dc=com");
lcs.setPassword("Admin12345678");
lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
lcs.afterPropertiesSet();
ldapTemplate = new LdapTemplate(lcs);
contextSource = lcs;
}
public boolean authenticate() {
DirContext ctx = null;
try {
ldapTemplate.authenticate(query().base("dc=xyz,dc=com").where("mail").is("shoukathmd@xyz.com"), "Admin12345678");
return true;
} catch (Exception e) {
// Context creation failed - authentication did not succeed
//logger.error("Login failed", e);
e.printStackTrace();
return false;
} finally {
// It is imperative that the created DirContext instance is always closed
LdapUtils.closeContext(ctx);
}
}
}