如何将验证码添加到 Laravel 5.1 登录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34109233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add captcha to Laravel 5.1 login?
提问by Amirmasoud
How can I add captcha/Google-reCaptcha to user auth login? there is only validation for register that I can modify, how can I modify validation for login?
如何将验证码/Google-reCaptcha 添加到用户身份验证登录?我只能修改注册验证,如何修改登录验证?
my problem is not how to add recaptcha, my problem is how to add recaptcha validattion to login validation rules.
我的问题不是如何添加recaptcha,我的问题是如何将recaptcha 验证添加到登录验证规则。
回答by aram yosefi
add this line to loginController:
将此行添加到 loginController:
public function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required',
'password' => 'required',
// new rules here
'g-recaptcha-response' => 'required|captcha',
]);
}
回答by Sleuteltje
Here's how i figured it out, for anyone else that also has this problem (using http://github.com/greggilbert/recaptcha):
这就是我想出来的方法,对于其他也有这个问题的人(使用 http://github.com/greggilbert/recaptcha):
Copy the validateLogin function from vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php to app\http\Controllers\Auth\AuthController.php and just add the line to the array. You end up with this:
将 validateLogin 函数从 vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php 复制到 app\http\Controllers\Auth\AuthController.php 并将该行添加到数组中。你最终得到这个:
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function validateLogin(\Illuminate\Http\Request $request)
{
$this->validate($request, [
$this->loginUsername() => 'required', 'password' => 'required','g-recaptcha-response' => 'required|recaptcha',
]);
}
That's it, captcha works now on login form. (You should ofcourse also add {!! Recaptcha::render() !!} in the login view)
就是这样,验证码现在可用于登录表单。(您当然也应该在登录视图中添加 {!! Recaptcha::render() !!})
回答by Amirmasoud
That's how I figure it out, I copied postLogin method from vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenicatesUsers.php
and edit it like bellow:
我就是这样想出来的,我复制了 postLogin 方法vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenicatesUsers.php
并像下面这样编辑它:
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postLogin(\Illuminate\Http\Request $request)
{
$this->validate($request, [
$this->loginUsername() => 'required',
'password' => 'required',
'g-recaptcha-response' => 'required|recaptcha'
]);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
return $this->sendLockoutResponse($request);
}
$credentials = $this->getCredentials($request);
if (\Auth::attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles) {
$this->incrementLoginAttempts($request);
}
return redirect($this->loginPath())
->withInput($request->only($this->loginUsername(), 'remember'))
->withErrors([
$this->loginUsername() => $this->getFailedLoginMessage(),
]);
}
I've changed Request
in method param to \Illuminate\Http\Request
and Auth
to \Auth
. you can add these 2 at top of your file.
我已经改变了Request
在方法参数去\Illuminate\Http\Request
和Auth
到\Auth
。您可以在文件顶部添加这两个。
then I changed validation rules of login and add 'g-recaptcha-response' => 'required|recaptcha'
in order to use Google recaptcha (https://github.com/greggilbert/recaptcha)
然后我更改了登录验证规则并添加'g-recaptcha-response' => 'required|recaptcha'
以使用 Google recaptcha ( https://github.com/greggilbert/recaptcha)
回答by Mohammad Mahdi KouchakYazdi
Use BotDetect CAPTCHA for laravel 5.2 : https://captcha.com/doc/php/examples/laravel-auth-captcha-example.html#laravel_auth_controller
对 laravel 5.2 使用 BotDetect 验证码:https://captcha.com/doc/php/examples/laravel-auth-captcha-example.html#laravel_auth_controller
回答by rkd.me
You have to add this library via composer.json
, then edit views
and put the captcha code in it.
您必须通过 添加此库composer.json
,然后编辑views
并将验证码放入其中。
About Validation: you can make new rule and integrate it with Laravel validator or validate it in controller before or after laravel validation.
关于验证:您可以制定新规则并将其与 Laravel 验证器集成或在 Laravel 验证之前或之后在控制器中对其进行验证。
回答by Saad
You could use greggilbert/recaptcha
package for Laravel-5. This github page explains how to use the package https://github.com/greggilbert/recaptcha
你可以使用greggilbert/recaptcha
Laravel-5 的包。这个 github 页面解释了如何使用这个包https://github.com/greggilbert/recaptcha