Home » Java » Enterprise Java » Securing your Tomcat app with SSL and Spring Security

About Roger Hughes

Securing your Tomcat app with SSL and Spring Security

If you’ve seen my last blog, you’ll know that I listed ten things that you can do with Spring Security. However, before you start using Spring Security in earnest one of the first things you really must do is to ensure that your web app uses the right transport protocol, which in this case is HTTPS – after all there’s no point in having a secure web site if you’re going to broadcast your user’s passwords all over the internet in plain text. To setup SSL there are three basic steps…

Creating a Key Store

The first thing you need is a private keystore containing a valid certificate and the simplest way to generate one of these is to use Java’s keytool utility located in the $JAVA_HOME/bin directory.

keytool -genkey -alias MyKeyAlias -keyalg RSA -keystore /Users/Roger/tmp/roger.keystore

In the above example,

  • -alias is the unique identifier for your key.
  • -keyalg is the algorithm used to generate the key. Most examples you find on the web usually cite ‘RSA’, but you could also use ‘DSA’ or ‘DES’
  • -keystore is an optional argument specifying the location of your key store file. If this argument is missing then the default location is your $HOME directory.

RSA stands for Ron Rivest (also the creator of the RC4 algorithm), Adi Shamir and Leonard Adleman

DSA stands for Digital Signature Algorithm

DES stands for Data Encryption Standard

For more information on keytool and its arguments take a look at this Informit article by Jon Svede

When you run this program you’ll be asked a few questions:

Roger$ keytool -genkey -alias MyKeyAlias -keyalg RSA -keystore /Users/Roger/tmp/roger.keystore
Enter keystore password: 
Re-enter new password:
What is your first and last name?
  [Unknown]:  localhost
What is the name of your organizational unit?
  [Unknown]:  MyDepartmentName
What is the name of your organization?
  [Unknown]:  MyCompanyName
What is the name of your City or Locality?
  [Unknown]:  Stafford
What is the name of your State or Province?
  [Unknown]:  NA
What is the two-letter country code for this unit?
  [Unknown]:  UK
Is CN=localhost, OU=MyDepartmentName, O=MyCompanyName, L=Stafford, ST=UK, C=UK correct?
  [no]:  Y

Enter key password for 

     (RETURN if same as keystore password):

Most of the fields are self explanatory; however for the first and second name values, I generally use the machine name – in this case

Updating the Tomcat Configuration

The second step in securing your app is to ensure that your tomcat has an SSL connector. To do this you need to find tomcat’s server.xml configuration file, which is usually located in the 'conf' directory. Once you’ve got hold of this and if you’re using tomcat, then it’s a matter of uncommenting:

<Connector port='8443' protocol='HTTP/1.1' SSLEnabled='true'
               maxThreads='150' scheme='https' secure='true'
               clientAuth='false' sslProtocol='TLS' />

…and making it look something like this:

<Connector SSLEnabled='true' keystoreFile='/Users/Roger/tmp/roger.keystore' keystorePass='password' port='8443' scheme='https' secure='true' sslProtocol='TLS'/>

Note that the password ‘password’ is in plain text, which isn’t very secure. There are ways around this, but that’s beyond the scope of this blog.

If you’re using Spring’s tcServer, then you’ll find that it already has a SSL connector that’s configured something like this:

<Connector SSLEnabled='true' acceptCount='100' connectionTimeout='20000' executor='tomcatThreadPool' keyAlias='tcserver' keystoreFile='${catalina.base}/conf/tcserver.keystore' keystorePass='changeme' maxKeepAliveRequests='15' port='${bio-ssl.https.port}' protocol='org.apache.coyote.http11.Http11Protocol' redirectPort='${bio-ssl.https.port}' scheme='https' secure='true'/>

…in which case it’s just a matter of editing the various fields including keyAlias, keystoreFile and keystorePass.

Configuring your App

If you now start tomcat and run your web application, you’ll now find that it’s accessible using HTTPS. For example typing https://localhost:8443/my-app will work, but so will http://localhost:8080/my-app This means that you also need to do some jiggery-pokery on your app to ensure that it only responds to HTTPS and there are two approaches you can take.

If you’re not using Spring Security, then you can simply add the following to yourweb.xml before the last web-app tag:


If you are using Spring Security, then there are a few more steps to getting things going. Part of the general Spring Security setup is to add the following to your web.xml file. Firstly you need to add a Spring Security application context file to the contextConfigLocation context-param:


Secondly, you need to add the Spring Security filter and filter-mapping:


Lastly, you need to create, or edit, your application-security.xml as shown in the very minimalistic example below:

<?xml version='1.0' encoding='UTF-8'?>
<beans:beans xmlns='http://www.springframework.org/schema/security'

       <http auto-config='true' >
          <intercept-url pattern='/**' requires-channel='https' />    



In the example above intercept-url element has been set up intercept all URLs and force them to use the https channel.

The configuration details above may give the impression that it’s quicker to use the simple web.xml config change, but if you’re already using Spring Security, then it’s only a matter of adding a requires-channel attribute to your existing configuration.

A sample app called tomcat-ssl demonstrating the above is available on git hub at: https://github.com/roghughe/captaindebug

Reference: Securing your Tomcat app with SSL and Spring Security from our JCG partner Roger Hughes at the Captain Debug’s Blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....



  1. Thanks a lot for this. It was very helpful!!

  2. This is very useful if you are using Tomcat in a single server world.

    Why would you not just recommend to put a WebServer in front to deal with the HTTPS then send to Tomcat .

    Using a webserver to deal with the HTTPS seems more straight forward and if you do need load balancing or multiple site URLS it is very easy.

    The previous post about configuring Apache –> Tomcat via Mod_JK seems the best approach.

Leave a Reply

Your email address will not be published. Required fields are marked *


× nine = 27

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.