美文网首页dubbo
dubbo应用笔记(1)dubbo基于Filter的接口访问权鉴

dubbo应用笔记(1)dubbo基于Filter的接口访问权鉴

作者: 桥头放牛娃 | 来源:发表于2019-02-14 14:45 被阅读84次

    Dubbo中的Filter实现是专门为服务提供方和服务消费方调用过程进行拦截,Dubbo本身的大多功能均基于此扩展点实现,每次远程方法执行,该拦截都会被执行。而在一些应用场景中,因为权限或接口调用监控或调用统计等原因,需要对接口调用方进行权鉴处理。而我们可以用Filter实现权鉴。

    关于filter调用链加载请参考:
    https://www.jianshu.com/p/1276015b479f

    关于Filter配置请参考:
    https://www.jianshu.com/p/a8fc21e7c8db

    1、服务端Filter实现

    public class ProviderRpcFilter implements Filter{
    
        private static Logger logger = LoggerFactory.getLogger(ProviderRpcFilter.class);
    
        @Override
        public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
            logger.info("className:{}", invocation.getClass().getName());
            logger.info("methodName:{}", invocation.getMethodName());
    
            String appCode = RpcContext.getContext().getAttachment("appCode");
            String secretKey = RpcContext.getContext().getAttachment("secretKey");
    
            if(Strings.isEmpty(appCode) || Strings.isEmpty(secretKey)){
                throw new RpcException("Sorry, your access is denied!");
            }
    
            logger.info("appCode:{}", appCode);
    
            if(!appCode.equals("zhaozhou11") || !secretKey.equals("666666")){
                throw new RpcException("your appCode or secretKey is error!");
            }
    
            return invoker.invoke(invocation);
        }
    }
    

    服务端的Filter主要获取RpcContext中的附带参数:appCode(应用编码)和secretKey(访问秘钥),通过验证这两个参数验证客户端是否有权限访问此接口。

    2、客户端Filter实现

    public class ConsumerRpcFilter implements Filter {
    
        private static Logger logger = LoggerFactory.getLogger(ConsumerRpcFilter.class);
    
        @Override
        public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
            logger.info("className:{}", invocation.getClass().getName());
            logger.info("methodName:{}", invocation.getMethodName());
            String appCode = invoker.getUrl().getParameter("appCode");
            String secretKey = invoker.getUrl().getParameter("secretKey");
    
            RpcContext.getContext().setAttachment("appCode",appCode);
            RpcContext.getContext().setAttachment("secretKey", secretKey);
            return invoker.invoke(invocation);
        }
    }
    

    客户端的Filter主要是获取XML配置appCode及secretKey,并将这连个参数放到RpcContext的Attachment中,随接口调用传给服务端。

    3、META-INF.dubbo中Filter配置

    Filter的配置目录:

    Filter的配置目录.png

    Filter的配置:

    acessFilter=dubbo.demo.filter.ProviderRpcFilter
    consumerFilter=dubbo.demo.filter.ConsumerRpcFilter
    

    4、服务端配置

    <?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://dubbo.apache.org/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="privider-api-test"  />
    
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry id="zookeeperRegistry" protocol="zookeeper" address="zookeeper://127.0.0.1:2181" check="false" />
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
    
    
        <dubbo:provider filter="acessFilter" ></dubbo:provider>
    
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service id="dubboTestApi" interface="dubbo.demo.api.DubboTestApi" ref="dubboTestApiImpl" cache="false" />
    
        <!-- 和本地bean一样实现服务 -->
        <bean id="dubboTestApiImpl" class="dubbo.demo.api.DubboTestApiImpl" />
    </beans>
    

    <dubbo:provider filter>配置访问提供者的Filter,此处的accessFilter是在META-INO.dubbo中配置的过滤器。

    5、客户端配置

    <?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://dubbo.apache.org/schema/dubbo" xmlns:dabbo="http://dubbo.apache.org/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
        <dubbo:application name="consumer-api-test"  />
    
        <!-- 使用zookeeper注册中心暴露发现服务地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    
        <dabbo:consumer filter="consumerFilter">
            <dubbo:parameter key="appCode" value="zhaozhou11" />
            <dubbo:parameter key="secretKey" value="666666" />
        </dabbo:consumer>
    
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="dubboTestApi" interface="dubbo.demo.api.DubboTestApi" />
    
    </beans>
    

    此处通过<dabbo:consumer filter>统一配置客户端的过滤器,consumerFilter是在META-INO.dubbo中配置的过滤器。

    6、输出

    1.5.1、服务端Filter日志输出

    服务端Filter日志输出.png

    1.5.2、客户端Filter及调用日志输出

    客户端Filter及调用日志输出.png

    1.5.3、当客户端appCode配置错误时的异常日志

    当客户端appCode配置错误时的异常日志.png

    示例源码:https://github.com/zhaozhou11/dubbo-demo.git

    相关文章

      网友评论

        本文标题:dubbo应用笔记(1)dubbo基于Filter的接口访问权鉴

        本文链接:https://www.haomeiwen.com/subject/arseeqtx.html