Note
The steps below were tested on Tomcat7
running on an Ubuntu Linux 14.04 LTS
instance in AWS EC2
. However, these steps should work on Tomcat7
running on any OS (after adjusting for the particular installation directories).
Steps
- Enable the
HTTPS Connector
in the fileserver.xml
in/etc/tomcat7/
or/var/lib/tomcat7/conf/
<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="[Your Keystore Filename].jks"
keystorePass="[Your Keystore Password]"/>
- Next, enable the
HTTP Connector
<Connector port="8080" enableLookups="false"
redirectPort="443" />
This instructs Tomcat
to redirect all HTTP
traffic on Port 8080
to the HTTPS Connector
on Port 443
- At this point your
Tomcat
will allow bothHTTP
(Port 8080
) andHTTPS
(Port 443
) traffic through- Note that the Ports were specified in the respective
Connectors
in theserver.xml
file
- Note that the Ports were specified in the respective
- Next you need to instruct
Tomcat
and tell it whichURLs
to redirect toHTTPS
and which ones to allow usingHTTP
- You can do this in two ways
- Together for all your
Web Applications
orContexts
deployed on yourTomcat
- Edit the file
web.xml
in/etc/tomcat7/
or/var/lib/tomcat7/conf/
- At the end of the file, add the following security constraints
- Edit the file
- Together for all your
<security-constraint>
<web-resource-collection>
<web-resource-name>HTTP Allowed</web-resource-name>
<url-pattern>/API/v1/Public/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>HTTPS Only</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<!-- auth-constraint goes here if you requre authentication -->
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
- The first Security Constraint specifies to which URLs, HTTP access is allowed
- We have used the
url-pattern
“/API/v1/Public/*
“ - This translates to
http://[Your Domain Name OR IP Address]:[Your Port]/[Your Web Application Name]/API/v1/Public/[Any Resource Name (e.g., Servlet Name) that your Web Application is listening for]
- You can of course use any
url-pattern
you like - Note the
transport-guarantee
is set toNONE
implyingHTTP
access
- We have used the
- The second
Security Constraint
specifies to whichURLs
,HTTPS
access is required- Note the
transport-guarantee
is set toCONFIDENTIAL
implyingHTTPS
access - The
url-pattern
here is/*
- This translates to
http://[Your Domain Name OR IP Address]:[Your Port]/[Your Web Application Name]/[Any Resource Name (e.g., Servlet Name) that your Web Application is listening for]
- See this page to understand how
Tomcat
matchesURL
patterns – http://docs.roguewave.com/hydraexpress/3.5.0/html/rwsfservletug/4-3.html - Essentially
Tomcat
will do a longestURL
match which means that if aURL
matcheshttp://[Your Domain Name OR IP Address]:[Your Port]/[Your Web Application Name]/API/v1/Public/*
then the firstsecurity-constraint
will be used instead of the second - You have essentially configured your
Tomcat7
so that by default it usesHTTPS
and only allowsHTTP
access to specificURLs
(theseURLs
are your security exceptions)
- Note the
- For each
Web Application
independently- Add the exact same
security constraints
as above to your specific Web Application’sweb.xml
deployment descriptor file instead of the globalweb.xml
file, which was updated above - Your Web Application’s individual
web.xml
file is typically in theWEB-INF
directory - Do this for each Web Application individually
- The Web Applications’ whose
web.xml
files you do not update, will allow bothHTTP
andHTTPS
access to all their URLs
- Add the exact same