import javax.naming.*;
import javax.naming.directory.*;
import java.security.Security;
import java.util.Hashtable;
import java.util.ResourceBundle;
/**
* Shows contexts that share the same connection.
*
* usage: java Shared
*/
class JNDI_DirectoryAdd {
//fichier properties de connection Base
static ResourceBundle rb = ResourceBundle.getBundle("connectAnnuaireLdap");
public static void main(String[] args)
{
//---
//SSL
// Dynamically set JSSE as a security provider
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// Dynamically set the property that JSSE uses to identify
// the keystore that holds trusted root certificates
String certifPath=System.getProperty("user.dir")+"\\certificats\\"+rb.getString("certificat").trim();
System.setProperty("javax.net.ssl.trustStore", certifPath);
// Set up environment for creating initial context
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldaps://*******:636");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=****,dc=***,dc=fr");
env.put(Context.SECURITY_CREDENTIALS, "****");
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.REFERRAL,"follow");
try {
//Create initial context
DirContext ctx = new InitialDirContext(env);
//AddDn
//String parentDn="o=****,o=individus,dc=***,dc=fr";
String parentDn="o=****,dc=***,dc=fr";
//The objectclass
Attribute objclass = new BasicAttribute("objectclass");
//objclass.add("myPerson");
objclass.add("inetOrgPerson");
objclass.add("person");
objclass.add("top");
// Attribut
Attribute userUid=new BasicAttribute("uid","AlphaBob");
Attribute userCnName=new BasicAttribute("cn","Bob");
Attribute userSnName=new BasicAttribute("sn","Boby");
Attribute userFirstName=new BasicAttribute("givenname","Boby");
// Create attributes to be associated with the new context
Attributes attrs = new BasicAttributes(true); // case-ignore
attrs.put(objclass);
attrs.put(userUid);
attrs.put(userCnName);
attrs.put(userSnName);
attrs.put(userFirstName);
// Create the context
Context result = ctx.createSubcontext("uid=AlphaBob,"+parentDn, attrs);
// Close the contexts when we're done
result.close();
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
Partager