netty的ip过滤

本文介绍了Netty中实现IP过滤的方法,通过继承ChannelUpstreamHandler并使用不同的过滤规则(如+i, -i, +n, -n, +c, -c)来允许或拒绝特定IP、地址名称和CIDR。示例代码展示了如何创建和添加IpSubnetFilterRule到ChannelPipeline,以在Netty4中实现基于规则的IP过滤。" 8939517,1003392,Kettle命令行调用参数详解及定时任务设置,"['数据抽取', '数据转换', '数据加载', '命令行工具', '任务调度']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们经常需要用到ip白名单,ip黑名单。netty本身就帮我实现了一套验证机制,提供了IpFilterRuleHandler类
 
1
public class IpFilterRuleHandler extends IpFilteringHandlerImpl
1
public abstract class IpFilteringHandlerImpl implements ChannelUpstreamHandler, IpFilteringHandler

 

该类和我们经常使用的解码器(decoder)以及逻辑处理handler一样都继承于ChannelUpstreamHandler,所以可以很方便的把它加入到我们的ChannelPipeline中。

例如:

1
2
3
4
5
ChannelPip<span></span>eline p = Channels.pipeline();
//ip过滤
IpFilterRuleHandler ipFilterRuleHandler = new IpFilterRuleHandler();
ipFilterRuleHandler.addAll( new IpFilterRuleList( "+i:192.168.*" + ", -i:*" ));
p.addLast( "ipFilter" , ipFilterRuleHandler);

netty的ip过滤一共提供3中过滤:[i,n,c]

i对应的是ip地址,相应的 +i 表示allow(允许),-i 表示deny(否认)

n对应的是地址名称,相应的 +n 表示allow(允许),-n 表示deny(否认) 

c对应的是CIDR (Classless Inter-Domain Routing)无分类域间路由选择,相应的 +c 表示allow(允许),-c表示deny(否认)

官方中实例:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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 IpFilterRuleList( "+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(InetAddress
                .getLocalHost().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(InetAddress
                .getLocalHost().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(InetAddress
                .getLocalHost().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(InetAddress
                .getLocalHost().getHostName()), 8080 );
        System.out.println(accept(h, addr));
    }
}



CIDR参考:http://blog.youkuaiyun.com/yaoyao4959/article/details/10084973

 

我在Netty4中用的是IpSubnetFilterRule来过滤对应的IP

看我的代码实例:

ChannelPipeline pipeline = ch.pipeline();
       
        String[] ip = Server.getServerParam().getSlbAddress();
        int count = ip.length;
        IpSubnetFilterRule[] ipsf = new IpSubnetFilterRule[count];
        for( int i=0;i<count;i++){
         ipsf[i] = new IpSubnetFilterRule(ip[i],16,IpFilterRuleType.REJECT);
        }
        pipeline.addLast(new RuleBasedIpFilter(ipsf));


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值