RESTful Web Services Basic Authentication in Java

Securing Java RESTful web services with HTTP Basic Authentication in Tomcat using web.xml security constraints.

Basic authentication is the simplest way to securing any URL. User should have permission to the server to access the URL. This is Container-managed authentication.

What is Authentication?

Is the process to identifying the user, who can access the system. Http basic authentication is the first step for RESTful security. There are many ways to implements the RESTful security, the basic authentication is the top level security where Base64 encoded string is used to access the server.

Note –

Basic authentication is not 100% secure, because it is not strongly encrypt the password. We need to use the HTTPS or other encryption mechanism between client and server.

Flow the below steps to implements the basic Authentication

Step 1

Develop a Restful webservices using jersey RESTful api. If you don't know how to develop webservices in jersey read my previous post.

Step 2

Open the web.xml file and add the below code inside the web-app tag.

XML
BasicAuthentication
  RestFulWebServices
  
    RestTest
    org.glassfish.jersey.servlet.ServletContainer
    
      javax.ws.rs.Application
      com.controller.MyRESTApplication
    
    1
  
  
    RestTest
    /*
  
  
    
        /*
    
    
        admin
    


    
        /*
    
    
        customer
    


    BASIC
    my-default-realm

Step 3

Now open the tomcat-users.xml. and write the user details like below.

XML

now type the url in any client it will ask for usrid and password. Provide the user id and password which is there in the tocat-users.xml.

Note-

If it is not accepting userid and password , open the server.xml and remove all Realm tag.

XML
About Glenna Babshire29 Articles
Java & libraries writer. Writes the Java tutorials: the language from Java 21 on, encryption, configuration and the libraries around it.