前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gRPC: 如何在 gRPC 服务中添加 HTTP 基本验证?

gRPC: 如何在 gRPC 服务中添加 HTTP 基本验证?

原创
作者头像
尹东勋
修改2021-12-13 00:14:35
2.7K0
修改2021-12-13 00:14:35
举报
文章被收录于专栏:开源 & 技术分享

介绍

本文将介绍如何在 gRPC 微服务中添加 API Auth。我们将介绍 Basic Auth,X-API-Key 两种 API Auth 模式。

我们将会使用 rk-boot 来启动 gRPC 服务。

请访问如下地址获取完整教程:https://rkdev.info/cn https://rkdocs.netlify.app/cn (备用)

安装

代码语言:txt
复制
go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-grpc

快速开始

详细文档可参考:

1.创建 boot.yaml

为了验证,我们启动了 commonService,commonService 里包含了一系列常用 API,例如 /rk/v1/healthy,并且启动了 sw 来提供 Swagger UI。

这一步,我们启动 Basic Auth,用户名密码为 user:pass。

代码语言:txt
复制
---
grpc:
  - name: greeter
    port: 8080
    enabled: true
    commonService:
      enabled: true          # Enable common service for testing
    sw:
      enabled: true
    interceptors:
      auth:
        enabled: true        # Enable auth interceptor/middleware
        basic: ["user:pass"] # Enable basic auth

2.创建 main.go

代码语言:txt
复制
package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
	_ "github.com/rookie-ninja/rk-grpc/boot"
)

// Application entrance.
func main() {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Bootstrap
	boot.Bootstrap(context.Background())

	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}

3.启动 main.go

代码语言:txt
复制
$ go run main.go

4.验证

在不提供 Basic Auth 的情况下,我们得到了 401 错误码。

代码语言:txt
复制
$ curl  -X GET localhost:8080/rk/v1/healthy
# This is RK style error code if unauthorized
{
    "error":{
        "code":401,
        "status":"Unauthorized",
        "message":"Missing authorization, provide one of bellow auth header:[Basic Auth]",
        "details":[
            {
                "code":16,
                "status":"Unauthenticated",
                "message":"[from-grpc] Missing authorization, provide one of bellow auth header:[Basic Auth]"
            }
        ]
    }
}

用 Swagger UI 试一下。访问 http://localhost:8080/sw,直接发送请求,我们依然会得到 401 错误。

提供 Basic Auth,出于安全考虑,Request Header 里的 Auth 需要用 Base64 进行编码。我们对 user:pass 字符串进行了 Base64 编码。

代码语言:txt
复制
$ curl localhost:8080/rk/v1/healthy -H "Authorization: Basic dXNlcjpwYXNz"
{
    "healthy":true
}

在 Swagger UI 中,点击【锁】按钮,添加 Basic Auth。

使用 X-API-Key 授权模式

1.修改 boot.yaml

为了验证,我们启动了 commonService,commonService 里包含了一系列常用 API,例如 /rk/v1/healthy,并且启动了 sw 来提供 Swagger UI。

注意!

grpc-gateway 默认是不会把 X-API-Key header 传到 gRPC 服务侧的,因此,我们需要启动 enableRkGwOption,这个选项能让 X-API-Key header 传到 gRPC 后端。

这一步,我们启动 X-API-Key,key 的值为 token。

代码语言:txt
复制
---
grpc:
  - name: greeter
    port: 8080
    enabled: true
    enableRkGwOption: true   # Enable rk style grpc-gateway option
    commonService:
      enabled: true          # Enable common service for testing
    sw:
      enabled: true
    interceptors:
      auth:
        enabled: true        # Enable auth interceptor/middleware
        apiKey: ["token"]    # Enable X-API-Key auth

2.启动 main.go

代码语言:txt
复制
$ go run main.go

3.验证

同样的情况,在不提供 X-API-Key 的情况下,我们得到了 401 错误码。

代码语言:txt
复制
$ curl  -X GET localhost:8080/rk/v1/healthy
# This is RK style error code if unauthorized
{
  "code": 16,
  "message": "Missing authorization, provide one of bellow auth header:[X-API-Key]",
  "details": [
    {
      "@type": "type.googleapis.com/rk.api.v1.ErrorDetail",
      "code": 16,
      "status": "Unauthenticated",
      "message": "[from-grpc] Missing authorization, provide one of bellow auth header:[X-API-Key]"
    }
  ]
}
代码语言:txt
复制
$ curl localhost:8080/rk/v1/healthy -H "X-API-Key: token"
{
    "healthy":true
}

忽略请求路径

我们可以添加一系列 API 请求路径,让后段忽略授权这些 API 请求。

注意!

这里要写 gRPC 的路径,而不是 Restful API 的路径。

代码语言:txt
复制
---
grpc:
  - name: greeter
    port: 8080
    enabled: true
    commonService:
      enabled: true          # Enable common service for testing
    sw:
      enabled: true
    interceptors:
      auth:
        enabled: true        # Enable auth interceptor/middleware
        basic: ["user:pass"] # Enable basic auth
        ignorePrefix: ["/rk.api.v1.RkCommonService/Healthy"]  # Ignoring path with prefix

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 安装
  • 快速开始
    • 1.创建 boot.yaml
      • 2.创建 main.go
        • 3.启动 main.go
          • 4.验证
          • 使用 X-API-Key 授权模式
            • 1.修改 boot.yaml
              • 2.启动 main.go
                • 3.验证
                • 忽略请求路径
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档