Sometimes it’s easier to use server based authentication than to implement your own authentication. Just write some lines in some xml and everything works.
But what if your users don’t respect all the rules for a standard tomcat authentication?

The smart guys from Tomcat have a solution for that. You can write your own Realm Authentication.

In my case I needed a SSL + LDAP authentication. Tomcat has standard authentication for each one, but not combined.
The SSL certificate provides an username that must be verified in the LDAP (if the user has the right permissions).

A Realm authentication is defined by the org.apache.catalina.Realm interface. But it’s easier to extend RealmBase and override some methods instead of implementing Realm.

In my case I had to override:

public class LdapCertificateRealm extends RealmBase {
//the authentification for the certificate
//I've created my own Principal which returns the username from the LDAP corensponding to a SSL certificate.
//you have to write your own code for the ldap operations
public Principal authenticate(X509Certificate[] arg0) {

//with the LDAP username from the custom Principal we check the role
//you have to write your own code for the ldap operations
public boolean hasRole(Principal arg0, String arg1) {

protected String getName() {
return "LdapCertificateRealm";

protected String getPassword(String arg0) {
// do nothing
return null;

protected Principal getPrincipal(String arg0) {
//do nothing
return null;



Tomcat configuration:

1. SSL configuration (another story which will be told by Adi)
2. Realm configuration should look something like this

<Engine name="Catalina" defaultHost="localhost" debug="0">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="catalina_log." suffix=".txt"

<Realm className="auth.LdapCertificateRealm" debug="0"/>

<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="localhost_log." suffix=".txt" timestamp="true"/>

Web.xml (Application web.xml)
<realm-name>Site configuration</realm-name>

<web-resource-name>Application configuration</web-resource-name>


<!-- Automatically redirects http to https -->

That’s all folks!

Helpful links: