Securing RESTful Web Service with Basic Authentication

1. Introduction

We have been through several web service tutorials and particularly on creating RESTful Web Service with Spring. In this tutorial, we will check out how we can secure our RESTful Web Service with Basic Authentication using Spring Security.

2. Implementation

Once the RESTful Web Service is ready, add the following to the existing pom dependencies –


Make sure the web.xml has the Spring Security filter configured. If not, add the following –


In the spring context XML, add the following –


Note that we have added a sample user, with credentials user/123, instead of which we could easily configure an LDAP authentication in place for advanced implementations.

<security:http-basic /> is something that’s more important here to enable basic authentication. Read more about Spring Security Basic Authentication here

3. Useful Links

4. Running the application

Secure Rest Service

5. Download the source code

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.