Laravel 无痛使用 recaptcha 不用科学 sw

以 laravel5.7 登录为例。

步骤

首先

composer require 233sec/laravel-recaptchav3

修改 App\Http\Controllers\Auth\LoginController, 添加如下方法

    /**
     * Validate the user login request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function validateLogin(Request $request)
    {
        $request->validate([
            $this->username() => 'required|string',
            'password' => 'required|string',
            'g-recaptcha-response' => 'required|recaptchav3:login,0.5', // 重点在这一行
        ]);
    }

修改 resources/views/auth/login.blade.php
<button type="submit"...> 之前加入如下代码

            <div class="form-group
            @if ($errors->has('g-recaptcha-response'))
            has-error
            has-feedback
            @endif
            ">
                {!! RecaptchaV3::field('login') !!}
                @if ($errors->has('g-recaptcha-response'))
                    <span class="glyphicon glyphicon-warning-sign form-control-feedback" aria-hidden="true"></span>
                    <span class="help-block">{{ $errors->first('g-recaptcha-response') }}</span>
                @endif
            </div>

并在你的模板 javascript

@push('script')
    {!! RecaptchaV3::initJs() !!}
@endpush

.env 加入如下

RECAPTCHAV3_SITEKEY=#你的sitekey#
RECAPTCHAV3_SECRET=#你的secret#
RECAPTCHAV3_ORIGIN=https://www.recaptcha.net #这一行特别重要,否则在大陆无法使用

验证消息自定义

修改 resources/lang/#你的语言设置#/validation.php

<?php
return [
    ...,
    'custom' => [
        'g-recaptcha-response' => [
            'recaptchav3' => '验证码错误'
        ]
    ],

    'attributes' => [
        'g-recaptcha-response' => '验证码',
    ]
];

效果

file
file

来源

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 7

什么原理不用梯子

5年前 评论

@CorePlusPlus
这个域名是解析在了北京,所以不可能提供google其他的服务的,不然也被封了。

5年前 评论

看到标题 无痛 第一个想到了 人流~

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!