使用Laravel 5.2记录失败的登录尝试 [英] Log failed login attempts with Laravel 5.2

查看:93
本文介绍了使用Laravel 5.2记录失败的登录尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用laravel 5.2记录失败的登录尝试,我已安装了auth脚手架.

How would I log failed login attempts with laravel 5.2 I have the auth scaffolding installed.

我已将以下内容添加到我的EventsServiceProvider.php

I have added the following to my EventsServiceProvider.php

 protected $listen = [
    'Illuminate\Auth\Events\Attempting' => [
        'App\Listeners\LogAuthenticationAttempt',
    ],

    'Illuminate\Auth\Events\Login' => [
        'App\Listeners\LogSuccessfulLogin',
    ],

    'Illuminate\Auth\Events\Logout' => [
        'App\Listeners\LogSuccessfulLogout',
    ],

    'Illuminate\Auth\Events\Lockout' => [
        'App\Listeners\LogLockout',
    ],
];

在我的app/Listeners/LogAuthenticationAttempt.php中

And in my app/Listeners/LogAuthenticationAttempt.php

我有

    $log = new Access_Log();
    $log->ip_address = Request::getClientIp();
    $log->is_success = 0;
    $log->save();

但是,这仅记录已进行登录尝试>我可以使用LogSuccessfulLogin侦听器记录成功的登录尝试,但是我看不到如何记录失败的登录尝试.

But this just logs that an login attempt has been made> I can log a successful login attempt using the LogSuccessfulLogin Listener but I cant see how to log a failed login attempt.

我想到我可以在LogSuccessfulLogin侦听器中更新日志条目上的is_success值,但是在LogAuthenticationAttempt和LogSuccessfulLogin之间可以使用什么持久化以将其标识为同一登录尝试?

It has occurred to me that I could just update the is_success value on the log entry in the LogSuccessfulLogin Listener but what can I use to persist between LogAuthenticationAttempt and LogSuccessfulLogin to identify this as the same login attempt?

推荐答案

原来有一个失败的事件,只是我关注的文档中没有.参见Pervara的评论.

Turns out there was a failed event it just wasn't in the docs I was following. See Pervara's comment.

我将其添加到EventsServiceProvider.php:

I added this to the EventsServiceProvider.php:

    'Illuminate\Auth\Events\Failed' => [
        'App\Listeners\LogFailedAuthenticationAttempt',
    ],

并使用以下代码创建了app/Listeners/LogFailedAuthenticationAttempt.php:

And created app/Listeners/LogFailedAuthenticationAttempt.php with the following code:

     /**
 * Handle the event.
 *
 * @param  Failed  $event
 * @return void
 */
public function handle(Failed $event)
{
    $log = new Access_Log();
    $log->user_id = $event->user->id;
    $log->ip_address = Request::getClientIp();
    $log->event = 'Login Failed';
    $log->is_success = 0;
    $log->save();
}

完美运行.

这篇关于使用Laravel 5.2记录失败的登录尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆