To enable basic authentication with tomcat we need to tweak 2 xml file.
Add following lines of code in tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
where myname is username and password is the password for the user. You can change the rolename, username, and password as you wish.
In web.xml of you webapp add following lines of code:
Wildcard means whole app requires authentication
/*
GET
POST
tomcat
NONE
BASIC
If you see above config we specified role-name. We can define multiple role names in tomcat-user.xml. Each role name will have a different user name and password.
After you done with changes restart tomcat and access your webapp for testing, it should ask for authentication.
-Sany