Laravel重写用户登录简单示例


Posted in PHP onOctober 08, 2016

本文实例讲述了Laravel重写用户登录的方法。分享给大家供大家参考,具体如下:

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; //每分钟最大尝试登录次数
  protected $lockoutTime = 600; //登录锁定时间
  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' => '验证码错误',
      'captcha.required' => '验证码必须',
    ]);
  }
  /**
   * 重写登录
   * @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方法,其实我是复制了原方法然后加入了一些自己的东西。

主要的一些修改就是:

1. 加入验证码(自定义了验证信息及提示)。

2. 后台登录频率的限制。

3. 登录日志记录。

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

PHP 相关文章推荐
php 过滤危险html代码
Jun 29 PHP
ueditor 1.2.6 使用方法说明
Jul 24 PHP
11个PHPer必须要了解的编程规范
Sep 22 PHP
对PHP PDO的一些认识小结
Jan 23 PHP
php采用session实现防止页面重复刷新
Dec 24 PHP
Zend Framework教程之前端控制器Zend_Controller_Front用法详解
Mar 07 PHP
基于Swoole实现PHP与websocket聊天室
Aug 03 PHP
针对多用户实现头像上传功能PHP代码 适用于登陆页面制作
Aug 17 PHP
深入理解PHP中mt_rand()随机数的安全
Oct 12 PHP
php实现姓名根据首字母排序的类与方法(实例代码)
May 16 PHP
Thinkphp 框架扩展之行为扩展原理与实现方法分析
Apr 23 PHP
PHP实现限制域名访问的实现代码(本地验证)
Sep 13 PHP
Laravel使用memcached缓存对文章增删改查进行优化的方法
Oct 08 #PHP
PHP  实现等比压缩图片尺寸和大小实例代码
Oct 08 #PHP
Laravel Memcached缓存驱动的配置与应用方法分析
Oct 08 #PHP
yii通过小物件生成view的方法
Oct 08 #PHP
php获取服务器操作系统相关信息的方法
Oct 08 #PHP
Yii2创建多界面主题(Theme)的方法
Oct 08 #PHP
php微信开发之自定义菜单完整流程
Oct 08 #PHP
You might like
一些php项目中比较通用的php自建函数的详解
2013/06/06 PHP
PHP实现微信网页授权开发教程
2016/01/19 PHP
PHP的Yii框架中YiiBase入口类的扩展写法示例
2016/03/17 PHP
PHP进程通信基础之信号
2017/02/19 PHP
php解析mht文件转换成html的实例
2017/03/13 PHP
PHP自定义函数判断是否为Get、Post及Ajax提交的方法
2017/07/27 PHP
PHP数据库操作四:mongodb用法分析
2017/08/16 PHP
模拟用户操作Input元素,不会触发相应事件
2007/05/11 Javascript
FF IE兼容性的修改小结
2009/09/02 Javascript
dojo学习第二天 ajax异步请求之绑定列表
2011/08/29 Javascript
jquery1.83 之前所有与异步列队相关的模块详细介绍
2012/11/13 Javascript
jQuery表格列宽可拖拽改变且兼容firfox
2014/09/03 Javascript
Javascript设计模式之观察者模式(推荐)
2016/03/29 Javascript
学习使用Bootstrap页面排版样式
2017/05/11 Javascript
jquery如何实现点击空白处隐藏元素
2017/12/05 jQuery
jQuery HTML获取内容和属性操作实例分析
2020/05/20 jQuery
一篇文章让你搞懂JavaScript 原型和原型链
2020/11/23 Javascript
在Python中调用ggplot的三种方法
2015/04/08 Python
合并百度影音的离线数据( with python 2.3)
2015/08/04 Python
python实现列表中由数值查到索引的方法
2018/06/27 Python
对python list 遍历删除的正确方法详解
2018/06/29 Python
使用python切片实现二维数组复制示例
2019/11/26 Python
解决Tensorflow 使用时cpu编译不支持警告的问题
2020/02/03 Python
Python 面向对象部分知识点小结
2020/03/09 Python
详解canvas在圆弧周围绘制文本的两种写法
2018/05/22 HTML / CSS
印度最好的在线药品订购网站:PharmEasy
2018/11/30 全球购物
最好的商品表达自己:Cafepress
2019/09/04 全球购物
预备党员期盼十八届四中全会召开思想汇报
2014/10/17 职场文书
2015年事业单位工作总结
2015/04/27 职场文书
茶花女读书笔记
2015/06/29 职场文书
安全温馨提示语大全
2015/07/14 职场文书
致接力运动员加油稿
2015/07/21 职场文书
2015国庆节感想
2015/08/04 职场文书
500字作文之难忘的同学
2019/12/20 职场文书
CocosCreator入门教程之网络通信
2021/04/16 Javascript
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
2021/06/21 Java/Android