-
IP Filter
Hi, I am a beginner in java security. Can anyone give me the details of the IP Filter with the help of java? I want to know how to achieve IP Filter with the help of Java. I had searched on different java forums. But, don't get any information about it. If you have any information then please, give it to me. It will be helpful to me. Thank you in advance.
-
Re: IP Filter
HI, I don't have that much knowledge about IP filter, but I have book which has following code related to IP Filter which will helpful to you to get methods about the IP Filter.
Code:
import java.io.IOException;
import java.util.StringTokenizer;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
public class IPFilter implements Filter
{
private FilterConfig fconfig;
public final static String IP_RANGE = "192.168";
public IPFilter()
{
}
public void init(FilterConfig filterConfig) throws ServletException
{
this.fconfig = filterConfig;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
String ip = request.getRemoteAddr();
HttpServletResponse httpResp = null;
if (response instanceof HttpServletResponse)
httpResp = (HttpServletResponse) response;
StringTokenizer toke = new StringTokenizer(ip, ".");
int dots = 0;
String byte1 = "";
String byte2 = "";
String client = "";
while (toke.hasMoreTokens())
{
++dots;
if (dots == 1)
{
byte1 = toke.nextToken();
}
else
{
byte2 = toke.nextToken();
break;
}
}
client = byte1 + "." + byte2;
if (IP_RANGE.equals(client))
{
httpResp.sendError(HttpServletResponse.SC_FORBIDDEN, "That means goodbye forever!");
}
else
{
chain.doFilter(request, response);
}
}
public void destroy()
{
}
}
-
Re: IP Filter
Hello, The filter is the program which provides you basic security mechanism for a firewall. It provides you the information about the traffic traffic which passes through the firewall with the help of IP address. This protects the secure network from outsiders. It is a object which work on the request and response. You can ban the traffic from specific IP address with the use of IP address. So, it is just needed to add this id in your program. It is the most simplest use of the security in computer.
-
Re: IP Filter
HI, You need to use following java code and servlet code to get IP Filter.
Code:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
public class IPFilterExample implements Filter
{
public IPFilterExample() {}
public final static String IP = "198.168.20.1";
private FilterConfig filterConfig;
public void init(FilterConfig config) throws ServletException
{
this.filterConfig = config;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>IP Filter Example</title></head>");
String userip = request.getRemoteAddr();
HttpServletResponse httpResponse = null;
if (response instanceof HttpServletResponse)
{
httpResponse = (HttpServletResponse) response;
}
if (IP.equals(userip))
{
httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN,"You are not
allowed to access the servlet!");
}
else
{
filterchain.doFilter(request, response);
out.println("<body><h3><font color='green'>Passed successfully from IP
Filter<font></h3></body></html>");
}
}
public void destroy() {}
}
Servlet code:
Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CallIpFilter extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>IP Filter</title></title>");
pw.println("<body>");
pw.println("<h1>Servlet called successfully</h1>");
pw.println("</body></html>");
}
}
Page generated in 1,714,144,411.40550 seconds with 11 queries