跳转到内容

React 网站身份验证

React 网站身份验证生成器使用 Amazon Cognito 为您的 React 网站添加身份验证功能。

此生成器配置 CDK 或 Terraform 基础设施以创建 Cognito 用户池和关联的身份池,以及用于处理用户登录流程的托管 UI,并将其与您的 React 网站集成。

您可以通过两种方式为 React 网站添加身份验证:

运行此生成器@aws/nx-plugin:ts#website#auth

pnpm nx g @aws/nx-plugin:ts#website#auth
构建你的命令5

必需

生成器选项5 个选项
project必需string

网站的根目录。

cognitoDomainstring

要使用的 Cognito 域前缀。如果省略,将从 npm scope 和网站项目名称派生一个值。

allowSignupboolean默认值: false

是否允许自助注册

iacenum默认值: inherit

首选的 IaC 提供商。默认情况下,这继承自您的初始选择。

inheritcdkterraform
preferInstallDependenciesboolean默认值: true

是否在生成器运行后优先安装依赖项。设置为 false 可在批量运行多个生成器时延迟安装(如果后续生成器需要计算 Nx 项目图,仍会运行安装);在最后统一安装一次。

您将在 React 网站中找到以下更改:

  • 文件夹src
    • 文件夹components
      • 文件夹CognitoAuth
        • index.tsx Main authentication component
    • main.tsx Updated to instrument the CognitoAuth component

由于此生成器根据您选择的 iac 提供基础设施即代码,它将在 packages/common 中创建一个项目,其中包含相关的 CDK 构造或 Terraform 模块。

通用基础设施即代码项目的结构如下:

  • 文件夹packages/common/constructs
    • 文件夹src
      • 文件夹app/ Constructs for infrastructure specific to a project/generator
      • 文件夹core/ Generic constructs which are reused by constructs in app
      • index.ts Entry point exporting constructs from app
    • project.json Project build targets and configuration

您还将根据所选的 iac 找到以下生成的基础设施代码:

  • 文件夹packages/common/constructs/src
    • 文件夹core
      • user-identity.ts Construct which defines the user pool and identity pool

此生成器在现有静态网站架构中添加了 Amazon Cognito 用户池(用于登录)和身份池(用于将已登录用户联合到作用域 IAM 凭证):

Loading the diagram…

用户池在 Cognito Plus 功能计划上创建,并将标准身份验证的威胁防护设置为 AUDIT 模式。在审计模式下,Cognito 为每次登录分配风险级别,并将评估记录到 CloudWatch,而不会阻止用户。

一旦您观察了用户的风险评估,您可以切换到全功能强制执行,以自动响应风险活动(例如要求 MFA 或阻止登录):

packages/common/constructs/src/core/user-identity.ts 中将 standardThreatProtectionMode 设置为 StandardThreatProtectionMode.FULL_FUNCTION

默认情况下,用户必须在登录之前配置 MFA(短信验证码或基于时间的一次性密码)。您可以将 MFA 设为可选、完全关闭它,或限制可用的第二因素方法:

import { Mfa } from 'aws-cdk-lib/aws-cognito';
new UserIdentity(this, 'Identity', {
mfa: Mfa.OPTIONAL,
mfaSecondFactor: { sms: false, otp: true },
});

mfa 接受 Mfa.OFF / Mfa.OPTIONAL / Mfa.REQUIREDmfaSecondFactor.smsmfaSecondFactor.otp 独立启用或禁用每个第二因素方法;当 mfaMfa.OFF 时它们无效。将 mfa: Mfa.REQUIRED 设置为两种方法都禁用会在合成时被拒绝,因为这样就没有人能完成登录。

默认情况下,用户池与 AWS WAFv2 Web ACL 关联,使用 AWSManagedRulesCommonRuleSetAWSManagedRulesKnownBadInputsRuleSet 托管规则组。如果您希望管理自己的 Web ACL 或不需要 Web ACL,可以禁用此功能:

new UserIdentity(this, 'Identity', { enableWaf: false });

您需要将用户身份基础设施添加到您的堆栈中,在网站_之前_声明它:

packages/infra/src/stacks/application-stack.ts
import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { MyWebsite, UserIdentity } from '@my-scope/common-constructs';
export class ApplicationStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
new UserIdentity(this, 'Identity');
new MyWebsite(this, 'MyWebsite');
}
}

UserIdentity 构造会自动添加必要的运行时配置,以确保您的网站可以指向正确的 Cognito 用户池进行身份验证。

为了授予已认证用户执行某些操作的权限,例如授予调用 API 的权限,您可以向身份池已认证角色添加 IAM 策略语句:

packages/infra/src/stacks/application-stack.ts
import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { MyWebsite, UserIdentity, MyApi } from '@my-scope/common-constructs';
export class ApplicationStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const identity = new UserIdentity(this, 'Identity');
const api = new MyApi(this, 'MyApi', {
integrations: MyApi.defaultIntegrations(this).build(),
});
api.grantInvokeAccess(identity.identityPool.authenticatedRole);
new MyWebsite(this, 'MyWebsite');
}
}