Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat: Restrict access to localhost for /just one/ webapp

I'm running Tomcat 6 to serve several web apps, most of which are public-facing. But I'd like to restrict access to just one webapp, allowing connections only from localhost.

I can restrict access for all webapps using a valve in context.xml, as described in:

  • Tomcat Restrict access by IP address

But I can't figure out how to restrict access on a per-app basis. Is there a way to do this with my app's web.xml? Or by adding additional rules to context.xml?

Thanks,

-B


Recapping Solution:

$ cp /var/lib/tomcat6/conf/context.xml \ 
   /var/lib/tomcat6/conf/Catalina/localhost/my-app-name.xml

$ cat /var/lib/tomcat6/conf/Catalina/localhost/my-app-name.xml

<Context>
    <Valve className="org.apache.catalina.valves.RemoteHostValve" allow="localhost"/>
... {as previously} ...
</Context>
like image 930
Bosh Avatar asked Mar 07 '11 19:03

Bosh


People also ask

Is Tomcat capable of restricting access?

element to tell Tomcat where to look for user accounts and password information. file, configure the security settings, including which URIs to secure, which authentication method to use (BASIC, DIGEST, FORM, or CLIENT-CERT), and whether to always use HTTPS.

Is it possible to block certain IP addresses in Tomcat?

Thanks for pointing to the great source! Oh by the way (for everyone trying this method), you have to restart your Tomcat instance for the IP restriction to take effect. Futhermore, you can replace deny by allow to only allow certain IP's rather than only blocking certain IP's.

How do I access Tomcat locally?

You need to make Tomcat listen to 192.168. 1.100 address also. If you want it to listen to all interfaces (IP-s) just remove "address=" from Connector string in your configuration file and restart Tomcat.


Video Answer


1 Answers

You can create an individual context.xml for you app.

This is an excerpt from Tomcat doc on context configuraion: Context elements may be explicitly defined:

  • In the $CATALINA_HOME/conf/context.xml file: the Context element information will be loaded by all webapps. In the $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host.
  • In individual files (with a .xml extension) in the $CATALINA_HOME/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml) extension will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.
  • Only if a context file does not exist for the application in the $CATALINA_HOME/conf/[enginename]/[hostname]/; in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_HOME/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.
like image 180
Dmitry Negoda Avatar answered Oct 08 '22 23:10

Dmitry Negoda