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

The following examples show how to use javax.naming.directory.DirContext#getEnvironment() . 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: LdapContextSourceIntegrationTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetReadOnlyContext() throws NamingException {
	DirContext ctx = null;

	try {
		ctx = tested.getReadOnlyContext();
		assertThat(ctx).isNotNull();
		Hashtable environment = ctx.getEnvironment();
		assertThat(environment.containsKey(LdapContextSource.SUN_LDAP_POOLING_FLAG)).isFalse();
		assertThat(environment.containsKey(Context.SECURITY_PRINCIPAL)).isTrue();
		assertThat(environment.containsKey(Context.SECURITY_CREDENTIALS)).isTrue();
	}
	finally {
		// Always clean up.
		if (ctx != null) {
			try {
				ctx.close();
			}
			catch (Exception e) {
				// Never mind this
			}
		}
	}
}
 
Example 2
Source File: LdapContextSourceIntegrationTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetReadWriteContext() throws NamingException {
	DirContext ctx = null;

	try {
		ctx = tested.getReadWriteContext();
		assertThat(ctx).isNotNull();
		// Double check to see that we are authenticated.
		Hashtable environment = ctx.getEnvironment();
           assertThat(environment.containsKey(LdapContextSource.SUN_LDAP_POOLING_FLAG)).isFalse();
		assertThat(environment.containsKey(Context.SECURITY_PRINCIPAL)).isTrue();
		assertThat(environment.containsKey(Context.SECURITY_CREDENTIALS)).isTrue();
	}
	finally {
		// Always clean up.
		if (ctx != null) {
			try {
				ctx.close();
			}
			catch (Exception e) {
				// Never mind this
			}
		}
	}
}
 
Example 3
Source File: AbstractContextSource.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
/**
 * Create a DirContext using the supplied environment.
 *
 * @param environment the LDAP environment to use when creating the
 * <code>DirContext</code>.
 * @return a new DirContext implementation initialized with the supplied
 * environment.
 */
protected DirContext createContext(Hashtable<String, Object> environment) {
	DirContext ctx = null;

	try {
		ctx = getDirContextInstance(environment);

		if (LOG.isInfoEnabled()) {
			Hashtable<?, ?> ctxEnv = ctx.getEnvironment();
			String ldapUrl = (String) ctxEnv.get(Context.PROVIDER_URL);
			LOG.debug("Got Ldap context on server '" + ldapUrl + "'");
		}

		return ctx;
	}
	catch (NamingException e) {
		closeContext(ctx);
		throw LdapUtils.convertLdapException(e);
	}
}
 
Example 4
Source File: LdapContextSourceIntegrationTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
   @Category(NoAdTest.class)
public void testGetContext() throws NamingException {
	DirContext ctx = null;
	try {
		String expectedPrincipal = "cn=Some Person,ou=company1,ou=Sweden," + base;
		String expectedCredentials = "password";
		ctx = tested.getContext(expectedPrincipal, expectedCredentials);
		assertThat(ctx).isNotNull();
		// Double check to see that we are authenticated, and that we did not receive
		// a connection eligible for connection pooling.
		Hashtable environment = ctx.getEnvironment();
           assertThat(environment.containsKey(LdapContextSource.SUN_LDAP_POOLING_FLAG)).isFalse();
		assertThat(environment.get(Context.SECURITY_PRINCIPAL)).isEqualTo(expectedPrincipal);
		assertThat(environment.get(Context.SECURITY_CREDENTIALS)).isEqualTo(expectedCredentials);
	}
	finally {
		// Always clean up.
		if (ctx != null) {
			try {
				ctx.close();
			}
			catch (Exception e) {
				// Never mind this
			}
		}
	}
}
 
Example 5
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 6
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 7
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;
}