Java Code Examples for javax.naming.directory.DirContext#addToEnvironment()

The following examples show how to use javax.naming.directory.DirContext#addToEnvironment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JNDIRealm.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Configure the context to use {@link #connectionName} and
 * {@link #connectionPassword} if specified or an anonymous connection if
 * those attributes are not specified.
 *
  * @param context      DirContext to configure
  * @exception NamingException if a directory server error occurs
 */
private void userCredentialsRemove(DirContext context)
        throws NamingException {
    // Restore the original security environment
    if (connectionName != null) {
        context.addToEnvironment(Context.SECURITY_PRINCIPAL,
                                 connectionName);
    } else {
        context.removeFromEnvironment(Context.SECURITY_PRINCIPAL);
    }

    if (connectionPassword != null) {
        context.addToEnvironment(Context.SECURITY_CREDENTIALS,
                                 connectionPassword);
    }
    else {
        context.removeFromEnvironment(Context.SECURITY_CREDENTIALS);
    }
}
 
Example 2
Source File: JNDIRealm.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Configure the context to use {@link #connectionName} and
 * {@link #connectionPassword} if specified or an anonymous connection if
 * those attributes are not specified.
 * 
  * @param context      DirContext to configure
 */
private void userCredentialsRemove(DirContext context)
        throws NamingException {
    // Restore the original security environment
    if (connectionName != null) {
        context.addToEnvironment(Context.SECURITY_PRINCIPAL,
                                 connectionName);
    } else {
        context.removeFromEnvironment(Context.SECURITY_PRINCIPAL);
    }

    if (connectionPassword != null) {
        context.addToEnvironment(Context.SECURITY_CREDENTIALS,
                                 connectionPassword);
    }
    else {
        context.removeFromEnvironment(Context.SECURITY_CREDENTIALS);
    }
}
 
Example 3
Source File: JNDIRealm.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Configure the context to use {@link #connectionName} and
 * {@link #connectionPassword} if specified or an anonymous connection if
 * those attributes are not specified.
 * 
  * @param context      DirContext to configure
 */
private void userCredentialsRemove(DirContext context)
        throws NamingException {
    // Restore the original security environment
    if (connectionName != null) {
        context.addToEnvironment(Context.SECURITY_PRINCIPAL,
                                 connectionName);
    } else {
        context.removeFromEnvironment(Context.SECURITY_PRINCIPAL);
    }

    if (connectionPassword != null) {
        context.addToEnvironment(Context.SECURITY_CREDENTIALS,
                                 connectionPassword);
    }
    else {
        context.removeFromEnvironment(Context.SECURITY_CREDENTIALS);
    }
}
 
Example 4
Source File: JNDIRealm.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void restoreEnvironmentParameter(DirContext context,
        String parameterName, Hashtable<?, ?> preservedEnvironment) {
    try {
        context.removeFromEnvironment(parameterName);
        if (preservedEnvironment != null && preservedEnvironment.containsKey(parameterName)) {
            context.addToEnvironment(parameterName,
                    preservedEnvironment.get(parameterName));
        }
    } catch (NamingException e) {
        // Ignore
    }
}
 
Example 5
Source File: JNDIRealm.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void restoreEnvironmentParameter(DirContext context,
        String parameterName, Hashtable<?, ?> preservedEnvironment) {
    try {
        context.removeFromEnvironment(parameterName);
        if (preservedEnvironment != null && preservedEnvironment.containsKey(parameterName)) {
            context.addToEnvironment(parameterName,
                    preservedEnvironment.get(parameterName));
        }
    } catch (NamingException e) {
        // Ignore
    }
}
 
Example 6
Source File: JNDIRealm.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void restoreEnvironmentParameter(DirContext context,
        String parameterName, Hashtable<?, ?> preservedEnvironment) {
    try {
        context.removeFromEnvironment(parameterName);
        if (preservedEnvironment != null && preservedEnvironment.containsKey(parameterName)) {
            context.addToEnvironment(parameterName,
                    preservedEnvironment.get(parameterName));
        }
    } catch (NamingException e) {
        // Ignore
    }
}
 
Example 7
Source File: LDAPLoginModule.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected boolean bindUser(DirContext context, String dn, String password) throws NamingException {
   boolean isValid = false;

   if (logger.isDebugEnabled()) {
      logger.debug("Binding the user.");
   }
   context.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
   context.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
   try {
      context.getAttributes("", null);
      isValid = true;
      if (logger.isDebugEnabled()) {
         logger.debug("User " + dn + " successfully bound.");
      }
   } catch (AuthenticationException e) {
      isValid = false;
      if (logger.isDebugEnabled()) {
         logger.debug("Authentication failed for dn=" + dn);
      }
   }

   if (isLoginPropertySet(CONNECTION_USERNAME)) {
      context.addToEnvironment(Context.SECURITY_PRINCIPAL, getLDAPPropertyValue(CONNECTION_USERNAME));
   } else {
      context.removeFromEnvironment(Context.SECURITY_PRINCIPAL);
   }
   if (isLoginPropertySet(CONNECTION_PASSWORD)) {
      context.addToEnvironment(Context.SECURITY_CREDENTIALS, getPlainPassword(getLDAPPropertyValue(CONNECTION_PASSWORD)));
   } else {
      context.removeFromEnvironment(Context.SECURITY_CREDENTIALS);
   }

   return isValid;
}
 
Example 8
Source File: LdifScript.java    From scriptella-etl with Apache License 2.0 5 votes vote down vote up
/**
 * Adds/modifies ctx using entry information.
 *
 * @param ctx directory context to use for change.
 * @param e   entry with change description.
 * @throws NamingException if operation with directory failed.
 */
static void modify(DirContext ctx, final Entry e) throws NamingException {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Processing " + e);
    }
    Attributes atts = e.getAttributes();
    final String rootDn = ctx.getNameInNamespace();
    if (atts != null) { //If add entry
        ctx.createSubcontext(getRelativeDN(rootDn, e.getDn()), e.getAttributes());
    } else if (e.isChangeDelete()) {
        ctx.destroySubcontext(getRelativeDN(rootDn, e.getDn()));
    } else if (e.isChangeModDn() || e.isChangeModRdn()) {
        Name newRdn;
        if (e.getNewSuperior() != null) { //If new superior
            newRdn = getRelativeDN(rootDn, e.getNewSuperior());
        } else { //otherwise use DN as a base
            newRdn = getRelativeDN(rootDn, e.getDn());
            newRdn.remove(newRdn.size() - 1);
        }
        newRdn.add(e.getNewRdn());
        ctx.addToEnvironment("java.naming.ldap.deleteRDN", String.valueOf(e.isDeleteOldRdn()));
        ctx.rename(getRelativeDN(rootDn, e.getDn()), newRdn);
        ctx.removeFromEnvironment("java.naming.ldap.deleteRDN");//a better solution to use the previous value

    } else {
        List<ModificationItem> items = e.getModificationItems();
        ctx.modifyAttributes(getRelativeDN(rootDn, e.getDn()),
                items.toArray(new ModificationItem[items.size()]));
    }
}
 
Example 9
Source File: JNDIRealm.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Get the principal associated with the specified certificate.
 * @param context The directory context
 * @param username The user name
 * @param gssCredential The credentials
 * @return the Principal associated with the given certificate.
 * @exception NamingException if a directory server error occurs
 */
protected synchronized Principal getPrincipal(DirContext context,
        String username, GSSCredential gssCredential)
    throws NamingException {

    User user = null;
    List<String> roles = null;
    Hashtable<?, ?> preservedEnvironment = null;

    try {
        if (gssCredential != null && isUseDelegatedCredential()) {
            // Preserve the current context environment parameters
            preservedEnvironment = context.getEnvironment();
            // Set up context
            context.addToEnvironment(
                    Context.SECURITY_AUTHENTICATION, "GSSAPI");
            context.addToEnvironment(
                    "javax.security.sasl.server.authentication", "true");
            context.addToEnvironment(
                    "javax.security.sasl.qop", spnegoDelegationQop);
            // Note: Subject already set in SPNEGO authenticator so no need
            //       for Subject.doAs() here
        }
        user = getUser(context, username);
        if (user != null) {
            roles = getRoles(context, user);
        }
    } finally {
        restoreEnvironmentParameter(context,
                Context.SECURITY_AUTHENTICATION, preservedEnvironment);
        restoreEnvironmentParameter(context,
                "javax.security.sasl.server.authentication", preservedEnvironment);
        restoreEnvironmentParameter(context, "javax.security.sasl.qop",
                preservedEnvironment);
    }

    if (user != null) {
        return new GenericPrincipal(user.getUserName(), user.getPassword(),
                roles, null, null, gssCredential);
    }

    return null;
}
 
Example 10
Source File: JNDIRealm.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Return the Principal associated with the given user name.
 */
protected synchronized Principal getPrincipal(DirContext context,
        String username, GSSCredential gssCredential)
    throws NamingException {

    User user = null;
    List<String> roles = null;
    Hashtable<?, ?> preservedEnvironment = null;

    try {
        if (gssCredential != null && isUseDelegatedCredential()) {
            // Preserve the current context environment parameters
            preservedEnvironment = context.getEnvironment();
            // Set up context
            context.addToEnvironment(
                    Context.SECURITY_AUTHENTICATION, "GSSAPI");
            context.addToEnvironment(
                    "javax.security.sasl.server.authentication", "true");
            context.addToEnvironment(
                    "javax.security.sasl.qop", spnegoDelegationQop);
            // Note: Subject already set in SPNEGO authenticator so no need
            //       for Subject.doAs() here
        }
        user = getUser(context, username);
        if (user != null) {
            roles = getRoles(context, user);
        }
    } finally {
        restoreEnvironmentParameter(context,
                Context.SECURITY_AUTHENTICATION, preservedEnvironment);
        restoreEnvironmentParameter(context,
                "javax.security.sasl.server.authentication", preservedEnvironment);
        restoreEnvironmentParameter(context, "javax.security.sasl.qop",
                preservedEnvironment);
    }

    if (user != null) {
        return new GenericPrincipal(user.getUserName(), user.getPassword(),
                roles, null, null, gssCredential);
    }
    
    return null;
}
 
Example 11
Source File: JNDIRealm.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Return the Principal associated with the given user name.
 */
protected synchronized Principal getPrincipal(DirContext context,
        String username, GSSCredential gssCredential)
    throws NamingException {

    User user = null;
    List<String> roles = null;
    Hashtable<?, ?> preservedEnvironment = null;

    try {
        if (gssCredential != null && isUseDelegatedCredential()) {
            // Preserve the current context environment parameters
            preservedEnvironment = context.getEnvironment();
            // Set up context
            context.addToEnvironment(
                    Context.SECURITY_AUTHENTICATION, "GSSAPI");
            context.addToEnvironment(
                    "javax.security.sasl.server.authentication", "true");
            context.addToEnvironment(
                    "javax.security.sasl.qop", spnegoDelegationQop);
            // Note: Subject already set in SPNEGO authenticator so no need
            //       for Subject.doAs() here
        }
        user = getUser(context, username);
        if (user != null) {
            roles = getRoles(context, user);
        }
    } finally {
        restoreEnvironmentParameter(context,
                Context.SECURITY_AUTHENTICATION, preservedEnvironment);
        restoreEnvironmentParameter(context,
                "javax.security.sasl.server.authentication", preservedEnvironment);
        restoreEnvironmentParameter(context, "javax.security.sasl.qop",
                preservedEnvironment);
    }

    if (user != null) {
        return new GenericPrincipal(user.getUserName(), user.getPassword(),
                roles, null, null, gssCredential);
    }
    
    return null;
}
 
Example 12
Source File: JNDIRealm.java    From Tomcat8-Source-Read with MIT License 3 votes vote down vote up
/**
  * Configure the context to use the provided credentials for
  * authentication.
  *
  * @param context      DirContext to configure
  * @param dn           Distinguished name of user
  * @param credentials  Credentials of user
  * @exception NamingException if a directory server error occurs
  */
private void userCredentialsAdd(DirContext context, String dn,
        String credentials) throws NamingException {
    // Set up security environment to bind as the user
    context.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
    context.addToEnvironment(Context.SECURITY_CREDENTIALS, credentials);
}
 
Example 13
Source File: JNDIRealm.java    From Tomcat7.0.67 with Apache License 2.0 3 votes vote down vote up
/**
  * Configure the context to use the provided credentials for
  * authentication.
  *
  * @param context      DirContext to configure
  * @param dn           Distinguished name of user
  * @param credentials  Credentials of user
  */
private void userCredentialsAdd(DirContext context, String dn,
        String credentials) throws NamingException {
    // Set up security environment to bind as the user
    context.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
    context.addToEnvironment(Context.SECURITY_CREDENTIALS, credentials);
}
 
Example 14
Source File: JNDIRealm.java    From tomcatsrc with Apache License 2.0 3 votes vote down vote up
/**
  * Configure the context to use the provided credentials for
  * authentication.
  *
  * @param context      DirContext to configure
  * @param dn           Distinguished name of user
  * @param credentials  Credentials of user
  */
private void userCredentialsAdd(DirContext context, String dn,
        String credentials) throws NamingException {
    // Set up security environment to bind as the user
    context.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
    context.addToEnvironment(Context.SECURITY_CREDENTIALS, credentials);
}