List of usage examples for javax.naming.directory DirContext lookup
public Object lookup(Name name) throws NamingException;
From source file:NewConn.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {//from w w w . jav a 2 s .co m // Create initial context (first connection) DirContext ctx = new InitialDirContext(env); // Get a copy of the same context DirContext ctx2 = (DirContext) ctx.lookup(""); // Change authentication properties in ctx2 ctx2.addToEnvironment(Context.SECURITY_PRINCIPAL, "cn=C. User, ou=NewHires, o=JNDITutorial"); ctx2.addToEnvironment(Context.SECURITY_CREDENTIALS, "mysecret"); // Method on ctx2 will use new connection System.out.println(ctx2.getAttributes("ou=NewHires")); // Close the contexts when we're done ctx.close(); ctx2.close(); } catch (NamingException e) { e.printStackTrace(); } }
From source file:UseFactory.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env//w ww . j ava 2 s. c o m .put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); // Specify the socket factory env.put("java.naming.ldap.factory.socket", "CustomSocketFactory"); // Authenticate as S. User and password "mysecret" env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial"); env.put(Context.SECURITY_CREDENTIALS, "mysecret"); try { // Create initial context DirContext ctx = new InitialDirContext(env); System.out.println(ctx.lookup("ou=NewHires")); // ... do something useful with ctx // Close the context when we're done ctx.close(); } catch (NamingException e) { e.printStackTrace(); } }
From source file:RenameInterior.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {/*from w w w . j a va 2 s.c o m*/ // Create initial context DirContext ctx = new InitialDirContext(env); // Perform rename ctx.rename("ou=NewHires", "ou=OldHires"); // Check that it worked System.out.println(ctx.lookup("ou=OldHires")); // Revert change ctx.rename("ou=OldHires", "ou=NewHires"); // Check that we are back at our original setup System.out.println(ctx.lookup("ou=NewHIres")); // Close the context when we're done ctx.close(); } catch (NamingException e) { e.printStackTrace(); } }
From source file:RenameDiffParent.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {//w w w .j a va 2 s. c om // Create initial context DirContext ctx = new InitialDirContext(env); // Perform rename ctx.rename("cn=C. User, ou=NewHires", "cn=C. User, ou=People"); // Check that it worked System.out.println(ctx.lookup("cn=C. User, ou=People")); // Revert change ctx.rename("cn=C. User, ou=People", "cn=C. User, ou=NewHires"); // Check that we are back at our original setup System.out.println(ctx.lookup("cn=C. User, ou=NewHires")); // Close the context when we're done ctx.close(); } catch (NamingException e) { e.printStackTrace(); } }
From source file:SerObjWithCodebase.java
public static void main(String[] args) { if (args.length != 1) { System.err.println("usage: java SerObjWithCodebase <codebase URL>"); System.exit(-1);// www . j a v a2s .com } String codebase = args[0]; // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try { // Create the initial context DirContext ctx = new InitialDirContext(env); // Create object to be bound Flower f = new Flower("rose", "pink"); // Perform bind and specify codebase ctx.bind("cn=Flower", f, new BasicAttributes("javaCodebase", codebase)); // Check that it is bound Flower f2 = (Flower) ctx.lookup("cn=Flower"); System.out.println(f2); // Close the context when we're done ctx.close(); } catch (NamingException e) { System.out.println("Operation failed: " + e); } }
From source file:LDAPTest.java
/** * Gets a context from the properties specified in the file ldapserver.properties * @return the directory context//from www . j av a2 s . c om */ public static DirContext getContext() throws NamingException, IOException { Properties props = new Properties(); FileInputStream in = new FileInputStream("ldapserver.properties"); props.load(in); in.close(); String url = props.getProperty("ldap.url"); String username = props.getProperty("ldap.username"); String password = props.getProperty("ldap.password"); Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.SECURITY_PRINCIPAL, username); env.put(Context.SECURITY_CREDENTIALS, password); DirContext initial = new InitialDirContext(env); DirContext context = (DirContext) initial.lookup(url); return context; }
From source file:org.archone.ad.dao.GroupDao.java
public GroupDao(DirContext dirContext, GroupDn groupDn) throws NamingException { this.dirContext = dirContext; this.group = (DirContextAdapter) dirContext.lookup(groupDn); }
From source file:net.officefloor.plugin.jndi.ldap.CredentialStoreTest.java
/** * Ensure able to obtain the roles.//from ww w.j a v a 2s .c o m */ public void testObtainRoles() throws Exception { // Obtain the context DirContext context = this.ldap.getDirContext(); // Obtain the People context DirContext people = (DirContext) context.lookup("ou=People,dc=officefloor,dc=net"); assertNotNull("Should have People context", people); // Search for person NamingEnumeration<SearchResult> personResults = people.search("", "(&(objectClass=inetOrgPerson)(uid=daniel))", null); assertTrue("Expecting to find daniel entry", personResults.hasMore()); SearchResult daniel = personResults.next(); assertFalse("Should only have the daniel entry", personResults.hasMore()); // Obtain the Groups context DirContext groups = (DirContext) context.lookup("ou=Groups,dc=officefloor,dc=net"); assertNotNull("Should have Groups context", groups); // Search for groups containing daniel String danielDn = daniel.getNameInNamespace(); NamingEnumeration<SearchResult> groupResults = groups.search("", "(&(objectClass=groupOfNames)(member=" + danielDn + "))", null); // Obtain the listing of roles for daniel List<String> roles = new ArrayList<String>(2); for (; groupResults.hasMore();) { SearchResult group = groupResults.next(); // Obtain the role from the group String role = (String) group.getAttributes().get("ou").get(); // Add role to listing roles.add(role); } // Ensure the correct roles assertEquals("Incorrect number of roles", 2, roles.size()); assertTrue("Missing user role", roles.contains("developer")); assertTrue("Missing developer role", roles.contains("committer")); }
From source file:net.officefloor.plugin.jndi.ldap.CredentialStoreTest.java
/** * Ensure able to obtain credentials.//from w w w . j a va 2 s.co m */ public void testObtainCredentials() throws Exception { final Charset ASCII = Charset.forName("ASCII"); // Calculate the expected credential String expectedRaw = "daniel:officefloor:password"; MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(expectedRaw.getBytes(ASCII)); byte[] expectedBytes = digest.digest(); String expectedCredentials = Base64.encodeBase64String(expectedBytes).trim(); // Obtain the context DirContext context = this.ldap.getDirContext(); // Obtain the People context DirContext people = (DirContext) context.lookup("ou=People,dc=officefloor,dc=net"); assertNotNull("Should have People context", people); // Search for person NamingEnumeration<SearchResult> results = people.search("", "(&(objectClass=inetOrgPerson)(uid=daniel))", null); assertTrue("Expecting to find daniel entry", results.hasMore()); SearchResult result = results.next(); assertFalse("Should only have the daniel entry", results.hasMore()); // Obtain the digest MD5 credentials for Daniel String digestMd5Credential = null; Attributes attributes = result.getAttributes(); Attribute passwordAttribute = attributes.get("userPassword"); for (NamingEnumeration<?> enumeration = passwordAttribute.getAll(); enumeration.hasMore();) { byte[] credentials = (byte[]) enumeration.next(); String text = new String(credentials, ASCII); // Determine if MD5 credential if (text.toUpperCase().startsWith("{MD5}")) { // Found MD5 credential digestMd5Credential = text.substring("{MD5}".length()); } } assertNotNull("Must have digest MD5 credential", digestMd5Credential); // Ensure correct credentials assertEquals("Incorrect DIGEST MD5 credentials", expectedCredentials, digestMd5Credential); }
From source file:com.googlecode.fascinator.authentication.custom.ldap.CustomLdapAuthenticationHandler.java
/** * Attempts to authenticate user credentials with the LDAP server * // w w w . j a v a2s .c om * @param username * a username * @param password * a password * @param dn * if precise dn known, otherwise should be empty string * @return <code>true</code> if authentication was successful, * <code>false</code> otherwise */ private boolean bind(String username, String password) { try { String principal = String.format("%s=%s,%s", idAttr, username, baseDn); env.put(Context.SECURITY_PRINCIPAL, principal); env.put(Context.SECURITY_CREDENTIALS, password); DirContext ctx = new InitialDirContext(env); ctx.lookup(principal); ctx.close(); return true; } catch (NamingException ne) { log.warn("Failed LDAP lookup doAuthenticate", ne); } return false; }