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中执行系统外部命令
Oct 09 PHP
PHP详细彻底学习Smarty
Mar 27 PHP
php处理json时中文问题的解决方法
Apr 12 PHP
php对数组排序代码分享
Feb 24 PHP
PHP5.3与5.5废弃与过期函数整理汇总
Jul 10 PHP
PHP将session信息存储到数据库的类实例
Mar 04 PHP
php实现字符串翻转的方法
Mar 27 PHP
PHP上传文件参考配置大文件上传
Dec 16 PHP
php+ajax注册实时验证功能
Jul 20 PHP
PHPWind9.0手动屏蔽验证码解决后台关闭验证码但是依然显示的问题
Aug 12 PHP
微信公众平台开发(五) 天气预报功能开发
Dec 03 PHP
解决laravel session失效的问题
Oct 14 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
使用sockets:从新闻组中获取文章(二)
2006/10/09 PHP
php+redis在实际项目中HTTP 500: Internal Server Error故障排除
2017/02/05 PHP
Laravel框架中VerifyCsrfToken报错问题的解决
2017/08/30 PHP
jquery 操作单选框,复选框,下拉列表实现代码
2009/10/27 Javascript
jQuery html() in Firefox (uses .innerHTML) ignores DOM changes
2010/03/05 Javascript
JQuery,Extjs,YUI,Prototype,Dojo 等JS框架的区别和应用场景简述
2010/04/15 Javascript
javascript 系统文件夹文件操作及参数介绍
2013/01/08 Javascript
js编码、解码函数介绍及其使用示例
2013/09/05 Javascript
JQuery对表单元素的基本操作使用总结
2014/07/18 Javascript
jquery操作复选框checkbox的方法汇总
2015/02/05 Javascript
javascript比较两个日期相差天数的方法
2015/07/23 Javascript
jQuery动态添加可拖动元素完整实例(附demo源码下载)
2016/06/21 Javascript
bootstrap实现每隔5秒自动轮播效果
2016/12/20 Javascript
javascript事件的绑定基础实例讲解(34)
2017/02/14 Javascript
vue调试工具vue-devtools安装及使用方法
2018/11/07 Javascript
ES6 迭代器与可迭代对象的实现
2019/02/11 Javascript
js实现幻灯片轮播图
2020/08/14 Javascript
[01:04:48]VGJ.S vs TNC Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
Python XML RPC服务器端和客户端实例
2014/11/22 Python
编写Python脚本抓取网络小说来制作自己的阅读器
2015/08/20 Python
Python基于pygame实现的font游戏字体(附源码)
2015/11/11 Python
python的一些加密方法及python 加密模块
2019/07/11 Python
Python 如何优雅的将数字转化为时间格式的方法
2019/09/26 Python
Python openpyxl 插入折线图实例
2020/04/17 Python
Pytorch 解决自定义子Module .cuda() tensor失败的问题
2020/06/23 Python
用Python 爬取猫眼电影数据分析《无名之辈》
2020/07/24 Python
Python 如何操作 SQLite 数据库
2020/08/17 Python
巴西一家专门从事家居和装饰的连锁店:Camicado
2019/08/14 全球购物
英语翻译系毕业生求职信
2013/09/29 职场文书
毕业生实习鉴定
2013/12/11 职场文书
抗震救灾标语
2014/06/26 职场文书
2014年妇幼卫生工作总结
2014/12/09 职场文书
2014年银行年终工作总结
2014/12/19 职场文书
奶茶店的创业计划书该怎么写?
2019/07/15 职场文书
一文搞懂php的垃圾回收机制
2021/06/18 PHP
Nginx性能优化之Gzip压缩设置详解(最大程度提高页面打开速度)
2022/02/12 Servers