Вот так я проверял зарегистрирован ли пользователь в LDAP или нет. Закомментированный блок почему то не работает с AD. Он узнаёт какие способы аутентификации поддерживает сервер LDAP. Но и без него можно обойтись.
| Код | public class CActiveDirectoryService { /** * AD server host. */ private String host;
/** * AD server domain. */ private String domain;
/** * AD server port. */ private String port;
/** * Prefix for user name. */ private String userNamePrefix;
/** * base for user name */ private String base;
/** * digest method name for the authentication */ private String authDigetsMethod;
/** * defines support LDAP server digest authentication or not */ private boolean digestMethodSupport;
/** * Logger for this class */ private static final Logger logger = Logger.getLogger(CActiveDirectoryService.class);
/** * Creates new instance. */ public CActiveDirectoryService() {
host = CResourceBundleUtil.getLdapBundleString("ldap.host", null); domain = CResourceBundleUtil.getLdapBundleString("ldap.domain", null); port = CResourceBundleUtil.getLdapBundleString("ldap.port", null); base = CResourceBundleUtil.getLdapBundleString("ldap.base", null); userNamePrefix = CResourceBundleUtil.getLdapBundleString("ldap.userNamePrefix", null); authDigetsMethod = CResourceBundleUtil .getLdapBundleString("ldap.auth.digest.method", null); digestMethodSupport = false; }
/** * Returns true if user with provided credentials is registered * in Active Directory. * * @param userName login of the user to check. * @param password password of the user. * @return true if user with provided credentials is registered * in Active Directory * @throws NamingException if smth wrong */ public boolean isUserRegisteredInActiveDirectory(String userName, String password) throws NamingException {
String urlDC = "ldap://" + host + "." + domain + ":" + port + "/"; Hashtable ldapEnv = new Hashtable(); /* // getting supported SASL Mechanism for the authentication LdapContext ctx = new InitialLdapContext(); Enumeration enumeration = ctx.getAttributes(urlDC, new String[] { "supportedSASLMechanisms" }).getAll();
while (enumeration.hasMoreElements()) { Attribute attr = (Attribute) enumeration.nextElement();
if (attr.contains(authDigetsMethod)) { digestMethodSupport = true; } }
if (digestMethodSupport) { ldapEnv.put(Context.SECURITY_AUTHENTICATION, authDigetsMethod); ldapEnv.put(Context.SECURITY_PRINCIPAL, userName); logger.debug("Authentication method: " + authDigetsMethod); } else { ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); ldapEnv.put(Context.SECURITY_PRINCIPAL, userNamePrefix + userName + "," + base); logger.debug("Authentication method: simple"); } */ logger.debug("Authentication method: simple"); ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); ldapEnv.put(Context.SECURITY_PRINCIPAL, userNamePrefix + userName); ldapEnv.put(Context.SECURITY_CREDENTIALS, password); ldapEnv.put(Context.PROVIDER_URL, urlDC); ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); try { LdapContext ctx = new InitialLdapContext(ldapEnv, null); return true; } catch (AuthenticationException e) { logger.error("Error accessing Active Directory:", e); return false; } catch (NamingException e) { logger.error("Error accessing Active Directory:", e); throw new NamingException("Error accessing Active Directory"); } finally { if (ctx != null) { ctx.close(); } } } }
|
|