认证
Node-RED的管理API是通过您的settings.js文件中的adminAuth属性进行安全验证的。安全性部分描述了该属性应如何配置。
如果该属性未设置,任何有网络访问权限的人都可以访问Node-RED的管理API。
步骤 0 - 检查认证方案
对/auth/login进行HTTP GET请求将返回当前的认证方案。
curl 示例:
curl http://localhost:1880/auth/login在API的当前版本中,有两种可能的结果:
- 无活跃认证
{}所有API请求都可以在不提供任何进一步认证信息的情况下进行。
- 基于凭证的认证
{
  "type": "credentials",
  "prompts": [
    {
      "id": "username",
      "type": "text",
      "label": "用户名"
    },
    {
      "id": "password",
      "type": "password",
      "label": "密码"
    }
  ]
}API由访问令牌保护。
步骤 1 - 获取访问令牌
对/auth/token进行HTTP POST请求,用于将用户凭证换取访问令牌。
必须提供以下参数:
- client_id - 标识客户端。目前,必须是node-red-admin或node-red-editor。
- grant_type - 必须是password。
- scope - 请求的权限的空格分隔列表。目前,必须是*或read。
- username - 用于认证的用户名。
- password - 用于认证的密码。
curl 示例:
curl http://localhost:1880/auth/token --data 'client_id=node-red-admin&grant_type=password&scope=*&username=admin&password=password'如果成功,响应将包含访问令牌:
{
  "access_token": "A_SECRET_TOKEN",
  "expires_in": 604800,
  "token_type": "Bearer"
}步骤 2 - 使用访问令牌
所有后续的API调用都应在头部提供此令牌Authorization。
curl 示例:
curl -H "Authorization: Bearer A_SECRET_TOKEN" http://localhost:1880/settings撤销令牌
当不再需要令牌时,为了撤销它,应该在一个HTTP POST中发送到/auth/revoke。
curl 示例:
curl --data 'token=A_SECRET_TOKEN' -H "Authorization: Bearer A_SECRET_TOKEN" http://localhost:1880/auth/revoke 
            
 
                                 
                                 
                                 
                        