资讯专栏INFORMATION COLUMN

laravel auth 认证

Lionad-Morotar / 2222人阅读

摘要:如果两个经哈希运算的密码相匹配那么将会为这个用户开启一个认证。如果认证成功的话方法将会返回。重定向器上的方法将用户重定向到登录之前用户想要访问的,在目标无效的情况下回退将会传递给该方法。最后如有错误,欢迎指出交流群

Auth认证 路由

从路由开始,找到源码,再进行研究
找到根目录下面的

vendor/laravel/framework/src/Illuminate/Routing/Router.php

在282-303之间,具体代码如下:

    /**
     * Register the typical authentication routes for an application.
     *
     * @return void
     */
    public function auth()
    {
        // Authentication Routes...
        $this->get("login", "AuthLoginController@showLoginForm")->name("login");
        $this->post("login", "AuthLoginController@login");
        $this->post("logout", "AuthLoginController@logout");

        // Registration Routes...
        $this->get("register", "AuthRegisterController@showRegistrationForm");
        $this->post("register", "AuthRegisterController@register");

        // Password Reset Routes...
        $this->get("password/reset", "AuthForgotPasswordController@showLinkRequestForm");
        $this->post("password/email", "AuthForgotPasswordController@sendResetLinkEmail");
        $this->get("password/reset/{token}", "AuthResetPasswordController@showResetForm");
        $this->post("password/reset", "AuthResetPasswordController@reset");
    }

其中一共有9种路由,登录三种,注册两种,重置密码有四种

登录部分(login)

登录有3种,一种get方式的,两种post方式的,显然get方式的用来获取登录页面,这个没啥好说啦,其中一种POST方式的是用来登出的,也就是logout,一种是登入,也就是login。

login($this->post("login", "AuthLoginController@login");)

找到

vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

(如果不知道怎么找源码的话,直接在编辑器中全局搜索showLoginForm)

在23-56行,就是我们的login方法

/**
     * Handle a login request to the application.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    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.
        if ($lockedOut = $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        $credentials = $this->credentials($request);

        if ($this->guard()->attempt($credentials, $request->has("remember"))) {
            return $this->sendLoginResponse($request);
        }

        // 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 (! $lockedOut) {
            $this->incrementLoginAttempts($request);
        }

        return $this->sendFailedLoginResponse($request);
    }

也就是说,我们在页面上面点击登录按钮,就会将请求提交到该方法。
下面我们来看看login方法的具体实现
首先

$this->validateLogin($request);

具体如58~69行:

/**
 * Validate the user login request.
 *
 * @param  IlluminateHttpRequest  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->username() => "required", "password" => "required",
    ]);
}

这里只是校验了用户名以及密码是否为非空

回到login方法,校验完后

if ($lockedOut = $this->hasTooManyLoginAttempts($request)) {
    $this->fireLockoutEvent($request);

    return $this->sendLockoutResponse($request);
}

这里面主要是对用户登录失败次数的限制,如果登录失败次数过多就限制用户登录

接着,最重要的部分到啦
$credentials = $this->credentials($request);

具体方法在71~80行,如下:

/**
 * Get the needed authorization credentials from the request.
 *
 * @param  IlluminateHttpRequest  $request
 * @return array
 */
protected function credentials(Request $request)
{
    return $request->only($this->username(), "password");
}

这里返回了request请求里面的‘$this->username()’也就是email字段以及password字段的数据
然后根据上面得到的数据,调用guard()进行用户认证

if ($this->guard()->attempt($credentials, $request->has("remember"))) {
    return $this->sendLoginResponse($request);
}

由guard()具体代码可以看到(152-160行):

/**
 * Get the guard to be used during authentication.
 *
 * @return IlluminateContractsAuthStatefulGuard
 */
protected function guard()
{
    return Auth::guard();
}

显然用户的具体账号密码的认证用到了laravel的门面(Facades)来实现最后的用户认证

attempt($credentials, $request->has("remember"))

最后,

if (! $lockedOut) {
    $this->incrementLoginAttempts($request);
}
return $this->sendFailedLoginResponse($request);

如果认证不通过,这里将增加一次失败的次数并返回相对应的信息

经过上面的分析,如果我们不想使用laravel自带的认证的话,我们可以直接使用 Laravel 认证类来管理用户认证,例如

 $email, "password" => $password])) {
            // Authentication passed...
            return redirect()->intended("dashboard");
        }
    }
}

attempt 方法接收键值数组对作为第一个参数,数组中的值被用于从数据表中查找用户,因此,在上面的例子中,用户将会通过email 的值获取,如果用户被找到,经哈希运算后存储在数据中的密码将会和传递过来的经哈希运算处理的密码值进行比较。如果两个经哈希运算的密码相匹配那么将会为这个用户开启一个认证Session。

如果认证成功的话 attempt 方法将会返回 true。否则,返回 false。
重定向器上的 intended 方法将用户重定向到登录之前用户想要访问的 URL,在目标 URL 无效的情况下回退 URI 将会传递给该方法。

如果需要的话,除了用户邮件和密码之外还可以在认证查询时添加额外的条件,例如,我们可以验证被标记为有效的用户:

if (Auth::attempt(["email" => $email, "password" => $password, "active" => 1])) {
    // The user is active, not suspended, and exists.
}

注:在这些例子中,并不仅仅限于使用 email 进行登录认证,这里只是作为演示示例,你可以将其修改为数据库中任何其他可用作“username”的字段。

最后

如有错误,欢迎指出


QQ交流群:489832466

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/22953.html

相关文章

  • Laravel核心解读 -- 用户认证系统(基础介绍)

    摘要:系统的核心是由的认证组件的看守器和提供器组成。使用的认证系统,几乎所有东西都已经为你配置好了。其配置文件位于,其中包含了用于调整认证服务行为的注释清晰的选项配置。 用户认证系统(基础介绍) 使用过Laravel的开发者都知道,Laravel自带了一个认证系统来提供基本的用户注册、登录、认证、找回密码,如果Auth系统里提供的基础功能不满足需求还可以很方便的在这些基础功能上进行扩展。这篇...

    RebeccaZhong 评论0 收藏0
  • Laravel 多用户认证系统改造方案

    摘要:本文基于,主要介绍如何针对多站点分别进行用户认证的改造,用意是最大限度利用自带的认证系统。具体方案为清晰起见,项目按照不同站点组织成不同模块。学院版用户认证文档版用户认证文档更详细学院版验证文档版验证文档更详细翁航版多用户认证方案 原文发表于 http://www.jianshu.com/p/d6c112f27661 showImg(https://segmentfault.com/i...

    paulli3 评论0 收藏0
  • Laravel核心解读 -- 扩展用户认证系统

    摘要:扩展用户认证系统上一节我们介绍了系统实现的一些细节知道了是如何应用看守器和用户提供器来进行用户认证的,但是针对我们自己开发的项目或多或少地我们都会需要在自带的看守器和用户提供器基础之上做一些定制化来适应项目,本节我会列举一个在做项目时遇到的 扩展用户认证系统 上一节我们介绍了Laravel Auth系统实现的一些细节知道了Laravel是如何应用看守器和用户提供器来进行用户认证的,但是...

    王伟廷 评论0 收藏0
  • Laravel 5.5 使用 Jwt-Auth 实现 API 用户认证以及无痛刷新访问令牌

    摘要:默认的时间为周。大概意思就是如果用户有一个,那么他可以带着他的过来领取新的,直到周的时间后,他便无法继续刷新了,需要重新登录。指定在刷新令牌时要保留的声明密钥。为了使令牌无效,您必须启用黑名单。指定用于对用户进行身份验证的提供程序。 showImg(https://segmentfault.com/img/remote/1460000012606251?w=1920&h=1280); ...

    xavier 评论0 收藏0
  • Laravel核心解读--用户认证系统的实现细节

    摘要:通过装载看守器和用户提供器装载看守器和用户提供器用到的方法比较多,用文字描述不太清楚,我们通过注解这个过程中用到的方法来看具体的实现细节。 用户认证系统的实现细节 上一节我们介绍来Laravel Auth系统的基础知识,说了他的核心组件都有哪些构成,这一节我们会专注Laravel Auth系统的实现细节,主要关注Auth也就是AuthManager是如何装载认证用的看守器(Guard)...

    NicolasHe 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<