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 相关文章推荐
PHP5 安装方法
Oct 09 PHP
php中通过smtp发邮件的类,测试通过
Jan 22 PHP
php中通过虚代理实现延迟加载的实现代码
Jun 10 PHP
php 模拟 asp.net webFrom 按钮提交事件实例
Oct 13 PHP
修改WordPress中文章编辑器的样式的方法详解
Dec 15 PHP
在openSUSE42.1下编译安装PHP7 的方法
Dec 24 PHP
Laravel中Trait的用法实例详解
Mar 16 PHP
PHP版微信第三方实现一键登录及获取用户信息的方法
Oct 14 PHP
ThinkPHP5框架缓存查询操作分析
May 30 PHP
PHP JWT初识及其简单示例
Oct 10 PHP
php layui实现前端多图上传实例
Jul 30 PHP
php桥接模式应用案例分析
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
德生S2000电路分析
2021/03/02 无线电
PHP 5.0对象模型深度探索之绑定
2006/09/05 PHP
数据库的日期格式转换
2006/10/09 PHP
ThinkPHP之M方法实例详解
2014/06/20 PHP
Zend Framework分页类用法详解
2016/03/22 PHP
PHP简单读取PDF页数的实现方法
2016/07/21 PHP
PHP对称加密函数实现数据的加密解密
2016/10/27 PHP
PHP编程快速实现数组去重的方法详解
2017/07/22 PHP
tp5(thinkPHP5)框架数据库Db增删改查常见操作总结
2019/01/10 PHP
jQuery学习笔记(1)--用jQuery实现异步通信(用json传值)具体思路
2013/04/08 Javascript
js实现日历可获得指定日期周数及星期几示例分享(js获取星期几)
2014/03/14 Javascript
jQuery选择器源码解读(四):tokenize方法的Expr.preFilter
2015/03/31 Javascript
jquery+ajax实现上传图片并显示上传进度功能【附php后台接收】
2019/06/06 jQuery
javascript实现简易聊天室
2019/07/12 Javascript
浅谈Webpack4 Tree Shaking 终极优化指南
2019/11/18 Javascript
原生js+css调节音量滑块
2020/01/15 Javascript
Vue+Element-U实现分页显示效果
2020/11/15 Javascript
[02:39]DOTA2英雄基础教程 天怒法师
2013/11/29 DOTA
python用ConfigObj读写配置文件的实现代码
2013/03/04 Python
简单的python协同过滤程序实例代码
2018/01/31 Python
Python3.7 新特性之dataclass装饰器
2019/05/27 Python
Python使用指定端口进行http请求的例子
2019/07/25 Python
python批量处理文件或文件夹
2020/07/28 Python
Python 窗体(tkinter)下拉列表框(Combobox)实例
2020/03/04 Python
matplotlib 对坐标的控制,加图例注释的操作
2020/04/17 Python
python3.9实现pyinstaller打包python文件成exe
2020/12/13 Python
美国二手复古奢侈品包包购物网站:LXRandCo
2019/06/18 全球购物
LACOSTE波兰官网:Polo衫、服装和鞋类
2020/09/29 全球购物
元旦晚会上单位领导演讲稿
2014/01/05 职场文书
服装采购员岗位职责
2014/03/15 职场文书
2014年青年教师工作总结
2014/12/17 职场文书
关于长城的导游词
2015/01/30 职场文书
Python内置数据结构列表与元组示例详解
2021/08/04 Python
Golang Elasticsearches 批量修改查询及发送MQ
2022/04/19 Golang
Nginx配置之禁止指定IP访问
2022/05/02 Servers
详解NumPy中的线性关系与数据修剪压缩
2022/05/25 Python