org.springframework.ldap.core.support.DefaultDirObjectFactory Java Examples

The following examples show how to use org.springframework.ldap.core.support.DefaultDirObjectFactory. 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: LdapSearchContext.java    From hesperides with GNU General Public License v3.0 6 votes vote down vote up
private DirContext buildSearchContext(String username, String password) {
    Hashtable<String, String> env = new Hashtable<>();
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapConfiguration.getUrl());
    env.put(Context.OBJECT_FACTORIES, DefaultDirObjectFactory.class.getName());
    env.put("com.sun.jndi.ldap.connect.timeout", ldapConfiguration.getConnectTimeout());
    env.put("com.sun.jndi.ldap.read.timeout", ldapConfiguration.getReadTimeout());
    env.put(Context.SECURITY_PRINCIPAL, String.format("%s\\%s", ldapConfiguration.getDomain(), username));
    env.put(Context.SECURITY_CREDENTIALS, password);

    try {
        DirContext dirContext = new InitialLdapContext(env, null);
        // ici dirContext ne contient que des infos relatives au serveur avec lequel la connexion vient d'être établie
        if (log.isDebugEnabled()) { // on évite ce traitement si ce n'est pas nécessaire
            log.debug("[buildSearchContext] dirContext: {}", gson.toJson(attributesToNative(dirContext.getAttributes("").getAll())));
        }
        return dirContext;
    } catch (AuthenticationException | OperationNotSupportedException cause) {
        throw new BadCredentialsException(messages.getMessage(
                "LdapAuthenticationProvider.badCredentials", "Bad credentials"), cause);
    } catch (NamingException e) {
        log.error(e.getExplanation() + (e.getCause() != null ? (" : " + e.getCause().getMessage()) : ""));
        throw LdapUtils.convertLdapException(e);
    }
}
 
Example #2
Source File: LdapTestUtils.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
       try {
           LdapName baseDn = (LdapName)
                   context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);

           LdifParser parser = new LdifParser(ldifFile);
           parser.open();
           while (parser.hasMoreRecords()) {
               LdapAttributes record = parser.getRecord();

               LdapName dn = record.getName();

               if(baseDn != null) {
                   dn = LdapUtils.removeFirst(dn, baseDn);
               }

               if(!rootNode.isEmpty()) {
                   dn = LdapUtils.prepend(dn, rootNode);
               }
               context.bind(dn, null, record);
           }
       } catch (Exception e) {
           throw new UncategorizedLdapException("Failed to populate LDIF", e);
       }
   }
 
Example #3
Source File: LdapTestUtils.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
    try {
        LdapName baseDn = (LdapName)
                context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);

        LdifParser parser = new LdifParser(ldifFile);
        parser.open();
        while (parser.hasMoreRecords()) {
            LdapAttributes record = parser.getRecord();

            LdapName dn = record.getName();

            if(baseDn != null) {
                dn = LdapUtils.removeFirst(dn, baseDn);
            }

            if(!rootNode.isEmpty()) {
                dn = LdapUtils.prepend(dn, rootNode);
            }
            context.bind(dn, null, record);
        }
    } catch (Exception e) {
        throw new UncategorizedLdapException("Failed to populate LDIF", e);
    }
}