In this series of articles, I will be explaining in detail about securing REST services. It includes configuring authentication, authorization and encryption. To begin with, this article talks about setting up BASIC authentication for a simple REST service running on Apache Tomcat.
Authentication is a process of validating the identity of a client who is trying to access the service. There are different authenticating a client, and the simplest protocol is the BASIC authentication over HTTP.
Steps involved in BASIC authentication
(1) Client sends a request without authentication details. e.g.,
Request: curl -D- -X GET localhost:8080/Fig-0.0.1/fig/task/123
(2) Server responds back with error code
401 which denotes unauthorized request. e.g.,
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="jaxrs"
Date: Thu, 05 Dec 2013 00:12:37 GMT
(3) Client sends Base64-encoded username & password in request. e.g.,
curl -D- -H "Authorization: Basic bXl1c2VybmFtZTpteXBhc3Njb2Rl" -X GET localhost:8080/Fig-0.0.1/fig/task/123
(4) Server receives the request, decodes the credentials and compares it with the server version. If the credentials match, then it responds back with status code
HTTP/1.1 200 OK
Expires: Wed, 31 Dec 1969 19:00:00 EST
Date: Thu, 05 Dec 2013 05:39:46 GMT
Steps to enable BASIC authentication in Apache Tomcat
How to add users, roles and passwords?
Under your tomcat installation, look for
$TOMCAT_HOME/conf/tomcat-users.xml file. Add the below lines which defines 2 users and assigns specific roles to them.
<?xml version='1.0' encoding='utf-8'?>
<user username="dev1" password="devpassword1" roles="developer"/>
<user username="admin1" password="adminpassword1" roles="admin"/>
How to enable BASIC authentication in Tomcat?
Now that the users and roles are added, let us activate the authentication in our web application. Open your application’s
web.xml and add the following.
<!-- Specifies the part of the application to be authenticated -->
<web-resource-name>Secure Task Services</web-resource-name>
<!-- Denotes which URI patterns needs to be protected. -->
<!-- Only POST, PUT & DELETE calls are authenticated. Omitting http-method tag
altogether will secure all access to the url-pattern above -->
<!-- Specifies which roles defined in tomcat-users.xml have access to the resources. -->
<!-- Requests are processed without encryption -->
<!-- Denotes the authentication method, which in our case is BASIC -->
That’s about it. If you bounce the Tomcat and try to access the resources via POST/PUT/DELETE method, you will be access for credentials.
Advantages of BASIC authentication: Very easy to set up authentication.
Disadvantages of BASIC authentication: Username/password can be easily sniffed out by attackers since it is just encoded, and not encrypted.
Encrypted HTTP Request
Using an encrypted HTTP connection like HTTPS where packets are encrypted with SSL solves the downside of BASIC authentication. To enable HTTPS, uncomment the below lines in
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
and change the
<transport-guanrantee> value in
CONFIDENTIAL instead of