Ejemplo n.º 1
0
  /** Constructor for LDAPQuery. */
  public JNDITomcat() throws Exception {
    super();

    Hashtable<String, String> env = new Hashtable<String, String>();

    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    env.put(Context.PROVIDER_URL, "jndi://nadetrou2:8080/");

    // Obtain our environment naming context

    Context initCtx = new InitialContext(env);

    // Context envCtx = (Context) initCtx.lookup("comp/env");

    // Look up our data source

    // DataSource datasource = (DataSource)
    // initialContext.lookup((String)settings.get(CONNECTION_POOL_NAME));

    DataSource ds = (DataSource) initCtx.lookup("/jdbc/vrdb");

    // Allocate and use a connection from the pool

    Connection conn = ds.getConnection();

    System.out.println(conn);
    // ... use this connection to access the database ...

    conn.close();

    // Hashtable env = new Hashtable();
    // //env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    // //env.put(Context.PROVIDER_URL, "t3://192.168.0.55:17001/" );
    //
    // env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    // env.put(Context.PROVIDER_URL, "3://127.0.0.1:7001/" );
    //
    // // env.put(Context.SECURITY_PRINCIPAL, "system");
    // // env.put(Context.SECURITY_CREDENTIALS, "12345678");
    // System.out.println(env);
    // //System.out.println("--A");
    // // Create initial context
    // DirContext ctx = new InitialDirContext(env);

    // exploreNext(0, envCtx, "");
  }
Ejemplo n.º 2
0
  /**
   * Validate User
   *
   * @param ldapURL provider url - e.g. ldap://dc.compiere.org
   * @param domain domain name = e.g. compiere.org
   * @param userName user name - e.g. jjanke
   * @param password password
   * @return true if validated with ldap
   */
  public static boolean validate(String ldapURL, String domain, String userName, String password) {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    //	ldap://dc.compiere.org
    env.put(Context.PROVIDER_URL, ldapURL);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    //	[email protected]
    StringBuffer principal = new StringBuffer(userName).append("@").append(domain);
    env.put(Context.SECURITY_PRINCIPAL, principal.toString());
    env.put(Context.SECURITY_CREDENTIALS, password);
    //
    try {
      // Create the initial context
      InitialLdapContext ctx = new InitialLdapContext(env, null);
      //	DirContext ctx = new InitialDirContext(env);

      //	Test - Get the attributes
      Attributes answer = ctx.getAttributes("");

      // Print the answer
      if (false) dump(answer);
    } catch (AuthenticationException e) {
      log.info("Error: " + principal + " - " + e.getLocalizedMessage());
      return false;
    } catch (Exception e) {
      log.log(Level.SEVERE, ldapURL + " - " + principal, e);
      return false;
    }
    log.info("OK: " + principal);
    return true;
  } //	validate