0

My application is using LDAP for user loging.

Wildfly config:

<security-domain name="LDAPAuth">
    <authentication>
        <login-module code="LdapExtended" flag="required">
            <module-option name="java.naming.factory.initial" value="com.sun.jndi.ldap.LdapCtxFactory"/>
            ....
        </login-module>
        <login-module name="Database-role" code="Database" flag="optional">
            <module-option name="password-stacking" value="useFirstPass"/>
            .....
        </login-module>
        <login-module name="Database-default" code="Database" flag="optional">
            <module-option name="password-stacking" value="useFirstPass"/>
            ....
        </login-module>
    </authentication>
</security-domain>

My question is how to search ldap users in app? I want to add possibility to search other users in ldap(for already logged users).

skoczo
  • 75
  • 1
  • 12

1 Answers1

0

You could connect to WildFly management API and read security domain configuration, but you should not do this.

Don't mix server management configuration and application configuration if you have other way.

Provide the LDAP configuration to application (e.g. context parameters in the deployment descriptor or use a property file) and then do sth. like:

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, user);
env.put(Context.SECURITY_CREDENTIALS, password);
final LdapContext ctx = new InitialLdapContext(env, null);
final SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<?> namingEnum = ctx.search("dc=mycompany,dc=example", "(uid=*)", searchControls);
while (namingEnum.hasMore()) {
    // TODO
}
namingEnum.close();
ctx.close();
kwart
  • 3,154
  • 1
  • 21
  • 22