gRPC 安全篇-1: 快速配置服务端 CORS
2023-04-18 13:16:19 时间
介绍
本文介绍如何通过 rk-boot 快速配置服务端 CORS。
什么是 CORS? 跨源资源共享 (CORS) (或通俗地译为跨域资源共享)是一种基于HTTP 头的机制,该机制通过允许服务器标示除了它自己以外的其它origin(域,协议和端口),这样浏览器可以访问加载这些资源。
请访问如下地址获取完整教程:
安装
go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-grpc
快速开始
gRPC 协议本身是没有跨域(CORS)问题的,因为无法从浏览器里直接发送 HTTP 请求给 gRPC。
rk-boot 默认会为 gRPC 服务开启 grpc-gateway,两个协议监听同一个端口。
当 gRPC 开启 grpc-gateway 提供 Restful 请求的时候,我们就需要在服务端配置 CORS。
1.创建 boot.yaml
boot.yaml 文件会告诉 rk-boot 如何启动 gRPC 服务。
在这个例子中,我们只允许 localhost:8080 发送过来的请求,通过验证。
同时,我们开启了 commonService,commonService 提供了 /rk/v1/healthy 等默认 API。详情
---
grpc:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
commonService:
enabled: true # Optional, default: false
interceptors:
cors:
enabled: true # Optional, default: false
# Accept all origins from localhost
allowOrigins:
- "http://localhost:8080" # Optional, default: *
2.创建 main.go
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
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.创建 cors.html
让我们创建一个简单网页,网页里会调用 localhost:8080/rk/v1/healthy,我们通过返回结果来验证 CORS。
<!DOCTYPE html>
<html>
<body>
<h1>CORS Test</h1>
<p>Call http://localhost:8080/rk/v1/healthy</p>
<script type="text/javascript">
window.onload = function() {
var apiUrl = 'http://localhost:8080/rk/v1/healthy';
fetch(apiUrl).then(response => response.json()).then(data => {
document.getElementById("res").innerHTML = data["healthy"]
}).catch(err => {
document.getElementById("res").innerHTML = err
});
};
</script>
<h4>Response: </h4>
<p id="res"></p>
</body>
</html>
4.文件夹结构
.
├── boot.yaml
├── cors.html
├── go.mod
├── go.sum
└── main.go
0 directories, 5 files
5.验证
打开 cors.html,cors.html 会从非 8080 端口发送请求,所以无法通过验证。
6.给 CORS 添加白名单
这次,我们把 allowOrigins 配置成 localhost:*,即,让所有从 localhost 发送的请求,均可以通过。
---
grpc:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
commonService:
enabled: true # Optional, default: false
interceptors:
cors:
enabled: true # Optional, default: false
# Accept all origins from localhost
allowOrigins:
- "http://localhost:*" # Optional, default: *
7.验证
完整参数
请参考 grpc-cors 获取完整参数列表。
相关文章
- 终结者最强大脑!谷歌发布史上最大「通才」模型PaLM-E,5620亿参数,看图说话还能操控机器人
- IDC 预测:2023年全球AI系统支出将达1540亿美元
- Google Cloud发布三款新电信产品 瞄准云原生网络转型
- 超有梗!ChatGPT版必应戏精上身,下一秒就和小扎、马斯克在线聊天
- ChatGPT版佛祖爆火出圈:施主,说出你的烦恼
- 无所不在的AI为影视制作带来新变革
- 为什么我会离开谷歌AI而选择区块链
- Marvell与亚马逊云科技合作实现云优先芯片设计
- 看ChatGPT这形势,留给我们开发人员的时间不多了
- 乌克兰Raiffeisen银行的CTO谈战时的云迁移
- 强生集团的智能自动化之旅
- 云计算现代化将成为整体的方法
- 被谷歌开除的工程师再次警告:AI有情绪,它是原子弹之后最强的科技
- 能耗优势显著!类器官智能(OI)或将超越人工智能(AI)
- Kubernetes向集群外部暴露服务的方式,你知道吗?
- 力挺ChatGPT,黄仁勋押注AI十年获得丰厚回报
- 让"穷学生"更多了解AI,微软谷歌支持的教育组织发力
- 一文弄清混合云架构模式
- GKE 安全性:保护集群的 10 大策略
- 如何使用Azure Bot Services创建聊天机器人的分步说明