public void initRealm() { logger.debug("Initializing LDAP realm."); this.searchBase = config.getUserLdapBaseDn(); this.useSSL = config.getLdapUseSsl(); if (useSSL) { this.url = "ldaps://" + config.getLdapServer() + ":" + config.getLdapPort(); } else { this.url = "ldap://" + config.getLdapServer() + ":" + config.getLdapPort(); } this.systemUsername = config.getLdapUsername(); this.systemPassword = config.getLdapPassword(); this.objectClasses = config.getUserLdapObjectClasses(); this.userIdAttribute = config.getUserLdapUidAttribute(); logger.debug( "searchBase => {}, url => {}, systemUsername => {}, systemPassword => {}", new Object[] {searchBase, url, systemUsername, systemPassword}); logger.debug("user object classes => {}", objectClasses); logger.debug("user id attribute => {}", userIdAttribute); logger.info("Successfully initialized LDAP realm."); }
@Override protected AuthenticationInfo queryForAuthenticationInfo( AuthenticationToken token, LdapContextFactory contextFactory) throws NamingException { logger.debug( "queryForAuthenticationInfo, principal: {}, credentials: *****", token.getPrincipal()); logger.debug("contextFactory : {}", contextFactory); try { if (token == null || token.getPrincipal() == null) { logger.info("No authentication token provided, will not try to authenticate.."); return null; } LdapContext sysCtx = contextFactory.getSystemLdapContext(); String objClsFilter = createObjectClassFilter(objectClasses); String userIdFilter = createAttributeFilter(userIdAttribute, token.getPrincipal().toString()); String filter = mergeFiltersAND(objClsFilter, userIdFilter); NamingEnumeration<?> namingEnumeration = sysCtx.search(config.getUserLdapBaseDn(), filter, getSimpleSearchControls()); while (namingEnumeration.hasMore()) { SearchResult result = (SearchResult) namingEnumeration.next(); String dn = result.getNameInNamespace(); try { contextFactory.getLdapContext(dn, token.getCredentials()); return new SimpleAuthenticationInfo(dn, token.getCredentials(), "StaticRealm"); } catch (Exception e) { logger.error(e.getMessage(), e); } } } catch (Exception e) { logger.error(e.getMessage(), e); } return null; }