List of usage examples for javax.naming.directory Attribute get
Object get() throws NamingException;
From source file:LdapSearch.java
public static void main(String[] args) throws Exception { Hashtable env = new Hashtable(); String sp = "com.sun.jndi.ldap.LdapCtxFactory"; env.put(Context.INITIAL_CONTEXT_FACTORY, sp); String ldapUrl = "ldap://localhost:389/dc=yourName, dc=com"; env.put(Context.PROVIDER_URL, ldapUrl); DirContext dctx = new InitialDirContext(env); String base = "ou=People"; SearchControls sc = new SearchControls(); String[] attributeFilter = { "cn", "mail" }; sc.setReturningAttributes(attributeFilter); sc.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(&(sn=W*)(l=Criteria*))"; NamingEnumeration results = dctx.search(base, filter, sc); while (results.hasMore()) { SearchResult sr = (SearchResult) results.next(); Attributes attrs = sr.getAttributes(); Attribute attr = attrs.get("cn"); System.out.print(attr.get() + ": "); attr = attrs.get("mail"); System.out.println(attr.get()); }/*from ww w.j av a 2 s . c om*/ dctx.close(); }
From source file:pl.umk.mat.zawodyweb.ldap.LdapConnector.java
/** * Check user password and return that user * * Example of LDAP data://ww w . ja va 2s .c o m * <pre> * dn: uid=faramir,ou=People,ou=int,dc=mat,dc=uni,dc=torun,dc=pl * objectClass: top * objectClass: account * objectClass: posixAccount * objectClass: shadowAccount * objectClass: radiusprofile * objectClass: sambaSamAccount * dialupAccess: yes * uid: faramir * cn: Marek Nowicki * loginShell: /bin/tcsh * uidNumber: 30030 * sambaSID: S-1-30030 * gecos: Marek Nowicki, doktorant Info. * gidNumber: 160 * homeDirectory: /studdok/faramir * radiusSimultaneousUse: 1</pre> * @param login login * @param pass user password * @return Users if user found and password is OK or null if anything failed */ public static Users retieveUser(String login, String pass) { if (pass == null || pass.isEmpty() || login == null || login.isEmpty() || login.contains(",")) { return null; } Hashtable<String, String> ldapEnv = new Hashtable<String, String>(11); String dn = String.format("uid=%s,%s", login, baseDN); ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); ldapEnv.put(Context.PROVIDER_URL, ldapURL); ldapEnv.put(Context.SECURITY_PRINCIPAL, dn); ldapEnv.put(Context.SECURITY_CREDENTIALS, pass); try { DirContext authContext = new InitialDirContext(ldapEnv); Attributes userAttributes = authContext.getAttributes(dn); if (userAttributes.get("uidNumber") == null) { return null; } Attribute cn = userAttributes.get("cn"); // commonName - eg. Marek Nowicki String name = ((String) cn.get()); String firstName = name; String lastName = "(LDAP)"; int index = name.lastIndexOf(" "); if (index > 0) { firstName = name.substring(0, index).trim(); lastName = name.substring(index + 1).trim(); } Users user = new Users(); user.setLogin(login); user.setFirstname(firstName); user.setLastname(lastName); user.setEmail(login + emailSuffix); return user; } catch (AuthenticationException ex) { } catch (NamingException ex) { } catch (NullPointerException ex) { } catch (ClassCastException ex) { } catch (Exception ex) { log.fatal("LDAP Exception:", ex); } return null; }
From source file:org.sonar.plugins.ldap.LdapGroupMapping.java
private static String getAttributeValue(SearchResult user, String attributeId) { Attribute attribute = user.getAttributes().get(attributeId); if (attribute == null) { return null; }//from w w w . java2 s . c om try { return (String) attribute.get(); } catch (NamingException e) { throw new IllegalArgumentException(e); } }
From source file:security.AuthenticationManager.java
public static Map<String, String> getUserAttributes(DirContext ctx, String searchBase, String userName, String principalDomain, String... attributeNames) throws NamingException { if (StringUtils.isBlank(userName)) { throw new IllegalArgumentException("Username and password can not be blank."); }/*from w w w.j a v a 2 s.c o m*/ if (attributeNames.length == 0) { return Collections.emptyMap(); } Attributes matchAttr = new BasicAttributes(true); BasicAttribute basicAttr = new BasicAttribute("userPrincipalName", userName + principalDomain); matchAttr.put(basicAttr); NamingEnumeration<? extends SearchResult> searchResult = ctx.search(searchBase, matchAttr, attributeNames); if (ctx != null) { ctx.close(); } Map<String, String> result = new HashMap<>(); if (searchResult.hasMore()) { NamingEnumeration<? extends Attribute> attributes = searchResult.next().getAttributes().getAll(); while (attributes.hasMore()) { Attribute attr = attributes.next(); String attrId = attr.getID(); String attrValue = (String) attr.get(); result.put(attrId, attrValue); } } return result; }
From source file:org.projectforge.business.ldap.LdapUtils.java
public static Object getAttributeValue(final Attributes attributes, final String attrId) throws NamingException { final Attribute attr = attributes.get(attrId); if (attr == null) { return null; }//ww w.ja v a 2s . c o m return attr.get(); }
From source file:org.projectforge.business.ldap.LdapUtils.java
public static String getAttributeStringValue(final Attributes attributes, final String attrId) throws NamingException { final Attribute attr = attributes.get(attrId); if (attr == null) { return null; }/*from www.j a va2 s .co m*/ return (String) attr.get(); }
From source file:org.trypticon.xmpp.util.SrvLookup.java
/** * Resolves a domain name to a host and port, by looking up the SRV record. * * @param domain the domain to look up. * @param service the service to look up. * @param protocol the protocol to look up. * @return the address, or <code>null</code> if it couldn't be resolved. *///from ww w . jav a 2s. c om private static InetSocketAddress resolveSRV(String domain, String service, String protocol) { if (context == null) { return null; } try { Attributes dnsLookup = context.getAttributes("_" + service + "._" + protocol + "." + domain); Attribute srvAttribute = dnsLookup.get("SRV"); // The attribute is null if there was no record in DNS. if (srvAttribute == null) { return null; } String srvRecord = (String) srvAttribute.get(); // The attribute value is null if there was somehow a record with a null value. if (srvRecord == null) { return null; } String[] srvRecordEntries = srvRecord.split(" "); String host = srvRecordEntries[srvRecordEntries.length - 1]; int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]); return new InetSocketAddress(host, port); } catch (NamingException e) { log.warn("Problem looking up SRV record for domain " + domain + ", service " + service + ", protocol " + protocol, e); return null; } }
From source file:org.projectforge.business.ldap.LdapUtils.java
public static Integer getAttributeIntegerValue(final Attributes attributes, final String attrId) throws NamingException { final Attribute attr = attributes.get(attrId); if (attr == null) { return null; }/*from w ww . j ava 2 s . c om*/ return (Integer) attr.get(); }
From source file:org.apache.roller.weblogger.ui.core.security.CustomUserRegistry.java
private static String getLdapAttribute(Attributes attributes, String name) { if (attributes == null) { return null; }//from w w w. j a va 2s . com Attribute attribute = attributes.get(name); if (attribute == null) { return null; } Object oValue = null; try { oValue = attribute.get(); } catch (NamingException e) { return null; } if (oValue == null) { return null; } return oValue.toString(); }
From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java
public static String getObjectSID(Attributes attrs) throws NamingException { Attribute attr = attrs.get("objectsid"); if (attr == null) throw new NamingException("No ObjectSID attribute"); Object attrObject = attr.get(); if (attrObject == null) throw new NamingException("ObjectSID is empty"); if (attrObject instanceof String) { String attrString = (String) attrObject; if (attrString.startsWith("S-")) return attrString; return decodeSID(attrString.getBytes()); } else if (attrObject instanceof byte[]) { return decodeSID((byte[]) attrObject); } else/* ww w .j av a 2 s . co m*/ throw new NamingException("Unknown attribute type: " + attrObject.getClass().getName()); }