lukeusher / laravel-honeypot
Requires
- php: ^8.0
- illuminate/contracts: ^8.0
- illuminate/encryption: ^8.0
- illuminate/http: ^8.0
- illuminate/support: ^8.0
- illuminate/validation: ^8.0
- nesbot/carbon: ^2.0
- spatie/laravel-package-tools: ^1.6
- symfony/http-foundation: ^5.1.2
Requires (Dev)
- orchestra/testbench: ^6.0
- spatie/phpunit-snapshot-assertions: ^4.0
- spatie/test-time: ^1.2.1
README
这是一个spatie/laravel-honeypot的分支,升级到PHP8后可以与Laravel 6 (LTS)一起使用。上游包仅保留对PHP7和Laravel 6的支持。
当将表单添加到公开网站时,存在垃圾机器人尝试用假值提交表单的风险。幸运的是,这些机器人大多数都很笨。你可以通过在表单中添加一个不应包含值的不可见字段来阻止其中大部分。这种字段称为蜜罐。这些垃圾机器人会填写所有字段,包括蜜罐。
当收到包含填写蜜罐字段的提交时,此包将丢弃该请求。此外,此包还会检查提交表单所需的时间。这是通过另一个不可见字段中的时间戳来完成的。如果表单提交时间极短,反垃圾邮件也会被触发。
安装此包后,您只需将@honeypot
Blade指令添加到要保护的任何表单中。
<form method="POST"> @honeypot <input name="myField" type="text"> </form>
视频教程
在这个视频中,该视频是Mailcoach视频课程的一部分,您可以了解如何安装和使用此包。
支持我们
我们在创建一流的开放源代码包上投入了大量资源。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从家乡寄给我们明信片,说明您正在使用我们的哪个包。您可以在我们的联系页面上找到我们的地址。我们将在我们的虚拟明信片墙上发布所有收到的明信片。
安装
您可以通过Composer安装此包
composer require lukeusher/laravel-honeypot dev-master
可选地,您可以发布包的配置文件。
php artisan vendor:publish --provider="Spatie\Honeypot\HoneypotServiceProvider" --tag=config
这是要发布到config/honeypot.php
的配置文件内容
use Spatie\Honeypot\SpamResponder\BlankPageResponder; return [ /* * Here you can specify name of the honeypot field. Any requests that submit a non-empty * value for this name will be discarded. Make sure this name does not * collide with a form field that is actually used. */ 'name_field_name' => env('HONEYPOT_NAME', 'my_name'), /* * When this is activated there will be a random string added * to the name_field_name. This improves the * protection against bots. */ 'randomize_name_field_name' => env('HONEYPOT_RANDOMIZE', true), /* * This field contains the name of a form field that will be used to verify * if the form wasn't submitted too quickly. Make sure this name does not * collide with a form field that is actually used. */ 'valid_from_field_name' => env('HONEYPOT_VALID_FROM', 'valid_from'), /* * If the form is submitted faster than this amount of seconds * the form submission will be considered invalid. */ 'amount_of_seconds' => env('HONEYPOT_SECONDS', 1), /* * This class is responsible for sending a response to requests that * are detected as being spammy. By default a blank page is shown. * * A valid responder is any class that implements * `Spatie\Honeypot\SpamResponder\SpamResponder` */ 'respond_to_spam_with' => BlankPageResponder::class, /* * This switch determines if the honeypot protection should be activated. */ 'enabled' => env('HONEYPOT_ENABLED', true), ];
使用方法
首先,您必须将@honeypot
Blade指令添加到您希望保护的任何表单中。
<form method="POST" action="{{ action(App\Http\Controllers\ContactFormSubmissionController::class, 'create') }}")> @honeypot <input name="myField" type="text"> </form>
@honeypot
将添加两个字段:my_name
和my_time
(您可以在配置文件中更改名称)。
接下来,您必须在处理表单提交的路由中使用 Spatie\Honeypot\ProtectAgainstSpam
中间件。此中间件将拦截任何提交名为 my_name
的键的非空值的请求。如果请求提交的速度比包在 my_time
中生成的加密时间戳要快,它也会拦截该请求。
use App\Http\Controllers\ContactFormSubmissionController; use Spatie\Honeypot\ProtectAgainstSpam; Route::post([ContactFormSubmissionController::class, 'create'])->middleware(ProtectAgainstSpam::class);
如果您想将 Spatie\Honeypot\ProtectAgainstSpam
中间件与 Laravel 内置的认证路由集成,请用适当的中间件组包裹 Auth::routes();
声明(确保在认证表单中添加 @honeypot
指令)。
use Spatie\Honeypot\ProtectAgainstSpam; Route::middleware(ProtectAgainstSpam::class)->group(function() { Auth::routes(); });
如果您的应用中有许多表单由许多不同的控制器处理,您可以将其注册为全局中间件。
// inside app\Http\Kernel.php protected $middleware = [ // ... \Spatie\Honeypot\ProtectAgainstSpam::class, ];
测试中禁用
默认情况下,任何在1秒内提交的受保护表单都会被标记为垃圾邮件。在运行端到端测试时,这些测试应尽可能快地运行,您可能不希望这样。
要禁用代码中的所有蜜罐,可以将 enabled
配置值设置为 false
。
config()->set('honeypot.enabled', false)
自定义响应
当检测到垃圾邮件提交时,该包默认会显示一个空白页。您可以通过编写自己的 SpamResponse
并在 honeypot
配置文件的 respond_to_spam_with
键中指定其完全限定的类名来自定义此行为。
有效的 SpamResponse
是任何实现了 Spatie\Honeypot\SpamResponder\SpamResponder
接口的类。下面是这个接口的样子
namespace Spatie\Honeypot\SpamResponder; use Closure; use Illuminate\Http\Request; interface SpamResponder { public function respond(Request $request, Closure $next); }
尽管垃圾邮件响应器的首要目的是响应垃圾邮件请求,但您也可以在那里做其他事情。例如,您可以使用 $request
上的属性来确定垃圾邮件的来源(也许所有请求都来自同一个 IP),并添加一些逻辑来阻止该来源。
如果该包错误地确定请求是垃圾邮件,您可以像在中间件中一样,通过将 $request
传递给 $next
闭包来生成默认响应。
// in your spam responder $regularResponse = $next($request)
自定义生成的蜜罐字段
要自定义 @honeypot
生成的输出,可以使用以下命令发布 honeypot
视图
php artisan vendor:publish --provider="Spatie\Honeypot\HoneypotServiceProvider" --tag=views
视图将被放置在 resources/views/vendor/honeypot/honeypotFormFields.blade.php
。这是默认内容
@if($enabled) <div id="{{ $nameFieldName }}_wrap" style="display:none;"> <input name="{{ $nameFieldName }}" type="text" value="" id="my_name"> <input name="{{ $validFromFieldName }}" type="text" value="{{ $encryptedValidFrom }}"> </div> @endif
触发的事件
每当检测到垃圾邮件时,都会触发 Spatie\Honeypot\SpamDetected
事件。该事件有一个公开属性 $request
。
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
替代方案
如果您需要更强的垃圾邮件保护,请考虑使用 Google ReCaptcha 或 Akismet。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 freek@spatie.be 而不是使用问题跟踪器来报告。
致谢
本软件包受到Honeypot软件包的启发,该软件包由Maksim Surguy开发。
许可证
MIT许可证(MIT)。更多详细信息,请参阅许可证文件。