April 27, 2013

TLS(formerlly known as SSL) on Tomcat

You obviously need JDK, tomcat container and eclipse pre-installed for this to work.

1. Create .keystore file
    - For this cd in to {java_home}/bin directory like below and execute the following script

$ cd ${JAVA_HOME}/bin
$ keytool -genkey -alias tomcat -keyalg RSA
note - After you input a password and some other irreverent details, keytool command will generate
          .keystore file in home directory

2. configure tomcat to use .keystore like below.
First cd in to tomcat_home/conf and open server.xml
$ cd ${TOMCAT_HOME}/conf
$ gedit server.xml &

find the following commented lines
<connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
    maxThreads="150" scheme="https" secure="true"
    clientAuth="false" sslProtocol="TLS">
uncomment them..

and put the following right below it..
<Connector SSLEnabled="true" acceptCount="100" clientAuth="false"
   disableUploadTimeout="true" enableLookups="false" maxThreads="25"
   port="8443" keystoreFile="/Users/alpha/.keystore"
   keystorePass="password"
   protocol="org.apache.coyote.http11.Http11NioProtocol"
   scheme="https" secure="true" sslProtocol="TLS"/>
note - change path to .keyStore and keyStorePass variable values as per your configuration.

Open tomcat-user.xml in eclipse, add role, username and password for tomcat

In web.xml, add security-constraint and login-config tag
<security-constraint>
     <web-resource-collection>
         <web-resource-name>CRUD-APP</web-resource-name>
         <url-pattern>/*</url-pattern>
         <http-method>GET</http-method>
         <http-method>POST</http-method>
     </web-resource-collection>
     <auth-constraint>
         <role-name>user</role-name>
     </auth-constraint>

      <user-data-constraint>
         <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
 
        </security-constraint>

 <login-config>
     <auth-method>FORM</auth-method>
     <form-login-config>
         <form-login-page>/Login.html</form-login-page>
         <form-error-page>/LoginError.jsp</form-error-page>
     </form-login-config>
  </login-config>




Note: There is problem with WTP plugin in eclipse, eclipse wont pick up changes made in tomcat's server.xml file.
In order to make it work in eclipse, Just make sure you delete the tomcat server and create a new one with above specified details.

After implementing the above these were the results

If we access our app, with following url, note that, we are accessing th4 app with http and at port 8080.. like below..

http://localhost:8080/CRUD

But it changes to following..
Look closely the protocol changes to https and port no. to 8443(the default port of TLS)



Note: SSL is now called TLS(Transport Layer Security)

0 comments:

Post a Comment