【问题标题】:laravel5.1 basic http auth for APIlaravel5.1 API 的基本 http 身份验证
【发布时间】:2016-04-19 06:59:56
【问题描述】:

我正在开发基于 Web 的 API。为此,我不需要 laravel 的重载功能来使用表来存储 http auth 的登录详细信息。我只想通过使用硬编码的用户名和密码进行 HTTP 身份验证来保护一个功能(因此中间件也会过载)。

我现在发现的都不起作用,没有简单的代码示例如何做到这一点吗?我在教程中发现的只是使用 users 表,但对于我的 API,这会超载,因为我只需要一个我想硬编码到控制器中的帐户。

【问题讨论】:

  • 这可能看起来有点矫枉过正,但如果您想保护某些东西,您不应掉以轻心,也不要忽视一些更为矫枉过正的方法。最近我自己做了这个,我建议存储某种 API 密钥,或者在 User 表中使用用户名和密码,然后在您的请求中发送 - 然后您可以使用中间件来检查这个。设置起来非常简单,而且超级安全。

标签: php http authentication laravel-5.1 http-authentication


【解决方案1】:

在每次调用时在标头中传递用户名和密码,并检查控制器构造函数中的硬编码凭据。

例子:

use use Illuminate\Http\Request;

class MyController extends Controller
{
  public function __construct(Request $request)
  {
    parent::__construct();

    $username = $request->header('username');
    $password = $request->header('password');
    if($username !== 'Your Hardcoded username' || $password !== 'Hardcoded password') {
      throw new \Exception;
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-13
    • 2021-03-21
    • 2011-11-12
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多