繁体   English   中英

Laravel 5.6 Passport OAuth Max登录尝试

[英]Laravel 5.6 Passport OAuth Max Login Attempts

我刚刚使用Laravel Passport创建了一个简单的OAuth系统。 该系统将负责外部应用程序用户的注册和身份验证。 一切都按我的预期进行,现在我想实现一种机制,以在预定义次数的失败登录尝试后锁定用户。

我是Laravel和Passport的新手,是否有任何内置的软件包可以为我管理? 还是我必须自己开发此功能? 如果是这样,我该如何完成这项任务?

我一直在搜索整个互联网,但是直到现在我都找不到关于Passport OAuth的任何信息。

我已经成功完成了我想做的事情,如果有人遇到这个问题,这就是我所做的...

创建了一个自定义AuthController和登录方法来替换Laravel Passport的默认oauth / token:

use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Illuminate\Http\Response;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Response;
use \Laravel\Passport\Http\Controllers\AccessTokenController as AccessTokenController;

class AuthController extends AccessTokenController
{
    use AuthenticatesUsers;

    //custom login method
    public function login(Request $request)
    {
        //...
    }
}

在执行任何其他登录操作之前,请检查用户是否已达到最大登录尝试次数:

//custom login method
public function login(Request $request)
{
    //check if the max number of login attempts has been reached
    if ($this->hasTooManyLoginAttempts($request)) 
    {
        $this->fireLockoutEvent($request);

        return "To many attempts...";
    }

    //...
}

通过尝试登录来验证用户凭据。 如果登录成功,则重置失败尝试次数。 如果失败,则增加计数:

//check if user has reached the max number of login attempts

//verify user credentials
$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials)) 
{       
    //reset failed login attemps
    $this->clearLoginAttempts($request);

    //...
}
else
{       
    //count user failed login attempts
    $this->incrementLoginAttempts($request);

    return "Login failed...";
}

最后,由于Passport(OAuth2)使用PSR-7请求(服务器请求接口),因此我们需要将标准Laravel请求转换为PSR-7才能发出访问令牌:

//Authentication passed...

//convert Laravel Request (Symfony Request) to PSR-7
$psr7Factory = new DiactorosFactory();
$psrRequest = $psr7Factory->createRequest($request);

//generate access token
$tokenResponse = parent::issueToken($psrRequest);

//return issued token
return Response::json($tokenResponse);

这是完整的登录方法:

public function login(Request $request)
{
    //check if user has reached the max number of login attempts
    if ($this->hasTooManyLoginAttempts($request)) 
    {
        $this->fireLockoutEvent($request);

        return "To many attempts...";
    }


    //verify user credentials
    $credentials = $request->only('email', 'password');

    if (Auth::attempt($credentials)) 
    {
        //Authentication passed...

        //reset failed login attemps
        $this->clearLoginAttempts($request);

        //convert Laravel Request (Symfony Request) to PSR-7
        $psr7Factory = new DiactorosFactory();
        $psrRequest = $psr7Factory->createRequest($request);

        //generate access token
        $tokenResponse = parent::issueToken($psrRequest);

        //return issued token
        return Response::json($tokenResponse);
    } 
    else 
    {
        //count user failed login attempts
        $this->incrementLoginAttempts($request);

        return "Login failed...";
    }
}

实际上laravel已经具有此功能,您可以在以下有关https://laravel.com/docs/5.6/authentication#login-throttling的链接上进行检查

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM