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 date函数参数详解
Nov 27 PHP
PHP获取MAC地址的函数代码
Sep 11 PHP
比较好用的PHP防注入漏洞过滤函数代码
Apr 11 PHP
PHP中判断变量为空的几种方法小结
Nov 12 PHP
Thinkphp中的curd应用实用要点
Jan 04 PHP
PHP的Laravel框架中使用AdminLTE模板来编写网站后台界面
Mar 21 PHP
php实现常见图片格式的水印和缩略图制作(面向对象)
Jun 15 PHP
WHOOPS PHP调试库的使用
Sep 29 PHP
详解PHP实现支付宝小程序用户授权的工具类
Dec 25 PHP
深入学习微信网址链接解封的防封原理visit_type
Aug 15 PHP
Laravel框架验证码类用法实例分析
Sep 11 PHP
PHP中的输出echo、print、printf、sprintf、print_r和var_dump的示例代码
Dec 01 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
全国FM电台频率大全 - 20 广西省
2020/03/11 无线电
并发下常见的加锁及锁的PHP具体实现代码
2010/10/12 PHP
解析wamp5下虚拟机配置文档
2013/06/27 PHP
php 变量引用与变量销毁机制详细介绍
2016/12/05 PHP
php设计模式之工厂模式用法经典实例分析
2019/09/20 PHP
javascript数组使用调用方法汇总
2007/12/08 Javascript
JavaScript实现选择框按比例拖拉缩放的方法
2015/08/04 Javascript
JavaScript如何禁止Backspace键
2015/12/02 Javascript
JS三级可折叠菜单实现方法
2016/02/29 Javascript
Javascript获取随机数的实现方法
2016/06/22 Javascript
想用好React的你必须要知道的一些事情
2017/07/24 Javascript
详解如何用webpack4从零开始构建react开发环境
2019/01/27 Javascript
Angular请求防抖处理第一次请求失效问题
2019/05/17 Javascript
Vue实现开心消消乐游戏算法
2019/10/22 Javascript
解决vue项目axios每次请求session不一致的问题
2020/10/24 Javascript
js面向对象方式实现拖拽效果
2021/03/03 Javascript
python正则表达式修复网站文章字体不统一的解决方法
2013/02/21 Python
Python编程中的异常处理教程
2015/08/21 Python
python用reduce和map把字符串转为数字的方法
2016/12/19 Python
Pandas:DataFrame对象的基础操作方法
2018/06/07 Python
基于Python的微信机器人开发 微信登录和获取好友列表实现解析
2019/08/21 Python
Pandas聚合运算和分组运算的实现示例
2019/10/17 Python
pytorch 实现模型不同层设置不同的学习率方式
2020/01/06 Python
python连接mongodb数据库操作数据示例
2020/11/30 Python
英国排名第一的礼品体验公司:Red Letter Days
2018/08/16 全球购物
sleep()方法和wait()方法的区别是什么
2012/11/17 面试题
应用艺术毕业生的自我评价
2013/12/04 职场文书
军训自我鉴定
2014/01/22 职场文书
中学生自我鉴定
2014/02/04 职场文书
局机关干部群众路线个人对照检查材料思想汇报
2014/10/05 职场文书
部门2014年度工作总结
2014/11/12 职场文书
教师个人学习总结
2015/02/11 职场文书
自我评价优缺点范文
2015/03/11 职场文书
教师节班会主持词
2015/07/06 职场文书
思想工作总结范文
2015/08/12 职场文书
Python的flask接收前台的ajax的post数据和get数据的方法
2021/04/12 Python