您好,登錄后才能下訂單哦!
Laravel中怎么重寫用戶登錄,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
具體如下:
class AuthController extends Controller { // use ThrottlesLogins, AuthenticatesAndRegistersUsers; protected $redirectTo = 'admin/index'; protected $loginView = 'admin/login'; protected $guard = 'admin'; protected $redirectAfterLogout = 'admin/login'; protected $maxLoginAttempts = 5; //每分鐘最大嘗試登錄次數(shù) protected $lockoutTime = 600; //登錄鎖定時(shí)間 function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'username' => 'required|max:255', 'email' => 'required|email|max:255|unique:admin_users', 'password' => 'required|confirmed|min:6', ]); } /** * @param Request $request */ protected function validateLogin(Request $request) { $this->validate($request,[ $this->loginUsername() => 'required', 'password' => 'required', 'captcha' => 'required|captcha' ], [ 'email.required' => '郵箱必須', 'password.required' => '密碼必須', 'captcha.captcha' => '驗(yàn)證碼錯(cuò)誤', 'captcha.required' => '驗(yàn)證碼必須', ]); } /** * 重寫登錄 * @param Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response */ public function login(Request $request) { $this->validateLogin($request); // 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(); //dd($this->hasTooManyLoginAttempts($request)); if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); //日志記錄 $this->login_logs(['email'=>$request->input('email'), 'login_ip'=>$request->ip(), 'login_result'=>0, 'comments'=>'限制登錄10分鐘']); return $this->sendLockoutResponse($request); } $credentials = $this->getCredentials($request); if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) { //日志記錄 $this->login_logs(['email'=>$request->input('email'), 'login_ip'=>$request->ip(), 'login_result'=>1, 'comments'=>'登錄成功']); 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 && ! $lockedOut) { //日志記錄 $this->login_logs(['email'=>$request->input('email'), 'login_ip'=>$request->ip(), 'login_result'=>0, 'comments'=>'登錄失敗']); $this->incrementLoginAttempts($request); } return $this->sendFailedLoginResponse($request); } /** * 登錄記錄 * @param $data */ private function login_logs ($data) { LoginLog::create($data); } }
直接重寫login方法,其實(shí)我是復(fù)制了原方法然后加入了一些自己的東西。
主要的一些修改就是:
1. 加入驗(yàn)證碼(自定義了驗(yàn)證信息及提示)。
2. 后臺(tái)登錄頻率的限制。
3. 登錄日志記錄。
關(guān)于Laravel中怎么重寫用戶登錄問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。