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中的函数-- foreach()的用法详解
Jun 24 PHP
PHP中time(),date(),mktime()区别介绍
Sep 28 PHP
PHP mkdir()无写权限的问题解决方法
Jun 19 PHP
php中Ctype函数用法详解
Dec 09 PHP
详解WordPress中调用评论模板和循环输出评论的PHP函数
Jan 05 PHP
php获取文件后缀的9种方法
Mar 22 PHP
php 实现301重定向跳转实例代码
Jul 18 PHP
php 使用fopen函数创建、打开文件详解及实例代码
Sep 24 PHP
php nginx 实时输出的简单实现方法
Jan 21 PHP
PHP架构及原理知识点详解
Dec 22 PHP
php生成随机数/生成随机字符串的方法小结【5种方法】
May 27 PHP
TP5多入口设置实例讲解
Dec 15 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
收音机发烧友应当熟知的100条知识
2021/03/02 无线电
PHP+DBM的同学录程序(2)
2006/10/09 PHP
PHP Squid中可缓存的动态网页设计
2008/09/17 PHP
php+mysql结合Ajax实现点赞功能完整实例
2015/01/30 PHP
最新制作ThinkPHP3.2.3完全开发手册
2015/11/23 PHP
thinkPHP显示不出验证码的原因与解决方法分析
2017/05/20 PHP
各浏览器对link标签onload/onreadystatechange事件支持的差异分析
2011/04/27 Javascript
js操作滚动条事件实例
2015/01/29 Javascript
webpack中引用jquery的简单实现
2016/06/08 Javascript
AngularJS+Bootstrap实现多文件上传与管理
2016/11/08 Javascript
基于ExtJs在页面上window再调用Window的事件处理方法
2017/07/26 Javascript
解决Mac安装thrift因bison报错的问题
2018/05/17 Javascript
在vue.js中使用JSZip实现在前端解压文件的方法
2018/09/05 Javascript
解决vue-cli webpack打包后加载资源的路径问题
2018/09/25 Javascript
Vue使用.sync 实现父子组件的双向绑定数据问题
2019/04/04 Javascript
python根据出生年份简单计算生肖的方法
2015/03/27 Python
用Python编写一个简单的Lisp解释器的教程
2015/04/03 Python
Win7 64位下python3.6.5安装配置图文教程
2020/10/27 Python
Python迭代器与生成器基本用法分析
2018/07/26 Python
Python WSGI的深入理解
2018/08/01 Python
PyCharm 创建指定版本的 Django(超详图解教程)
2019/06/18 Python
Python中的list与tuple集合区别解析
2019/10/12 Python
python利用dlib获取人脸的68个landmark
2019/11/27 Python
Python ellipsis 的用法详解
2020/11/20 Python
CSS3中利用animation属性创建雪花飘落特效
2014/05/14 HTML / CSS
美国顶级防滑鞋:Shoes For Crews
2017/03/27 全球购物
小天鹅官方商城:LittleSwan
2017/06/16 全球购物
在子网210.27.48.21/30种有多少个可用地址?分别是什么?
2014/07/27 面试题
开会迟到检讨书
2014/01/08 职场文书
医生进修自我鉴定
2014/01/19 职场文书
兰兰过桥教学反思
2014/02/08 职场文书
后勤主管岗位职责
2014/03/01 职场文书
大学生实习鉴定评语
2014/04/25 职场文书
今日说法观后感
2015/06/08 职场文书
信息简报范文
2015/07/21 职场文书
python四种出行路线规划的实现
2021/06/23 Python