Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



IP Filter

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 07-12-2009
Member
 
Join Date: Nov 2009
Posts: 712
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.

Reply With Quote
  #2  
Old 07-12-2009
Member
 
Join Date: May 2008
Posts: 2,008
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()
 {   
  }
}
Reply With Quote
  #3  
Old 07-12-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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.
Reply With Quote
  #4  
Old 07-12-2009
Member
 
Join Date: May 2008
Posts: 2,293
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>");
  }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "IP Filter"
Thread Thread Starter Forum Replies Last Post
How to setup IP Filter & MAC filter on Huawei MT841? Eas!war Networking & Security 1 04-03-2012 04:06 PM
Scene filter GUI on vlc Alfiee Windows Software 5 09-07-2011 11:19 AM
What is a PHP Filter? Beter 2 Burn Out Software Development 4 23-02-2010 05:50 AM
Filter Tag In Html technika Software Development 5 01-02-2010 04:41 PM
Picollator filter : Automatic adult content filter monsitj Windows Software 1 29-01-2009 07:07 PM


All times are GMT +5.5. The time now is 03:11 AM.