Java, Software Development, Tomcat

Tomcat 9 and Up: JDBC Realm For Basic HTTP Authentication

It is amazing how we can modify Tomcat to help hasten the development. This post will demonstrate how to use Tomcat JDBC Realm, which uses an RDBMS for Basic HTTP Authentication. This could cut down development time and effort for simple applications.

Tomcat and JDBC Realm Requirements 

  1. Java 11 (JDK11)
  2. Tomcat 9.0.3
  3. Hsqldb-2.6.0
  4. Windows 10

Java, Tomcat, and HSQLDB Installation

First, we install JDK11. We can do this either using the installer or zip distribution. For this post, we opt for the former. Install the JDK and set JAVA_HOME to its home path (i.e., c:\program files\Java\jdk11). Then, restart Windows. On the other hand, using a zip distribution may be easier and simple because we do not need to install JDK11. Instead, we extract the binaries to some directory and set JAVA_HOME and PATH environment variables.

Second, we configure Tomcat 9.0.3. For Tomcat, we extract the downloaded zip file to some directory and go to <SOME-DIRECTORY>\apache-tomcat-9.0.3\bin. Then, start Tomcat by double-clicking “startup.bat.” Then, verify Tomcat is running by accessing http://localhost:8080. Later, we will configure Tomcat to allow for JDBC Realm Basic Authentication.

Third, we set up HSQLDB by extracting the downloaded zip file to some directory. Then, invoke the following in the command-line interpreter: <SOME-DIRECTORY>\hsqldb\bin\> runServer.bat --database.0 file:mydb --dbname.0 xdb. This will start the HSQLDB server and create an empty database with a “mydb” name.

Then, create tables and insert some rows into them.

Create HSQLDB Tables and Insert Data

Double-click the file runManagerSwing.bat in <SOME-DIRECTORY>\hsqldb\bin\, and the HSQL Database Manager will startup as shown below.

Tomcat will use these roles and users for the JDBC Realm Basic Authentication. Use the following configuration details to access the HSQLDB from an SQL IDE, e.g., Data Grip.

Configure Tomcat For JDBC Realm

Modify <TOMCAT_HOME>/conf/server.xml by replacing the following line numbers 144-145.

with this:

Deploy and configure your Application

Before we deploy our application, let us modify the web.xml file a bit. Note we added one role under the auth-constraint element, which is also available in our database.

Therefore, only users of the tomcat role can sign in to Tomcat using JDBC Realm via Basic HTTP Authentication.

tomcat jdbc realm authentication

Test JDBC Realm Basic HTTP Authentication

From the web.xml, only users with tomcat roles can get in to access the resources of Tomcat. Access the sample web application via http://localhost:8080/java_tomcat_jdbc_realm_war/index.jsp and use tomcat/tomcat123 username and password. Make sure HSQLDB is up and running.

tomcat jdbc realm authentication


You should be able to view the content of the index.jsp.

That is how we can use Tomcat JDBC Realm for HTTP Basic Authentication!

Got comments, or suggestions? Please visit our Facebook page!

You Might Also Like