31

Does anyone know if Tomcat can restrict access to certain application by IP address (like Apache's .htaccess)?

dur
  • 15,689
  • 25
  • 79
  • 125
SMSM
  • 1,509
  • 3
  • 19
  • 34

5 Answers5

27

You add a Valve to the Context in context.xml (specifically, org.apache.catalina.valves.RemoteAddrValve). See the docs on Remote Host Filters.

skaffman
  • 398,947
  • 96
  • 818
  • 769
26

This is an example:

in \apache-tomcat-7.0.33\conf\server.xml:

<Engine name="Catalina" defaultHost="localhost">
    ...
    ...
    ...
    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
        allow="10\.132\.77\.55|10\.132\.76\.120|10\.132\.77\.47"/>
    ...
</Engine>
xehpuk
  • 7,814
  • 3
  • 30
  • 54
5

In Tomcat 7, you can configure it in the web.xml.

If it's for all web apps, you can configure it in tomcat7/conf/web.xml, if it is just for one web app, you can configure it in the tomcat7/webapps/$(WEB_APP)/WEB-INF/web.xml, it's very convenient.

The configuration uses a RemoteAddrFilter filter, there is an example in Container Provided Filters.

dur
  • 15,689
  • 25
  • 79
  • 125
Ascatgz
  • 319
  • 3
  • 6
2

To set up access restriction to your web-application for the certain IP addresses, add the following strings to /opt/tomcat/webapps/{web-application name}/META-INF/context.xml file:

<Context antiJARLocking="true" path="/">
 <Valve className="org.apache.catalina.valves.RemoteIpValve" />
 <Valve className="org.apache.catalina.valves.RemoteAddrValve" deny="{IP_address}" />
</Context>

Here is the instruction how to do this via Jelastic panel. Be sure to restart your Tomcat for the changes to take effect.

Simon Baars
  • 1,877
  • 21
  • 38
Tetiana
  • 313
  • 1
  • 10
  • Thanks for this. I was looking how to restrict access for just a single webapp. By replacing "ROOT" by that specific webapp, it worked. Thanks for pointing to the great source! – Simon Baars Mar 11 '19 at 17:36
  • 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. – Simon Baars Mar 11 '19 at 17:37
  • Thanks a lot! For 8.5.37, only this solution works. Unfortunately, all other solutions are missing the RemoteIpValve statement. – Md. Apr 22 '19 at 17:56
0

in Tomcat 9,you can configure it in path:apache-tomcat-9.0.14\webapps\manager\META-INF\context.xml

袁文涛
  • 735
  • 1
  • 10
  • 23