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 fread()使用技巧
Jan 22 PHP
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
Jan 16 PHP
深入PHP异步执行的详解
Jun 03 PHP
PHP Cookie的使用教程详解
Jun 03 PHP
如何使用php绘制在图片上的正余弦曲线
Jun 08 PHP
PHP函数addslashes和mysql_real_escape_string的区别
Apr 22 PHP
PH P5.2至5.5、5.6的新增功能详解
Jul 14 PHP
php的crc32函数使用时需要注意的问题(不然就是坑)
Apr 21 PHP
详解如何在云服务器上部署Laravel
Jun 30 PHP
PHP 实现人民币小写转换成大写的方法及大小写转换函数
Nov 17 PHP
php判断目录存在的简单方法
Sep 26 PHP
PHP设计模式入门之迭代器模式原理与实现方法分析
Apr 26 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下网站防IP攻击代码,超级实用
2010/10/24 PHP
ThinkPHP独立分组使用的注意事项
2014/11/25 PHP
php中pcntl_fork创建子进程的方法实例
2019/03/14 PHP
javascript基础的动画教程,直观易懂
2007/01/10 Javascript
JQuery里选择超链接的实现代码
2011/05/22 Javascript
js获取元素到文档区域document的(横向、纵向)坐标的两种方法
2013/05/17 Javascript
javascript ajax的5种状态介绍
2014/08/18 Javascript
详解JavaScript正则表达式中的global属性的使用
2015/06/16 Javascript
jquery结婚电子请柬特效源码分享
2015/08/21 Javascript
JQuery实现图片轮播效果
2015/09/15 Javascript
JavaScript实现简单的tab选项卡切换
2016/01/05 Javascript
jQuery头像裁剪工具jcrop用法实例(附演示与demo源码下载)
2016/01/22 Javascript
使用Math.max,Math.min获取数组中的最值实例
2017/04/25 Javascript
JavaScript实现重力下落与弹性效果的方法分析
2017/12/20 Javascript
vue2.0 element-ui中el-select选择器无法显示选中的内容(解决方法)
2018/08/24 Javascript
微信小程序上线发布流程图文详解
2019/05/06 Javascript
[49:28]VP vs Optic 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
打印出python 当前全局变量和入口参数的所有属性
2009/07/01 Python
跟老齐学Python之集合的关系
2014/09/24 Python
深入讲解Java编程中类的生命周期
2016/02/05 Python
Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法
2018/03/13 Python
python读取有密码的zip压缩文件实例
2019/02/08 Python
浅谈python新式类和旧式类区别
2019/04/26 Python
python交易记录链的实现过程详解
2019/07/03 Python
python opencv将图片转为灰度图的方法示例
2019/07/31 Python
numpy.random.shuffle打乱顺序函数的实现
2019/09/10 Python
Python命令行参数解析工具 docopt 安装和应用过程详解
2019/09/26 Python
python 如何停止一个死循环的线程
2020/11/24 Python
Bally巴利英国官网:经典瑞士鞋履、手袋及配饰奢侈品牌
2018/05/07 全球购物
承诺书怎么写
2014/03/26 职场文书
校长四风对照检查材料
2014/09/27 职场文书
会议欢迎词范文
2015/01/27 职场文书
皇城相府导游词
2015/02/06 职场文书
2015年清明节网上祭英烈活动总结
2015/03/26 职场文书
导游带团欢迎词
2015/09/30 职场文书
教你使用Ubuntu搭建DNS服务器
2022/09/23 Servers