前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊dubbo-go的ConsumerSignFilter

聊聊dubbo-go的ConsumerSignFilter

原创
作者头像
code4it
修改2020-07-24 10:04:21
3660
修改2020-07-24 10:04:21
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下dubbo-go的ConsumerSignFilter

ConsumerSignFilter

dubbo-go-v1.4.2/filter/filter_impl/auth/consumer_sign.go

代码语言:javascript
复制
type ConsumerSignFilter struct {
}
​
func init() {
    extension.SetFilter(constant.CONSUMER_SIGN_FILTER, getConsumerSignFilter)
}
  • ConsumerSignFilter的init方法设置了getConsumerSignFilter

getConsumerSignFilter

dubbo-go-v1.4.2/filter/filter_impl/auth/consumer_sign.go

代码语言:javascript
复制
func getConsumerSignFilter() filter.Filter {
    return &ConsumerSignFilter{}
}
  • getConsumerSignFilter创建了ConsumerSignFilter

Invoke

dubbo-go-v1.4.2/filter/filter_impl/auth/consumer_sign.go

代码语言:javascript
复制
func (csf *ConsumerSignFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
    logger.Infof("invoking ConsumerSign filter.")
    url := invoker.GetUrl()
​
    err := doAuthWork(&url, func(authenticator filter.Authenticator) error {
        return authenticator.Sign(invocation, &url)
    })
    if err != nil {
        panic(fmt.Sprintf("Sign for invocation %s # %s failed", url.ServiceKey(), invocation.MethodName()))
​
    }
    return invoker.Invoke(ctx, invocation)
}
  • Invoke方法会先执行doAuthWork方法,其传递的func执行authenticator.Sign(invocation, &url)

OnResponse

dubbo-go-v1.4.2/filter/filter_impl/auth/consumer_sign.go

代码语言:javascript
复制
func (csf *ConsumerSignFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
    return result
}
  • OnResponse方法直接返回result

doAuthWork

filter/filter_impl/auth/default_authenticator.go

代码语言:javascript
复制
func doAuthWork(url *common.URL, do func(filter.Authenticator) error) error {
​
    shouldAuth := url.GetParamBool(constant.SERVICE_AUTH_KEY, false)
    if shouldAuth {
        authenticator := extension.GetAuthenticator(url.GetParam(constant.AUTHENTICATOR_KEY, constant.DEFAULT_AUTHENTICATOR))
        return do(authenticator)
    }
    return nil
}
  • doAuthWork方法先从url读取constant.SERVICE_AUTH_KEY判断是否需要auth,需要的话,则获取authenticator,执行do(authenticator)

Sign

dubbo-go-v1.4.2/filter/filter_impl/auth/default_authenticator.go

代码语言:javascript
复制
func (authenticator *DefaultAuthenticator) Sign(invocation protocol.Invocation, url *common.URL) error {
    currentTimeMillis := strconv.Itoa(int(time.Now().Unix() * 1000))
​
    consumer := url.GetParam(constant.APPLICATION_KEY, "")
    accessKeyPair, err := getAccessKeyPair(invocation, url)
    if err != nil {
        return errors.New("get accesskey pair failed, cause: " + err.Error())
    }
    inv := invocation.(*invocation_impl.RPCInvocation)
    signature, err := getSignature(url, invocation, accessKeyPair.SecretKey, currentTimeMillis)
    if err != nil {
        return err
    }
    inv.SetAttachments(constant.REQUEST_SIGNATURE_KEY, signature)
    inv.SetAttachments(constant.REQUEST_TIMESTAMP_KEY, currentTimeMillis)
    inv.SetAttachments(constant.AK_KEY, accessKeyPair.AccessKey)
    inv.SetAttachments(constant.CONSUMER, consumer)
    return nil
}
​
func getAccessKeyPair(invocation protocol.Invocation, url *common.URL) (*filter.AccessKeyPair, error) {
    accesskeyStorage := extension.GetAccesskeyStorages(url.GetParam(constant.ACCESS_KEY_STORAGE_KEY, constant.DEFAULT_ACCESS_KEY_STORAGE))
    accessKeyPair := accesskeyStorage.GetAccessKeyPair(invocation, url)
    if accessKeyPair == nil || IsEmpty(accessKeyPair.AccessKey, false) || IsEmpty(accessKeyPair.SecretKey, true) {
        return nil, errors.New("accessKeyId or secretAccessKey not found")
    } else {
        return accessKeyPair, nil
    }
}
  • Sign方法通过getAccessKeyPair从accesskeyStorage.GetAccessKeyPair获取accessKeyPair,然后通过getSignature计算signature,没有err则设置到attachment中

小结

ConsumerSignFilter的Invoke方法会先执行doAuthWork方法,其传递的func执行authenticator.Sign(invocation, &url)

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ConsumerSignFilter
  • getConsumerSignFilter
  • Invoke
  • OnResponse
  • doAuthWork
  • Sign
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档