dubbo filter实现接口认证springboot idea

最近公司有业务需求,要对Dubbo接口调用者进行身份验证,验证通过才能调用,网上一些资料不够全面,遂整理了一下.

 

provider方定义一个filter,需要实现com.alibaba.dubbo.rpc.Filter(需要引入com.alibaba.dubbo依赖)

 

public class AuthorizationFilter implements Filter {

    @Override

    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        String token = invocation.getAttachment("token");

        if("123456".equals(token)){

            return invoker.invoke(invocation);
        }

        Return new RpcResult();
    }
}

resources目录META-INF/dubbo下新建text文件com.alibaba.dubbo.rpc.Filter,内容如下:

AuthorizationFilter = com.xxx.AuthorizationFilter

修改dubboprovider配置文件,添加如下:

<dubbo:provider filter="AuthorizationFilter" />  

consumer方采用隐式传参:RpcContext.getContext().setAttachment("token","123456");

posted @ 2018-02-28 19:44  枫树湾河桥  阅读(2225)  评论(0编辑  收藏  举报
Live2D