Search code examples
javatomcatldapjndi

access JNDIRealm in JSP?


I have configured a JNDIRealm in tomcats server.xml

    <Realm className="org.apache.catalina.realm.JNDIRealm"
           connectionURL="ldaps://xxx"
           alternateURL="ldaps://xxx"
           userBase="xxx"
           userSearch="SamAccountName={0}"
           userSubtree="true"
           roleBase="xxx"
           roleName="CN"
           roleSearch="(member={0})"
           connectionName="xxx"
           connectionPassword="xxx"
   />

I would like to perform further ldap queries in some JSP files (like getting email of the user, or full username)

how can I access this realm from a JSP file?

basically I would like to have a LdapContext, which I can use for my existing ldap query code.


Solution

  • Unfortunately for you, no, you won't be able to make any direct LDAP queries.

    But that's not because the LDAPRealm isn't available, it's because you can't use an LDAPRealm to issue arbitrary LDAP queries. You could use JMX to get the LDAPRealm, but you'd only be able to call exposed methods on that object.

    If you want to issue arbitrary LDAP queries, you'll have to configure an LDAP resource in some other way.

    As far as I know, there is nothing like a DataSource that can be both used in a realm (like with DataSourceRealm) as well as directly, for an LDAP server. You'll have to contact the LDAP server using something other than what Tomcat can provide out of the box.

    If you'd still like to use JNDI to fetch an LDAP connection, you can have a look at this SO question and answers: Trying to configure LDAP as JNDI Resource in Tomcat