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 获取本机外网/公网IP的代码
May 09 PHP
PHP程序员最常犯的11个MySQL错误小结
Nov 20 PHP
fleaphp rolesNameField bug解决方法
Apr 23 PHP
PHP时间戳 strtotime()使用方法和技巧
Oct 29 PHP
PHP中常用的转义函数
Feb 28 PHP
PHP连接操作access数据库实例
Mar 30 PHP
PHP编写简单的App接口
Aug 28 PHP
探究Laravel使用env函数读取环境变量为null的问题
Dec 06 PHP
ZendFramework框架实现连接两个或多个数据库的方法
Dec 08 PHP
Laravel学习教程之View模块详解
Sep 18 PHP
php把字符串指定字符分割成数组的方法
Mar 12 PHP
php redis setnx分布式锁简单原理解析
Oct 23 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
WordPress中用于获取文章作者与分类信息的方法整理
2015/12/17 PHP
PHP检查端口是否可以被绑定的方法示例
2018/08/09 PHP
php传值和传引用的区别点总结
2019/11/19 PHP
浅谈Javascript面向对象编程
2011/11/15 Javascript
javascript中局部变量和全局变量的区别详解
2015/02/27 Javascript
Swiper实现轮播图效果
2017/07/03 Javascript
解决option标签selected="selected"属性失效的问题
2017/11/06 Javascript
nodejs中art-template模板语法的引入及冲突解决方案
2017/11/07 NodeJs
jQuery实现的简单图片轮播效果完整示例
2018/02/08 jQuery
使用Angular CLI从蓝本生成代码详解
2018/03/24 Javascript
JavaScript事件发布/订阅模式原理与用法分析
2018/08/21 Javascript
Vue路由history模式解决404问题的几种方法
2018/09/29 Javascript
koa2 从入门到精通(小结)
2019/07/23 Javascript
浅谈vue生命周期共有几个阶段?分别是什么?
2020/08/07 Javascript
Vue实现购物小球抛物线的方法实例
2020/11/22 Vue.js
[01:58]最残酷竞争 2016国际邀请赛中国区预选赛积分循环赛回顾
2016/06/28 DOTA
跟老齐学Python之正规地说一句话
2014/09/28 Python
python黑魔法之编码转换
2016/01/25 Python
Django模型序列化返回自然主键值示例代码
2019/06/12 Python
python实现共轭梯度法
2019/07/03 Python
python fuzzywuzzy模块模糊字符串匹配详细用法
2019/08/29 Python
python生成任意频率正弦波方式
2020/02/25 Python
Python 判断时间是否在时间区间内的实例
2020/05/16 Python
在python中list作函数形参,防止被实参修改的实现方法
2020/06/05 Python
详解background属性的8个属性值(面试题)
2020/11/02 HTML / CSS
Java语言程序设计测试题判断题部分
2013/01/06 面试题
建筑公司文秘岗位职责
2013/11/29 职场文书
高中同学聚会邀请函
2014/01/11 职场文书
员工薪酬福利制度
2014/01/17 职场文书
批评与自我批评材料
2014/02/15 职场文书
公司联欢会策划方案
2014/05/19 职场文书
2014年体育教师工作总结
2014/12/03 职场文书
质量保证书怎么写
2015/02/27 职场文书
贷款工作证明模板
2015/06/12 职场文书
Python如何把不同类型数据的json序列化
2021/04/30 Python
CentOS MySql8 远程连接实战
2022/04/19 MySQL