nswdpc/silverstripe-recaptcha-v3

为 Silverstripe 框架提供 Recaptcha V3 验证,包括表单字段、垃圾邮件防护和验证支持

安装次数: 9,559

依赖项: 2

建议者: 0

安全: 0

星标: 2

关注者: 4

分支: 3

公开问题: 0

类型:silverstripe-vendormodule

v1.0.0 2024-03-27 03:59 UTC

README

此模块通过使用 reCAPTCHA v3 验证网站交互的质量。它包含可以手动添加到表单或通过垃圾邮件防护配置自动添加的表单字段。

您还可以使用此模块来验证与您网站的非表单交互中的标记/操作。

验证的默认分数为 0.5,您可以在项目配置中更改此值

功能

  • 通过配置 API 设置基于项目阈值的阈值
  • 保护表单的细粒度规则
  • 管理徽章位置

安装

通过 composer 安装

composer require nswdpc/silverstripe-recaptcha-v3

要求

composer.json

表单垃圾邮件防护

手动

将字段添加到表单中,就像添加任何其他 FormField

use NSWDPC\SpamProtection;

// ...

$field = RecaptchaV3Field::create( 'MyRecaptchaField' );
$form->Fields()->push( $field );

垃圾邮件防护

要将 RecaptchaV3SpamProtector 设置为默认垃圾邮件防护程序,请将以下配置部分添加到您的项目配置中

---
Name: 'project_spamprotection'
After:
  - '#nswdpc_recaptchav3_spamprotection'
---
# set RecaptchaV3SpamProtector as the default spam protector
SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension:
  default_spam_protector: NSWDPC\SpamProtection\RecaptchaV3SpamProtector

然后调用 $form->enableSpamProtection() 将字段自动添加到表单中。阅读 silverstripe/spamprotection 文档 了解更多信息。

silverstripe/userforms 模块的字段

模块 nswdpc/silverstripe-recaptcha-v3-userforms 提供用户表单字段

进一步文档

配置

要使用此模块,在 reCAPTCHA 管理网站 上创建一个 reCAPTCHA V3 站点,创建后您将获得该站点的站点密钥和密钥,这两个密钥都适用于您添加的域名(及其子域名)。

将站点设置分开存储到您的环境中是一个好主意,以便隔离密钥。在 reCAPTCHA 站点设置中,添加您正在设置的环境的域名(以及任何其他所需选项和您共享项目的 Google 用户)。

将提供的站点/密钥复制到您的项目配置中,例如在项目 _config 目录中的 spamprotection.yml 文件中。

---
Name: 'my-project-spamprotector'
After:
  - '#nswdpc_recaptchav3_spamprotection'
---
# set RecaptchaV3SpamProtector as the default spam protector
SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension:
  default_spam_protector: 'NSWDPC\SpamProtection\RecaptchaV3SpamProtector'
# RecaptchaV3 configuration values
# TokenResponse is a model to verify responses from the reCAPTCHA v3 API
NSWDPC\SpamProtection\TokenResponse:
  # Set your desired score -> 1 = good, 0 = spam
  score: 0.4
# Verifier is a model to handle token verification
NSWDPC\SpamProtection\Verifier:
  # Your secret key provided in the reCAPTCHA admin site settings area
  secret_key: 'abc123....'
# The field handles setup of the API and token retrieval
NSWDPC\SpamProtection\RecaptchaV3Field:
  ## Your site key provided in the reCAPTCHA admin site settings area
  site_key: 'zyx321.....'
  # Global action prefix for the field
  execute_action: 'submit'
NSWDPC\SpamProtection\RecaptchaV3SpamProtector:
  # Place the reCAPTCHA privacy text adjacent to the hidden input form field
  badge_display: 'field'

令牌检索

触发从 reCAPTCHAv3 API 检索令牌的行为是表单的 focus() 事件。这避免了与例如前端表单验证器的提交事件竞争条件。

Safari 不支持表单元素(如单选按钮和复选框)的 focus 事件。使用 change() 事件处理程序来支持此功能。

当表单的第一个字段获得焦点时,将检索令牌。如果在配置的刷新时间后表单中的另一个字段获得焦点,则将刷新令牌。

将最新的令牌与表单一起提交并进行验证,如果它已过期(令牌有 2 分钟的有效期),则将提示访客检查并重新提交带有新令牌的表单。

reCAPTCHAv3 API 验证令牌值并提供分数。符合最小阈值要求的分数将通过验证。

许可证

BSD-3-Clause

维护者

错误追踪器

我们欢迎在 Github 问题跟踪器上对该项目的错误报告、拉取请求和功能请求。

在提交新问题之前,请先审阅行为准则

安全

如果您发现此模块存在安全问题,请首先通过电子邮件digital[@]dpc.nsw.gov.au告知,并详细说明您的发现。

开发和贡献

如果您想为此模块做出贡献,请确保提出拉取请求并与模块维护者进行讨论。

在完成拉取请求之前,请先审阅行为准则