Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by asd123 ( 15 years ago )
package xxxxxldap.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import xxxxxldap.service.annotations.LdapModuleProperties;
import xxxxxldap.service.entities.LdapUserProxy;
import org.apache.commons.configuration.Configuration;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
@Singleton
public class LdapServiceImpl implements LdapService {
private final Provider<Configuration> ldapPropertyContainer;
@Inject
public LdapServiceImpl(
@LdapModuleProperties Provider<Configuration> ldapPropertyContainer
) {
/* store objects */
this.ldapPropertyContainer = ldapPropertyContainer;
}
private Properties compileProperties(){
Properties props = new Properties();
Configuration cfg = ldapPropertyContainer.get();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.setProperty(Context.PROVIDER_URL, cfg.getString(LdapModule.PROPERTY_SERVER_URL));
props.setProperty(Context.URL_PKG_PREFIXES, "com.sun.jndi.url");
props.setProperty(Context.REFERRAL, "ignore");
props.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
props.setProperty(Context.SECURITY_PRINCIPAL, cfg.getString(LdapModule.PROPERTY_LDAP_PRINCIPAL));
props.setProperty(Context.SECURITY_CREDENTIALS, cfg.getString(LdapModule.PROPERTY_LDAP_CREDENTIALS));
return props;
}
@Override
public List<LdapUserProxy> getUserFromDirectory(String accountName) {
Properties props = compileProperties();
List<LdapUserProxy> users = new ArrayList<LdapUserProxy>();
DirContext ctx = null ;
try {
ctx = new InitialDirContext(props);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attList = {"sAMAccountName", "sn", "givenName", "mail", "dn"};
constraints.setReturningAttributes(attList);
String filter = "sAMAccountName=" + accountName;
NamingEnumeration results = ctx.search(this.ldapPropertyContainer.get().getString(LdapModule.PROPERTY_LDAP_CTX_BASE), filter, constraints);
while (results.hasMoreElements()) {
SearchResult sr = (SearchResult) results.next();
LdapUserProxy u = new LdapUserProxy();
Attribute att = sr.getAttributes().get("sAMAccountName");
if(null != att)
u.setUsername(String.valueOf(att.get()));
att = sr.getAttributes().get("givenName");
if(null != att)
u.setFirstname(String.valueOf(att.get()));
att = sr.getAttributes().get("sn");
if(null != att)
u.setLastname(String.valueOf(att.get()));
att = sr.getAttributes().get("mail");
if(null != att)
u.setEmail(String.valueOf(att.get()));
att = sr.getAttributes().get("dn");
if(null != att)
u.setDistinguishedName(String.valueOf(att.get()));
users.add(u);
}
return users;
} catch (NamingException e) {
return users;
}finally{
if (null != ctx)
try {
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
}
Revise this Paste