We often need to use IP whitelist, IP blacklist. Netty itself has helped me implement a set of validation mechanisms that provide the Ipfilterrulehandler class
public class Ipfilterrulehandler extends Ipfilteringhandlerimpl
Public abstract class Ipfilteringhandlerimpl implements Channelupstreamhandler, Ipfilteringhandler
This class is inherited from Channelupstreamhandler as much as the decoder (decoder) We use and the logic processing handler, so it can be easily added to our channelpipeline. For example:
ChannelpipEline p = channels.pipeline ()//ip filter Ipfilterrulehandler Ipfilterrulehandler = new Ipfilterrulehandler (); Ipfilterrulehandler.addall (New Ipfilterrulelist ("+i:192.168.*" + ",-i:*"));p. AddLast (" IpFilter ", Ipfilterrulehandler);
Netty IP Filtering offers a total of 3 filtering: [I,n,c]
i corresponds to an IP address, the corresponding +i means allow (allowed),-I means deny (deny)
n corresponds to the address name, the corresponding +n means allow (allowed),-n denotes deny (Deny)
c corresponds to CIDR (Classless inter-domain Routing) non-categorical domain routing by Selection , corresponding +c means allow (allowed),- c = Deny (Deny)
Official examples:
Package Org.jboss.netty.handler.ipfilter;import Java.net.inetaddress;import Java.net.inetsocketaddress;public Class Ipfilterruletest {public static Boolean accept (Ipfilterrulehandler h, inetsocketaddress addr) throws Exception { return h.accept (null, NULL, addr);} public static void Main (string[] args) throws Exception {Ipfilterrulehandler h = new Ipfilterrulehandler (); H.addall (New Ip Filterrulelist ("+n:localhost,-n:*")) inetsocketaddress addr = new Inetsocketaddress (Inetaddress.getlocalhost (), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname ("127.0.0.2"), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname (). GetHostName ()), 8080); System.out.println (Accept (H, addr)), H.clear (); H.addall (New Ipfilterrulelist ("+n:*" + inetaddress.getlocalhost (). GetHostName (). substring (1) + ",-n:*")); addr = new Inetsocketaddress (Inetaddress.getlocalhost (), 8080); System.out.println (Accept (H, addr)); addr= New Inetsocketaddress (Inetaddress.getbyname ("127.0.0.2"), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname (). GetHostName ()), 8080); System.out.println (Accept (H, addr)), H.clear (); H.addall (New Ipfilterrulelist ("+c:" + inetaddress.getlocalhost (). Gethostaddress () + "/32,-n:*"); addr = new Inetsocketaddress (Inetaddress.getlocalhost (), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname ("127.0.0.2"), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname (). GetHostName ()), 8080); System.out.println (Accept (H, addr)); H.clear (); H.addall (New Ipfilterrulelist ("")); addr = new Inetsocketaddress ( Inetaddress.getlocalhost (), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname ("127.0.0.2"), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname (inetaddress. Getlocalhost (). GetHostName ()), 8080); System.out.println (Accept (H, addr)); H.clear (); addr = new Inetsocketaddress (Inetaddress.getlocalhost (), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname ("127.0.0.2"), 8080); System.out.println (Accept (H, addr)), addr = new Inetsocketaddress (Inetaddress.getbyname (). GetHostName ()), 8080); System.out.println (Accept (H, addr));}}
CIDR Reference: http://blog.csdn.net/yaoyao4959/article/details/10084973
IP Filtering for Netty