laravel中Auth登录失败次数限制 代码实现

/**
     * 返回登录次数过多错误
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    protected function sendLockoutResponse(Request $request)
    {
        $seconds = $this->secondsRemainingOnLockout($request);
        return BaseResponse::getInstance(423,"登录次数过多,账号锁定, 请 $seconds 秒后再试", array())
            ->toApiResponse();
    }
    
    public function login(Request $request)
    {
        // 验证登录次数过多
        $throttles = $this->isUsingThrottlesLoginsTrait();
        
        if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);
            return $this->sendLockoutResponse($request);
        }
        
        $credentials = $request->only($this->loginUsername(), 'password');
        
        try {
            // attempt to verify the credentials and create a token for the user
            if (! $token = \JWTAuth::attempt($credentials)) {
                return static::sendFailedLoginResponse($request);
            }
        } catch (JWTException $e) {
            // something went wrong whilst attempting to encode the token
            throw new BaseException("创建token失败");
        }
        
        // all good so return the token
        return $this->handleUserAuthenticated($request, $throttles, $token);
    }

/**
     * 返回登录成功
     * @param Request $request
     * @param $throttles
     * @param $token
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    protected function handleUserAuthenticated(Request $request, $throttles, $token)
    {
        if ($throttles) {
            $this->clearLoginAttempts($request);
        }
        return BaseResponse::getInstance(200,'ok', array(
            'token' => $token
        ))->toApiResponse();
    }

public function logout()
    {
        \Auth::guard($this->getGuard())->logout();
        
        return BaseResponse::getInstance(200,'登出成功', array())
            ->toApiResponse();
    }

public function toApiResponse(){
        return response(array(
            static::FIELD_CODE => $this->{static::FIELD_CODE},
            static::FIELD_MESSAGE => $this->{static::FIELD_MESSAGE},
            static::FIELD_TRACK => $this->{static::FIELD_TRACK},
            static::FIELD_DATA => $this->{static::FIELD_DATA},
        static::FIELD_REPORT => $this->{static::FIELD_REPORT},
        ), $this->getHttpCode());
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容