I'm developing a webapp using Apache Tomcat. This webapp will be deployed in a server which hosts other webapps. Now, since my webapp requires the user to authenticate, I'd need to modify some files in the "conf" directory of Tomcat such as "tomcat-users.xml". The problem is that these files are shared with the other webapps. Is there a way to have a "conf" directory for my webapp which is separated from the shared one? Thank you.
Asked
Active
Viewed 4,329 times
1 Answers
3
Tomcat allows you to separate out configuration from the main server.xml
by creating an xml file underneath $CATALINA_BASE/conf/[enginename]/[hostname]
called [YourWebappName].xml
.
For example: $CATALINA_BASE/conf/Catalina/localhost/PetClinic.xml
This file bascially contains a <Context>
element defining the webapp specific context configuration. In here you can define your own webapp specific realm with the necessary authentication parameters for your app.
This mechanism is designed so that you can modify webapp specific configuration without touching the top-level shared files in Tomcat's conf
folder.

Will Keeling
- 22,055
- 4
- 51
- 61
-
Thank you a lot. I have decided to define the context of my webapp creating a file called "context.xml" in the META-INF directory of my webapp. Now the problem is: what am I supposed to write in that .xml file? I'd know how to modify the "tomcat-users.xml" and the "server.xml" files in Tomcat's "conf" folder to implement authentication but I really don't know how to translate those changes in the context.xml file. Can you help me? – user3067088 Dec 11 '13 at 18:06
-
1You can nest a `Realm` element directly underneath the `Context` element in your `context.xml` file (you should be able to pretty much lift it wholesale from your `server.xml`). You can also create a `WEB-INF/users.xml` file for the webapp specific users so you don't need to touch `tomcat-users.xml`. There's a good article [here](http://wiki.metawerx.net/wiki/SecuringYourSiteWithContainerManagedSecurity) (albeit a few years old - but should still be good) that walks you through it with examples of what the files should look like. – Will Keeling Dec 11 '13 at 19:19
-
You don't need to create a `conf/[enginename]/[hostname]/[YourWebappName].xml.` You can provide a META-INF/Context.xml inside your webapp. – user207421 Dec 11 '13 at 22:32