英文:
How to perform authentication of RPC server&client by HTTP in Go?
问题
我是你的中文翻译助手,以下是翻译好的内容:
我对GO语言还不熟悉,正在按照https://parthdesai.me/articles/2016/05/20/go-rpc-server/上的指南构建一个简单的RPC服务器和客户端。在这篇文章中,它说:
这种方法(HTTP)的好处是,您可以在允许RPC之前,使用HTTP支持的任何身份验证方法轻松对客户端进行身份验证。
但是页面上的示例似乎没有执行这个操作。我在StackOverflow上搜索到了https://stackoverflow.com/questions/50621335/passing-authentication-details-with-a-json-rpc-call,其中提到:
有两种方法可以实现你想要的功能:要么实现一个支持HTTP的
io.ReadWriteCloser
,并像你的示例中那样使用它,要么实现一个执行HTTP基本身份验证的rpc.ClientCodec
,并与rpc.NewClientWithCodec
一起使用。
然而,我仍然不知道如何做到这一点。我可以给你一些示例代码(可能是基本身份验证
方法)吗?
英文:
I'm new to GO and I'm following https://parthdesai.me/articles/2016/05/20/go-rpc-server/ to build a simple RPC server&client. In this article, it says
> Benefit of this approach (HTTP) is, you can perform authentication of client easily, before allowing RPC, using any authentication method supported by HTTP.
But the example on the page doesn't seem to perform it. I have searched it on StackOverflow, and found https://stackoverflow.com/questions/50621335/passing-authentication-details-with-a-json-rpc-call saying
> There's two ways to accomplish what you want: either implement an HTTP-speaking io.ReadWriteCloser
and use as in your example or implement an rpc.ClientCodec
that does the HTTP basic auth and use in conjunction with rpc.NewClientWithCodec
.
However, I still don't know how to do it. Could I have some example code (may be Basic Authentication
method)?
答案1
得分: 1
在使用gRPC网关的情况下,使用转码和检查身份验证的gRPC中间件对于HTTP服务器优于gRPC。
要在标头中获取授权,请使用gRPC中间件并从gRPC MD中的上下文中获取。
srv := grpc.NewServer(exampleJwtMiddleware())
func exampleJwtMiddleware() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
token, err := extractHeaderFromContext(ctx, "Authorization")
// 做一些操作...
return handler(ctx, req)
}
}
func extractHeaderFromContext(ctx context.Context, header string) ([]string, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, ERROR_NO_HEADER_IN_REQUEST
}
foundedHeaders, ok := md[header]
if !ok {
return nil, ERROR_NO_HEADER_IN_REQUEST
}
return foundedHeaders, nil
}
英文:
Using transcoding and checking authentication in gRPC middleware is better for http server over gRPC using gRPC gateway.
https://cloud.google.com/endpoints/docs/grpc/transcoding
To get authorization in headers, use gRPC middleware and get from context with gRPC MD.
srv := grpc.NewServer(exampleJwtMiddleware())
func exampleJwtMiddleware() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
token, err := extractHeaderFromContext(ctx, "Authorization")
// do sometings...
return handler(ctx, req)
}
}
func extractHeaderFromContext(ctx context.Context, header string) ([]string, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, ERROR_NO_HEADER_IN_REQUEST
}
foundedHeaders, ok := md[header]
if !ok {
return nil, ERROR_NO_HEADER_IN_REQUEST
}
return foundedHeaders, nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论