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 Connectorin the fileserver.xmlin/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
Tomcatwill allow bothHTTP(Port 8080) andHTTPS(Port 443) traffic through- Note that the Ports were specified in the respective
Connectorsin theserver.xmlfile
- Note that the Ports were specified in the respective
- Next you need to instruct
Tomcatand tell it whichURLsto redirect toHTTPSand which ones to allow usingHTTP - You can do this in two ways
- Together for all your
Web ApplicationsorContextsdeployed on yourTomcat- Edit the file
web.xmlin/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-patternyou like - Note the
transport-guaranteeis set toNONEimplyingHTTPaccess
- We have used the
- The second
Security Constraintspecifies to whichURLs,HTTPSaccess is required- Note the
transport-guaranteeis set toCONFIDENTIALimplyingHTTPSaccess - The
url-patternhere 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
TomcatmatchesURLpatterns – http://docs.roguewave.com/hydraexpress/3.5.0/html/rwsfservletug/4-3.html - Essentially
Tomcatwill do a longestURLmatch which means that if aURLmatcheshttp://[Your Domain Name OR IP Address]:[Your Port]/[Your Web Application Name]/API/v1/Public/*then the firstsecurity-constraintwill be used instead of the second - You have essentially configured your
Tomcat7so that by default it usesHTTPSand only allowsHTTPaccess to specificURLs(theseURLsare your security exceptions)
- Note the
- For each
Web Applicationindependently- Add the exact same
security constraintsas above to your specific Web Application’sweb.xmldeployment descriptor file instead of the globalweb.xmlfile, which was updated above - Your Web Application’s individual
web.xmlfile is typically in theWEB-INFdirectory - Do this for each Web Application individually
- The Web Applications’ whose
web.xmlfiles you do not update, will allow bothHTTPandHTTPSaccess to all their URLs
- Add the exact same