laravel5.1基本的http auth for API

时间:2016-04-19 06:59:57

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

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

我目前发现的所有工作都不起作用,是不是有一个简单的代码示例怎么做?我在教程中找到的只是使用users表,但是对于我的API,这会被重载,因为我只需要一个帐户,我想将其硬编码到控制器中。

1 个答案:

答案 0 :(得分:0)

通过用户名&每次调用时标头中的密码,并使用控制器构造函数中的硬编码凭证进行检查。

示例:

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;
    }
  }
}
相关问题