Java Code Examples for org.springframework.ldap.support.LdapUtils#closeContext()

The following examples show how to use org.springframework.ldap.support.LdapUtils#closeContext() . 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: LdapAuthRepositoryCustomImpl.java    From Spring-5.0-Projects with MIT License 6 votes vote down vote up
@Override
public boolean authenticateLdapUserWithContext(String userName, String password) {
	DirContext ctx = null;
	try {
		String userDn = getDnForUser(userName);
		ctx = ldapTemplate.getContextSource().getContext(userDn, password);
		return true;
	} catch (Exception e) {
		// If exception occurred while creating Context, means - authentication did not succeed
		logger.error("Authentication failed ", e.getMessage(),e);
		return false;
	} finally {
		// DirContext must be closed here.
		LdapUtils.closeContext(ctx);
	}
}
 
Example 2
Source File: LdapContextSourceIntegrationTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
   @Category(NoAdTest.class)
public void verifyAuthenticate() {
	EqualsFilter filter = new EqualsFilter("cn", "Some Person2");
	List<String> results = ldapTemplate.search("", filter.toString(), new DnContextMapper());
	if (results.size() != 1) {
		throw new IncorrectResultSizeDataAccessException(1, results.size());
	}

	DirContext ctx = null;
	try {
		ctx = tested.getContext(results.get(0), "password");
		assertThat(true).isTrue();
	}
	catch (Exception e) {
		fail("Authentication failed");
	}
	finally {
		LdapUtils.closeContext(ctx);
	}
}
 
Example 3
Source File: LdapAuthenticationProvider.java    From hesperides with GNU General Public License v3.0 5 votes vote down vote up
public HashSet<String> getUserGroupsDN(String username, String password) {
    DirContextAdapter dirContextAdapter = (DirContextAdapter) self.searchCN(username, password);
    Attributes attributes;
    try {
        attributes = dirContextAdapter.getAttributes("");
    } catch (NamingException e) {
        throw LdapUtils.convertLdapException(e);
    } finally {
        LdapUtils.closeContext(dirContextAdapter);
    }
    return extractDirectParentGroupDNs(attributes);
}
 
Example 4
Source File: AbstractTlsDirContextAuthenticationStrategy.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
public final DirContext processContextAfterCreation(DirContext ctx, String userDn, String password)
		throws NamingException {

	if (ctx instanceof LdapContext) {
		final LdapContext ldapCtx = (LdapContext) ctx;
		final StartTlsResponse tlsResponse = (StartTlsResponse) ldapCtx.extendedOperation(new StartTlsRequest());
		try {
			if (hostnameVerifier != null) {
				tlsResponse.setHostnameVerifier(hostnameVerifier);
			}
			tlsResponse.negotiate(sslSocketFactory); // If null, the default SSL socket factory is used
			applyAuthentication(ldapCtx, userDn, password);

			if (shutdownTlsGracefully) {
				// Wrap the target context in a proxy to intercept any calls
				// to 'close', so that we can shut down the TLS connection
				// gracefully first.
				return (DirContext) Proxy.newProxyInstance(DirContextProxy.class.getClassLoader(), new Class<?>[] {
						LdapContext.class, DirContextProxy.class }, new TlsAwareDirContextProxy(ldapCtx,
						tlsResponse));
			}
			else {
				return ctx;
			}
		}
		catch (IOException e) {
			LdapUtils.closeContext(ctx);
			throw new UncategorizedLdapException("Failed to negotiate TLS session", e);
		}
	}
	else {
		throw new IllegalArgumentException(
				"Processed Context must be an LDAPv3 context, i.e. an LdapContext implementation");
	}

}
 
Example 5
Source File: LdapSearchContext.java    From hesperides with GNU General Public License v3.0 4 votes vote down vote up
public void closeContext() {
    if (dirContext != null) {
        LdapUtils.closeContext(dirContext); // implique la suppression de l'env créé dans .buildSearchContext
    }
}
 
Example 6
Source File: LdapTransactionUtilsTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Test
public void testCloseContext() throws NamingException {
    LdapUtils.closeContext(dirContextMock);
    verify(dirContextMock).close();
}
 
Example 7
Source File: LdapTransactionUtilsTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Test
public void testCloseContext_NullContext() throws NamingException {
    LdapUtils.closeContext(null);
}