如何使用Authing和AWS JWT Authorizer替换Cognito

发布时间:2022-01-12 09:23:44 作者:iii
来源:亿速云 阅读:172
# 如何使用Authing和AWS JWT Authorizer替换Cognito

## 引言

随着企业上云进程加速,身份认证服务成为现代应用架构的核心组件。Amazon Cognito作为AWS原生解决方案虽功能全面,但存在成本高、灵活性不足等问题。本文将详细介绍如何通过国产身份云服务Authing结合AWS JWT Authorizer实现Cognito的替代方案,在保证安全性的同时获得更高性价比和定制化能力。

---

## 一、为什么需要替换Amazon Cognito?

### 1.1 Cognito的局限性
- **高昂成本**:每月活跃用户(MAU)计费模式在用户量大时成本激增
- **功能冗余**:多数企业仅需基础OIDC/OAuth2能力
- **国内访问延迟**:全球服务节点对国内用户不友好
- **定制化困难**:UI模板修改需通过复杂配置实现

### 1.2 替代方案优势对比
| 特性               | Cognito | Authing方案 |
|--------------------|---------|-------------|
| 国内访问速度       | 慢      | 快(国内CDN) |
| 每MAU成本         | $0.0055 | ¥0.02       |
| 自定义登录页       | 有限    | 完全可定制 |
| 社会化登录渠道     | 需配置  | 开箱即用    |
| 用户管理后台       | 基础版  | 企业级功能  |

---

## 二、技术架构设计

### 2.1 整体解决方案
```mermaid
graph LR
    A[客户端应用] -->|登录请求| B(Authing认证服务)
    B -->|返回JWT| A
    A -->|携带JWT| C[API Gateway]
    C --> D[AWS JWT Authorizer]
    D -->|验证通过| E[Lambda/ECS后端]

2.2 核心组件说明

  1. Authing:负责用户认证、令牌发放
  2. AWS API Gateway:提供HTTP端点
  3. JWT Authorizer:验证令牌有效性
  4. IAM策略:控制最终访问权限

三、详细实施步骤

3.1 Authing侧配置

创建应用

# 通过Authing控制台或CLI创建应用
authing apps:create \
  --name "AWS替代方案" \
  --type spa \
  --redirect-uris "https://yourdomain.com/callback"

配置JWT参数

{
  "algorithm": "RS256",
  "expiresIn": 3600,
  "issuer": "https://yourtenant.authing.cn",
  "audience": "your-aws-api-identifier"
}

获取公钥端点

GET https://yourtenant.authing.cn/oidc/.well-known/jwks.json

3.2 AWS基础设施配置

创建API Gateway

resource "aws_apigatewayv2_api" "main" {
  name          = "authing-secured-api"
  protocol_type = "HTTP"
}

配置JWT Authorizer

Type: AWS::ApiGatewayV2::Authorizer
Properties:
  ApiId: !Ref MyApi
  Name: authing-jwt-authorizer
  AuthorizerType: JWT
  IdentitySource: ["$request.header.Authorization"]
  JwtConfiguration:
    Audience: ["your-aws-api-identifier"]
    Issuer: "https://yourtenant.authing.cn"

3.3 权限策略绑定

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "JWT": {
          "Fn::GetAtt": ["AuthingAuthorizer", "AuthorizerId"]
        }
      },
      "Action": "execute-api:Invoke",
      "Resource": "arn:aws:execute-api:us-east-1:123456789012:api-id/*"
    }
  ]
}

四、关键问题解决方案

4.1 令牌验证流程

  1. 客户端获取Authing签发的JWT
  2. API Gateway将请求转发给Authorizer
  3. Authorizer通过JWKS端点验证签名
  4. 校验iss/aud/exp等标准声明
  5. 返回Allow/Deny策略

4.2 性能优化建议

4.3 安全最佳实践


五、迁移路径建议

5.1 双运行模式过渡

graph TB
    A[现有用户] -->|继续使用| B(Cognito)
    A -->|新用户| C(Authing)
    D[后端服务] --> E[统一JWT验证层]

5.2 数据迁移方案

  1. 使用Cognito用户导出功能
  2. 通过Authing批量导入API处理
  3. 密码字段采用渐进式迁移策略
# 示例迁移脚本
def migrate_user(cognito_user):
    authing_user = {
        "username": cognito_user["Username"],
        "email": cognito_user["Attributes"].get("email"),
        "metadata": {
            "migrated_from": "cognito",
            "original_id": cognito_user["UserId"]
        }
    }
    authing_client.create_user(authing_user)

六、成本对比分析

6.1 典型场景测算(10万MAU)

服务 Cognito费用 Authing方案费用
基础认证 $550 ¥2000
额外存储 $25 免费包含
社会化登录 $100 免费包含
总计 $675 ¥2000

注:按AWS美元报价和Authing人民币报价对比


七、扩展能力

7.1 进阶集成方案

7.2 无服务器架构示例

// Lambda Authorizer示例
export const handler = async (event) => {
  const token = event.authorizationToken.split(' ')[1];
  const decoded = jwt.verify(token, publicKey, {
    issuer: 'https://yourtenant.authing.cn',
    audience: 'your-audience'
  });
  
  return {
    principalId: decoded.sub,
    policyDocument: {
      Version: '2012-10-17',
      Statement: [{
        Action: 'execute-api:Invoke',
        Effect: 'Allow',
        Resource: event.methodArn
      }]
    }
  };
};

结论

通过Authing与AWS JWT Authorizer的组合,企业可以获得: 1. 更优的成本结构(节省约40-60%) 2. 更好的国内用户体验 3. 高度可定制的认证流程 4. 平滑的迁移路径

建议在实施前进行充分的POC验证,特别是对令牌验证延迟和故障转移机制进行测试。对于已有Cognito用户池的场景,采用渐进式迁移策略可最大限度降低业务风险。 “`

注:本文实际约2150字,可根据需要调整具体章节的深度。建议在实际部署时参考: 1. Authing官方文档:https://docs.authing.cn 2. AWS JWT Authorizer指南:https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-jwt-authorizer.html 3. 迁移工具包:https://github.com/authing/cognito-migration-toolkit

推荐阅读:
  1. Django使用uwsgi部署时的配置以及django日志文件的处理方法
  2. 使用Nuxt.js怎么实现一个SSR前端博客

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

cognito authing jwt

上一篇:C#接口、抽象类、抽象方法和虚方法的区别是什么

下一篇:MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》