【问题标题】:Changing the authentication procedure in Laravel 5.2在 Laravel 5.2 中更改身份验证过程
【发布时间】:2017-11-23 00:24:11
【问题描述】:

我正在使用 Laravel 5.2

我在我的 authController.php 中修改了 postLogin() 方法。

public function postLogin(Request $request)
    {
          $inputs = $request->all();

          $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();

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

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

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

          if ($inputs['email'] == 'test@test.com') {
              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->incrementLoginAttempts($request);
          }

          return $this->sendFailedLoginResponse($request);

    }

现在,如果我在电子邮件表单中输入不同于 test@test.com 的内容,我会返回登录页面,并显示此用户无法识别的错误。但是,如果我使用正确的电子邮件 test@test.com,那么我可以看到它转到正确的页面(重定向有效),然后返回登录页面,就好像我没有经过身份验证一样。

我想这是因为它没有设置 cookie 或类似的东西向 Laravel 表明用户已通过身份验证。

我试图实现的是: 事实上,我想改变我的身份验证方式,并使用用户名密码向我的公司活动目录发出请求,它会给我发回真假。这样我就不需要使用本地数据库来存储密码。所以它需要首先检查电子邮件是否在本地数据库中,然后它将通过 curl 连接到我公司的 AD 并得到响应,然后我会知道用户是否输入了正确的密码。

【问题讨论】:

  • 检查你的中间件是否有正确的方法来验证检查?

标签: laravel


【解决方案1】:

您忘记对用户进行实际身份验证。看看这个示例代码:

if (Auth::attempt($credentials) {
    return redirect()->intended($this->redirectPath());
} else {
    return redirect($this->loginPath()) // login route
        ->withInput($request->only('email'))
        ->withErrors([
            'email' => $this->getFailedLoginMessage(),
        ]);
}

另外,你想用这个来达到什么目的?您想绕过 test@test.com 用户的身份验证以进行测试吗?我建议您为此创建一个新用户(在php artisan tinker 或其他范围内)。

但是,如果您只想通过电子邮件对用户进行身份验证,则需要覆盖 validateLogin 方法以仅验证 email 输入,并覆盖 getCredentials 方法以仅获取 @ 987654327@ 来自$request

【讨论】:

  • 不,事实上我想改变我的身份验证方式,并使用用户名密码向我的公司活动目录发出请求,它会向我发送 true 或 false。这样我就不需要使用本地数据库来存储密码。所以它需要首先检查电子邮件是否在本地数据库中,然后它会通过 curl 连接到我公司的 AD 并得到响应,然后我会知道用户是否输入了正确的密码。
猜你喜欢
  • 2016-08-05
  • 2017-02-26
  • 2016-04-23
  • 2018-11-22
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 2017-07-03
相关资源
最近更新 更多