Results 1 to 4 of 4

Thread: IP Filter

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    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()
     {   
      }
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    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>");
      }
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,649,966.30321 seconds with 17 queries