为dubbo接口增加IP白名单

在开发dubbo接口时,有时可能会限制接口的访问,ip白名单即是一种。在dubbo中,通过扩展Filter接口,可以实现IP白名单的功能。

先定义一个配置IP白名单的bean

/**
 * Created by j.tommy on 2017/11/4.
 */
public class IPWhiteList {
    private boolean isEnabled; // 是否启用白名单
    private List<String> allowIps; // 允许的白名单列表
    public boolean isEnabled() {
        return isEnabled;
    }
    public void setEnabled(boolean isEnabled) {
        this.isEnabled = isEnabled;
    }
    public List<String> getAllowIps() {
        return allowIps;
    }
    public void setAllowIps(List<String> allowIps) {
        this.allowIps = allowIps;
    }
}

然后我们实现dubbo的Filter接口

/**
 * Created by j.tommy on 2017/11/4.
 */
public class IPWhiteListFilter implements Filter{
    private IPWhiteList ipWhiteList;
    public void setIpWhiteList(IPWhiteList ipWhiteList) {
        this.ipWhiteList = ipWhiteList;
    }
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        if (!ipWhiteList.isEnabled()) {
            System.out.println("dubbo IP白名单被禁用!");
            return invoker.invoke(invocation);
        }
        String clientIp = RpcContext.getContext().getRemoteHost();
        if (ipWhiteList.getAllowIps().contains(clientIp)) {
            return invoker.invoke(invocation);
        }
        System.out.println("dubbo客户端IP[" + clientIp + "]不在白名单,禁止调用!");
        return new RpcResult();
    }
}

在/resources目录下,新建META-INF/dubbo目录,并新建一个名为com.alibaba.dubbo.rpc.Filter的文本文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CFXXVRoq-1661553422394)(http://omh46px9n.bkt.clouddn.com/17-11-4/32927644.jpg)]
内容如下:

ipWhiteListFilter=com.tommy.service.provider.filter.IPWhiteListFilter

dubbo的配置文件中增加filter的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="demo-provider"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:provider filter="ipWhiteListFilter"/>
    <dubbo:service interface="com.tommy.service.DemoService" ref="demoService"/>
    <bean id="demoService" class="com.tommy.service.provider.DemoServiceImpl"/>
    <bean id="ipWhiteList" class="com.tommy.service.provider.bean.IPWhiteList">
        <property name="enabled" value="true"/>
        <property name="allowIps">
            <list>
                <value>127.0.0.1</value>
                <value>192.168.71.170</value>
            </list>
        </property>
    </bean>
</beans>

参考:http://blog.csdn.net/mj158518/article/details/47379799

)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值