view counter

Tomcat basic authentication with username and password

Thanks to Sandeep for this story

To enable basic authentication with tomcat we need to tweak 2 xml file.

view counter
  • tomcat-users.xml in TOMCAT_HOME/conf/
  • web.xml in your webapp for which you want to enable basic authentication.

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

Read the entire article at its source

view counter