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 相关文章推荐
PHP4中实现动态代理
Oct 09 PHP
一些常用的php简单命令代码集锦
Sep 24 PHP
关于PHP实现异步操作的研究
Feb 03 PHP
PHP垃圾回收机制引用计数器概念分析
Jun 24 PHP
php中3种方法删除字符串中间的空格
Mar 10 PHP
PHP的一个完美GIF等比缩放类,附带去除缩放黑背景
Apr 01 PHP
PHP中返回引用类型的方法
Apr 03 PHP
详解PHP的Laravel框架中Eloquent对象关系映射使用
Feb 26 PHP
zen_cart实现支付前生成订单的方法
May 06 PHP
php代码调试利器firephp安装与使用方法分析
Aug 21 PHP
PHP封装的完整分页类示例
Aug 21 PHP
使用git迁移Laravel项目至新开发环境的步骤详解
Apr 06 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 使用redis锁限制并发访问类示例
2016/11/02 PHP
Thinkphp事务操作实例(推荐)
2017/04/01 PHP
PHP编程实现计算抽奖概率算法完整实例
2017/08/09 PHP
laravel 实现向公共模板中传值 (view composer)
2019/10/22 PHP
Alliance vs AM BO3 第一场2.13
2021/03/10 DOTA
Javascript结合css实现网页换肤功能
2009/11/02 Javascript
JQuery Tips(2) 关于$()包装集你不知道的
2009/12/14 Javascript
asp.net+jquery滚动滚动条加载数据的下拉控件
2010/06/25 Javascript
C#中TrimStart,TrimEnd,Trim在javascript上的实现
2011/01/17 Javascript
jquery $.ajax相关用法分享
2012/03/16 Javascript
EasyUI中datagrid在ie下reload失败解决方案
2015/03/09 Javascript
bootstrap中添加额外的图标实例代码
2017/02/15 Javascript
动态统计当前输入内容的字节、字符数的实例详解
2017/10/27 Javascript
vue2实现数据请求显示loading图
2017/11/28 Javascript
基于vue2.0动态组件及render详解
2018/03/17 Javascript
layui点击导航栏刷新tab页的示例代码
2018/08/14 Javascript
vue-cli系列之vue-cli-service整体架构浅析
2019/01/14 Javascript
基于Proxy的小程序状态管理实现
2019/06/14 Javascript
Vue 3.x+axios跨域方案的踩坑指南
2019/07/04 Javascript
js canvas实现星空连线背景特效
2019/11/01 Javascript
vue中上传视频或图片或图片和文字一起到后端的解决方法
2019/12/01 Javascript
在vue中使用inheritAttrs实现组件的扩展性介绍
2020/12/07 Vue.js
[03:08]Ti4观战指南上
2014/07/07 DOTA
[03:17]2016完美“圣”典风云人物:冷冷专访
2016/12/08 DOTA
Python构造函数及解构函数介绍
2015/02/26 Python
Django框架下在视图中使用模版的方法
2015/07/16 Python
Python工程师面试题 与Python Web相关
2016/01/14 Python
Python中强大的命令行库click入门教程
2016/12/26 Python
python调用摄像头拍摄数据集
2019/06/01 Python
Python reshape的用法及多个二维数组合并为三维数组的实例
2020/02/07 Python
Mountain Hardwear官网:攀岩服装和户外装备
2019/09/26 全球购物
总经理秘书的岗位职责
2013/12/27 职场文书
大学毕业感言200字
2014/03/09 职场文书
商场营业员岗位职责
2015/04/14 职场文书
商务英语邮件开头问候语
2015/11/10 职场文书
浅谈mysql哪些情况会导致索引失效
2021/11/20 MySQL