通过您自己的 HTTP API 发送 SMS
本指南介绍如何通过没有内置类型的 SMS 网关发送 Casdoor 的验证码。Casdoor 调用你描述的 HTTP 端点,因此任何接受普通 HTTP 请求的 SMS API 均可工作。
学习目标
- 在 Casdoor 中描述你的 SMS 网关的 HTTP 请求。
- 了解 Casdoor 如何构建请求。
所需条件
- 具有 HTTP API 的 SMS 网关
- Casdoor 管理控制台的管理员访问权限
在 Casdoor 中添加提供商
-
在 Casdoor 管理控制台中,前往 Identity > Providers 并添加一个提供商。
-
将 Category 设置为
SMS,将 Type 设置为Custom HTTP SMS。 -
填写以下字段:
字段 含义 必需 模板代码 消息模板。验证码会替换 %s占位符(例如Your code is %s)。留空则直接发送验证码本身。不 端点 短信接口地址。支持 {mobile}和{code}占位符,会被替换为手机号和验证码。是 方法 HTTP 方法: GET、POST、PUT或DELETE。是 内容类型 非 GET方法的请求体编码:application/x-www-form-urlencoded(默认)或application/json。不 HTTP标头 额外的请求头(例如携带 API key 的 Authorization头)。不 HTTP body映射 请求中 phoneNumber和content所用的字段名(非GET方法)。不 参数 承载消息内容的字段名。优先于 HTTP body mapping 中的 content值。不 启用代理 通过 Casdoor 配置的 SOCKS5 代理发送请求。见 概览。 不 -
保存提供商.
Casdoor 如何构建请求
- Content: 消息是 Template code,其中
%s被替换为验证码。如果 Template code 为空,Casdoor 仅发送验证码。 - Field names: 电话号码放在
phoneNumber字段中,内容放在由 Parameter 指定的字段中。你可以在 HTTP body mapping 中重命名这两个字段。如果设置了 Parameter,它将优先于content的映射。 POST,PUT, andDELETE: Casdoor 以选定的 Content type 在正文中发送电话号码和内容。GET: Casdoor 将电话号码和内容作为查询参数追加。如果 Endpoint 包含占位符{mobile}或{code},Casdoor 会填充这些占位符且不添加查询参数。
示例
接受包含 to 和 text 字段的 POST 表单的网关:
| Field | Value |
|---|---|
| Endpoint | https://sms.example.com/api/send |
| Method | POST |
| Content type | application/x-www-form-urlencoded |
| HTTP header | Authorization: Bearer <your-token> |
| HTTP body mapping | phoneNumber to to |
| Parameter | text |
| Template code | Your verification code is %s |
通过 GET 将所有信息放在 URL 中的网关:
| Field | Value |
|---|---|
| Endpoint | https://sms.example.com/send?mobile={mobile}&code={code} |
| Method | GET |
验证结果
在 SMS Test 中输入手机号码并发送测试短信。