Example usage for javax.naming Context SECURITY_PRINCIPAL

List of usage examples for javax.naming Context SECURITY_PRINCIPAL

Introduction

In this page you can find the example usage for javax.naming Context SECURITY_PRINCIPAL.

Prototype

String SECURITY_PRINCIPAL

To view the source code for javax.naming Context SECURITY_PRINCIPAL.

Click Source Link

Document

Constant that holds the name of the environment property for specifying the identity of the principal for authenticating the caller to the service.

Usage

From source file:BadPasswd.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");

    // Authenticate as S. User and give incorrect password
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "notmysecret");

    try {/*from www  . ja  v a  2  s . co  m*/
        // 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:UseDiff.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");

    // 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 {/*  w  ww  .ja va  2  s.c  o  m*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // do something useful with ctx
        System.out.println(ctx.lookup("ou=NewHires"));

        // Change to using no authentication
        ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "none");

        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:Digest.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");

    // Authenticate as C. User and password "mysecret"
    env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5");

    env.put(Context.SECURITY_PRINCIPAL, "dn:cn=C. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");

    env.put("com.sun.jndi.ldap.trace.ber", System.out);

    try {/*from www. j av a2 s . c  o  m*/
        // 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:DigestRealm.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");

    // Authenticate as C. User and password "mysecret" in realm "JNDITutorial"
    env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5");

    env.put(Context.SECURITY_PRINCIPAL, "dn:cn=C. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");
    env.put("java.naming.security.sasl.realm", "JNDITutorial");

    try {/* w  w w.j a  va2s.c o  m*/
        // 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:Ldaps.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");

    // Specify LDAPS URL
    env.put(Context.PROVIDER_URL, "ldaps://localhost:636/o=JNDITutorial");

    // 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 {/*from   w  ww. j ava  2 s.co m*/
        // 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: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  .  ja v  a2 s .  c  o  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:Ssl.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:636/o=JNDITutorial");

    // Specify SSL
    env.put(Context.SECURITY_PROTOCOL, "ssl");

    // 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 {/*from  w  w  w .  j  av  a 2s. co  m*/
        // 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: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  w  w .  ja  v  a  2 s .co 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:org.apache.ranger.ldapconfigcheck.LdapConfigCheckMain.java

public static void main(String[] args) {

    CommandLineOptions cli = new CommandLineOptions(args);
    cli.parse();/*w w w.  j a  va2 s.  c om*/
    String inFileName = cli.getInput();
    String outputDir = cli.getOutput();
    if (!outputDir.endsWith("/")) {
        outputDir = outputDir.concat("/");
    }

    LdapConfig config = new LdapConfig(inFileName, cli.getBindPassword());
    if (cli.getLdapUrl() != null && !cli.getLdapUrl().isEmpty()) {
        config.updateInputPropFile(cli.getLdapUrl(), cli.getBindDn(), cli.getBindPassword(),
                cli.getUserSearchBase(), cli.getUserSearchFilter(), cli.getAuthUser(), cli.getAuthPass());
    }

    PrintStream logFile = null;
    PrintStream ambariProps = null;
    PrintStream installProps = null;
    LdapContext ldapContext = null;

    try {
        logFile = new PrintStream(new File(outputDir + LOG_FILE));
        ambariProps = new PrintStream(new File(outputDir + AMBARI_PROPERTIES));
        installProps = new PrintStream(new File(outputDir + INSTALL_PROPERTIES));

        UserSync userSyncObj = new UserSync(config, logFile, ambariProps, installProps);

        String bindDn = config.getLdapBindDn();

        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, config.getLdapUrl());
        env.put(Context.SECURITY_PRINCIPAL, bindDn);
        env.put(Context.SECURITY_CREDENTIALS, cli.getBindPassword());
        env.put(Context.SECURITY_AUTHENTICATION, config.getLdapAuthenticationMechanism());
        env.put(Context.REFERRAL, "follow");

        ldapContext = new InitialLdapContext(env, null);

        if (config.isPagedResultsEnabled()) {
            ldapContext.setRequestControls(
                    new Control[] { new PagedResultsControl(config.getPagedResultsSize(), Control.CRITICAL) });
        }

        String retrieveValues = "all";

        if (cli.getDiscoverProperties() != null) {
            retrieveValues = cli.getDiscoverProperties();
            if (cli.getDiscoverProperties().equalsIgnoreCase("users")) {
                userSyncObj.findUserProperties(ldapContext);
            } else if (cli.getDiscoverProperties().equalsIgnoreCase("groups")) {
                userSyncObj.findGroupProperties(ldapContext);
            } else {
                findAllUserSyncProperties(ldapContext, userSyncObj);
            }
        } else if (cli.getRetrieveValues() != null) {
            retrieveValues = cli.getRetrieveValues();

        } else {
            cli.help();
        }

        if (cli.isAuthEnabled()) {
            authenticate(userSyncObj, config, logFile, ambariProps, installProps);
        }

        retrieveUsersGroups(ldapContext, userSyncObj, retrieveValues);

        if (ldapContext != null) {
            ldapContext.close();
        }

    } catch (FileNotFoundException fe) {
        System.out.println(fe.getMessage());
    } catch (IOException ioe) {
        logFile.println("ERROR: Failed while setting the paged results controls\n" + ioe);
    } catch (NamingException ne) {
        System.out.println("ERROR: Failed to perfom ldap bind. Please verify values for "
                + "ranger.usersync.ldap.binddn and ranger.usersync.ldap.ldapbindpassword\n" + ne);
    } catch (Throwable t) {
        if (logFile != null) {
            logFile.println("ERROR: Connection failed: " + t.getMessage());
        } else {
            System.out.println("ERROR: Connection failed: " + t.getMessage());
        }
    } finally {
        if (logFile != null) {
            logFile.close();
        }
        if (ambariProps != null) {
            ambariProps.close();
        }
        if (installProps != null) {
            installProps.close();
        }
        try {
            if (ldapContext != null) {
                ldapContext.close();
            }
        } catch (NamingException ne) {
            System.out.println("Failed to close LdapContext!");
        }
    }
}

From source file:se.vgregion.service.barium.BariumRestClientIT.java

License:asdf

public static void main(String[] args) {

    try {//from w w  w.  java2s. c om
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "LDAP://my.ldap.server:389"); //replace with your server URL/IP
        //only DIGEST-MD5 works with our Windows Active Directory
        env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5"); //No other SALS worked with me
        env.put(Context.SECURITY_PRINCIPAL, "user1"); // specify the username ONLY to let Microsoft Happy
        env.put(Context.SECURITY_CREDENTIALS, "secret1"); //the password

        DirContext ctx = new InitialDirContext(env);

        ctx.close();

    } catch (NamingException ne) {
        System.out.println("Error authenticating user:");
        System.out.println(ne.getMessage());
        return;
    }

    //if no exception, the user is already authenticated.
    System.out.println("OK, successfully authenticating user");
}