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&mysql(四)
Oct 09 PHP
php下实现在指定目录搜索指定类型文件的函数
Oct 03 PHP
fleaphp rolesNameField bug解决方法
Apr 23 PHP
gd库图片下载类实现下载网页所有图片的php代码
Aug 20 PHP
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
Jun 04 PHP
Codeigniter中集成smarty和adodb的方法
Mar 04 PHP
php实现简单加入购物车功能
Mar 07 PHP
PHP实现的简单操作SQLite数据库类与用法示例
Jun 19 PHP
PHP封装的PDO数据库操作类实例
Jun 21 PHP
PHP5.6读写excel表格文件操作示例
Feb 26 PHP
TP5框架安全机制实例分析
Apr 05 PHP
PHP面试题 wakeup魔法 Ezpop pop序列化与反序列化
Apr 11 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函数篇之掌握ord()与chr()函数应用
2011/12/05 PHP
php while循环控制的简单实例
2016/05/30 PHP
利用php抓取蜘蛛爬虫痕迹的示例代码
2016/09/30 PHP
php中输出json对象的值(实现方法)
2018/03/07 PHP
JavaScript类和继承 constructor属性
2010/03/04 Javascript
jquery学习笔记二 实现可编辑的表格
2010/04/09 Javascript
AJAX异步从优酷专辑中采集所有视频及信息(JavaScript代码)
2010/11/20 Javascript
纯JavaScript代码实现移动设备绘图解锁
2015/10/16 Javascript
JS实现鼠标滑过链接改变网页背景颜色的方法
2015/10/20 Javascript
javascript+html5+css3自定义弹出窗口效果
2017/10/26 Javascript
Mint UI 基于 Vue.js 移动端组件库
2017/11/07 Javascript
Bootstrap 模态框多次显示后台提交多次BUG的解决方法
2017/12/26 Javascript
mpvue+vuex搭建小程序详细教程(完整步骤)
2018/09/30 Javascript
JS字典Dictionary类定义与用法示例
2019/02/01 Javascript
vue - props 声明数组和对象操作
2020/07/30 Javascript
uniapp开发小程序实现滑动页面控制元素的显示和隐藏效果
2020/12/10 Javascript
[01:06]DOTA2小知识课堂 Ep.01 TP出门不要忘记帮队友灌瓶哦
2019/12/05 DOTA
Python的Django框架中设置日期和字段可选的方法
2015/07/17 Python
Python 操作文件的基本方法总结
2017/08/10 Python
取numpy数组的某几行某几列方法
2018/04/03 Python
python解包概念及实例
2021/02/17 Python
CSS3实现文字波浪线效果示例代码
2016/11/20 HTML / CSS
Paradigit比利时电脑卖场:购买笔记本、电脑、平板和外围设备
2016/11/28 全球购物
物流专业大学应届生求职信
2013/11/03 职场文书
出纳的岗位职责
2013/11/09 职场文书
预备党员党课思想汇报
2014/01/13 职场文书
2014年入党积极分子党校培训心得体会
2014/07/08 职场文书
社区党的群众路线教育实践活动总结材料
2014/10/31 职场文书
酒店总经理岗位职责
2015/04/01 职场文书
施工员岗位职责范本
2015/04/11 职场文书
红色革命电影观后感
2015/06/18 职场文书
Linux安装Nginx步骤详解
2021/03/31 Servers
mysql中between的边界,范围说明
2021/06/08 MySQL
python基础入门之字典和集合
2021/06/13 Python
Go语言实现Base64、Base58编码与解码
2021/07/26 Golang
利用For循环遍历Python字典的三种方法实例
2022/03/25 Python