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下实现一个阿拉伯数字转中文数字的函数
Jul 10 PHP
php 三维饼图的实现代码
Sep 28 PHP
phpMyAdmin 安装配置方法和问题解决
Jun 08 PHP
应用开发中涉及到的css和php笔记分享
Aug 02 PHP
php加密解密实用类分享
Jan 07 PHP
PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
Jun 12 PHP
PHP实现链式操作的核心思想
Jun 23 PHP
php使用文本统计访问量的方法
May 12 PHP
PHP基于IMAP收取邮件的方法示例
Aug 07 PHP
PHP实现打包zip并下载功能
Jun 12 PHP
PHP格式化显示时间date()函数代码
Oct 03 PHP
PHP设计模式(一)工厂模式Factory实例详解【创建型】
May 02 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初学者写及时补给skype用户充话费的小程序
2008/11/02 PHP
PHP Document 代码注释规范
2009/04/13 PHP
PHP 采集程序原理分析篇
2010/03/05 PHP
php中函数的形参与实参的问题说明
2010/09/01 PHP
PHP file_exists问题杂谈
2012/05/07 PHP
PHP生成推广海报的方法分享
2018/04/22 PHP
php使用redis的几种常见操作方式和用法示例
2020/02/20 PHP
javascript js cookie的存储,获取和删除
2007/12/29 Javascript
[原创]js获取数组任意个不重复的随机数组元素
2010/03/15 Javascript
Dom在ajax技术中的作用说明
2010/10/25 Javascript
myEvent.js javascript跨浏览器事件框架
2011/10/24 Javascript
控制文字内容的显示与隐藏示例
2014/06/11 Javascript
深入理解JavaScript系列(30):设计模式之外观模式详解
2015/03/03 Javascript
JavaScript获取指定元素位置的方法
2015/04/08 Javascript
jquery实现的动态回到顶部特效代码
2015/10/28 Javascript
基于jQuery实现select下拉选择可输入附源码下载
2016/02/03 Javascript
javascript DIV实现跟随鼠标移动
2020/03/19 Javascript
AngularJS ng-mousedown 指令
2016/08/02 Javascript
使用Javascript监控前端相关数据的代码
2016/10/27 Javascript
基于vue循环列表时点击跳转页面的方法
2018/08/31 Javascript
vue webpack重写cookie路径的方法
2019/07/10 Javascript
vue 取出v-for循环中的index值实例
2019/11/09 Javascript
如何处理Python3.4 使用pymssql 乱码问题
2016/01/08 Python
[原创]Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】
2018/10/29 Python
解决TensorFlow调用Keras库函数存在的问题
2020/07/06 Python
Python环境配置实现pip加速过程解析
2020/11/27 Python
浅析Python 中的 WSGI 接口和 WSGI 服务的运行
2020/12/09 Python
canvas 下载二维码和图片加水印的方法
2018/03/21 HTML / CSS
德国街头和运动文化高品质商店:BSTN Store
2017/08/26 全球购物
教学器材管理制度
2014/01/26 职场文书
中学生班主任评语
2014/01/30 职场文书
会计求职信
2014/05/29 职场文书
我的中国梦演讲稿高中篇
2014/08/19 职场文书
禁毒主题班会教案
2015/08/14 职场文书
机械原理课程设计心得体会
2016/01/15 职场文书
mysql中varchar类型的日期进行比较、排序等操作的实现
2021/11/17 MySQL