1 17 package org.alfresco.repo.security.authentication.ldap; 18 19 import java.util.Collections ; 20 import java.util.Hashtable ; 21 import java.util.Map ; 22 23 import javax.naming.Context ; 24 import javax.naming.NamingException ; 25 import javax.naming.directory.Attribute ; 26 import javax.naming.directory.Attributes ; 27 import javax.naming.directory.BasicAttribute ; 28 import javax.naming.directory.BasicAttributes ; 29 import javax.naming.directory.InitialDirContext ; 30 31 import org.alfresco.repo.security.authentication.AuthenticationException; 32 import org.alfresco.util.ApplicationContextHelper; 33 import org.springframework.context.ApplicationContext; 34 35 public class LDAPInitialDirContextFactoryImpl implements LDAPInitialDirContextFactory 36 { 37 private Map <String , String > initialDirContextEnvironment = Collections.<String , String > emptyMap(); 38 39 static 40 { 41 System.setProperty("javax.security.auth.useSubjectCredentialsOnly", "false"); 42 } 43 44 public LDAPInitialDirContextFactoryImpl() 45 { 46 super(); 47 } 48 49 public void setInitialDirContextEnvironment(Map <String , String > initialDirContextEnvironment) 50 51 { 52 this.initialDirContextEnvironment = initialDirContextEnvironment; 53 } 54 55 public Map <String , String > getInitialDirContextEnvironment() 56 { 57 return initialDirContextEnvironment; 58 } 59 60 public InitialDirContext getDefaultIntialDirContext() throws AuthenticationException 61 { 62 Hashtable <String , String > env = new Hashtable <String , String >(initialDirContextEnvironment.size()); 63 env.putAll(initialDirContextEnvironment); 64 env.put("javax.security.auth.useSubjectCredsOnly", "false"); 65 return buildInitialDirContext(env); 66 } 67 68 private InitialDirContext buildInitialDirContext(Hashtable <String , String > env) throws AuthenticationException 69 { 70 try 71 { 72 return new InitialDirContext (env); 73 } 74 catch (javax.naming.AuthenticationException ax) 75 { 76 throw new AuthenticationException("LDAP authentication failed.", ax); 77 } 78 catch (NamingException nx) 79 { 80 throw new AuthenticationException("Unable to connect to LDAP Server; check LDAP configuration", nx); 81 } 82 } 83 84 public InitialDirContext getInitialDirContext(String principal, String credentials) throws AuthenticationException 85 { 86 if (principal == null) 87 { 88 throw new AuthenticationException("Null user name provided."); 89 } 90 91 if (credentials == null) 92 { 93 throw new AuthenticationException("No credentials provided."); 94 } 95 Hashtable <String , String > env = new Hashtable <String , String >(initialDirContextEnvironment.size()); 96 env.putAll(initialDirContextEnvironment); 97 env.put(Context.SECURITY_PRINCIPAL, principal); 98 env.put(Context.SECURITY_CREDENTIALS, credentials); 99 100 return buildInitialDirContext(env); 101 } 102 103 public static void main(String [] args) 104 { 105 107 110 int userMembers = Integer.parseInt(args[3]); 111 112 ApplicationContext applicationContext = ApplicationContextHelper.getApplicationContext(); 113 LDAPInitialDirContextFactory factory = (LDAPInitialDirContextFactory) applicationContext 114 .getBean("ldapInitialDirContextFactory"); 115 116 InitialDirContext ctx = null; 117 try 118 { 119 ctx = factory.getInitialDirContext("cn=" + args[0] + "," + args[2], args[1]); 120 121 122 Attribute objClasses = new BasicAttribute ("objectclass"); 123 objClasses.add("top"); 124 objClasses.add("person"); 125 objClasses.add("organizationalPerson"); 126 objClasses.add("inetOrgPerson"); 127 128 for (int i = 0; i < userMembers; i++) 129 { 130 131 Attribute cn = new BasicAttribute ("cn", "User" + i + " TestUser"); 132 Attribute sn = new BasicAttribute ("sn", "TestUser"); 133 Attribute givenNames = new BasicAttribute ("givenName", "User" + i); 134 Attribute telephoneNumber = new BasicAttribute ("telephoneNumber", "123"); 135 Attribute uid = new BasicAttribute ("uid", "User" + i); 136 Attribute mail = new BasicAttribute ("mail", "woof@woof"); 137 Attribute o = new BasicAttribute ("o", "Alfresco"); 138 Attribute userPassword = new BasicAttribute ("userPassword", "bobbins"); 139 140 String dn = "cn=User" + i + " TestUser," + args[2]; 141 142 Attributes orig = new BasicAttributes (); 143 orig.put(objClasses); 144 orig.put(cn); 145 orig.put(sn); 146 orig.put(givenNames); 147 orig.put(telephoneNumber); 148 orig.put(uid); 149 orig.put(mail); 150 orig.put(o); 151 orig.put(userPassword); 152 153 try 154 { 155 ctx.destroySubcontext(dn); 156 } 157 catch (NamingException e) 158 { 159 e.printStackTrace(); 161 } 162 163 ctx.createSubcontext(dn, orig); 164 } 165 166 } 167 catch (NamingException e) 168 { 169 e.printStackTrace(); 171 } 172 finally 173 { 174 if (ctx != null) 175 { 176 try 177 { 178 ctx.close(); 179 } 180 catch (NamingException e) 181 { 182 183 e.printStackTrace(); 184 } 185 } 186 } 187 188 } 189 190 } 191 | Popular Tags |