[Original] Method for java host to block access from certain IPs

Published: 2012-10-17

```html

JAVA host uses tomcat to process data, so it does not support the .htacess method used by apche previously. Generally, there are two blocking methods to choose from: one is to block in the server.xml file, or to modify the program code.

1. Block IP. In tomcat_home/conf/server.xml, add a line of code between <host></host>:
<Valve className="org.apache.catalina.valves.RemoteAddrValve" deny="192.168.1.1"/>   Here, fill in the IP with the IP address you need to block


 

2. To implement IP blocking programmatically, you can refer to the following JS code:
<SCRIPT LANGUAGE="java script">
<!-- Begin
netscape = (navigator.appName.indexOf("Netscape") != -1);
version4 = (navigator.appVersion.indexOf("4.") != -1);

if (netscape && version4) {
ip = "" + java.net.InetAddress.getLocalHost().getHostAddress();
if (ip.indexOf("235.12") >= -1)
// This is the IP you want to block, for example: 235.12.xxx.xxx

   {
   alert("You are not permitted to access this site.");
   history.go(-1);
   }
}
// End -->
</script>

Add the above code to the <head> section.

 

Either of the above methods can be used to block a fixed IP.

```