Tag Archives: apache

Use ldap-based authentication per a context

This article will be useful if you want to use a separated authentication system for each context with Apache Tomcat


Apache Tomcat’s manager application has its own security roles. If you define  configuration in server.xml ( after deleting “),  a Manager application deployed in a same host can’t use its own roles/users configuration.

If you can create roles with same names in a LDAP server  and add users to it, that’s a simple solution. But you have to request it to LDAP server manager if you don’t have rights.

This configuration help you to deploy an web application without requesting LDAP server configuration changes..

How to

  1. Create a META-INF/context.xml in a context’s application directory with default contents
    <?xml version="1.0" encoding="UTF-8"?>
    <Context antiResourceLocking="false" privileged="true" >
  2. Create  definition in  tag
    <Context antiResourceLocking="false" privileged="true" >
    <Realm className="org.apache.catalina.realm.JNDIRealm" debug="99" connectionURL="ldap://" authentication="simple" referrals="follow" connectionName="CN=ad_user,CN=Users,DC=Apache,DC=NET" connectionPassword="xxxxxx" userSearch="(sAMAccountName={0})" userBase="dc=Apache,dc=NET" userSubtree="true" roleSearch="(cn={0})" roleName="cn" roleSubtree="true" roleBase="dc=Apache,dc=NET">
  3. Add this xml code in web.xml
       <!-- Other codes -->

    If you change <auth-method>‘s value to ‘BASIC’, you can remove <form-login-config> tag and use Browser’s default login dialog.

  4. Create login.html page
    <form action="j_security_check" method="POST">
    <td colspan="2">Login to the TVL's OpenGrok System</td>
    <td colspan="2">Use LGE AD account and password:</td>
    <td><input name="j_username" type="text" /></td>
    <td><input name="j_password" type="password" /></td>
    <td colspan="2"><input type="submit" value="Go" /></td>
  5. Restart Tomcat or a context itself.