我正在尝试在Lumen项目中使用Laravel的HTTP基本身份验证。
在routes.php
文件中,我为需要验证的路由设置了auth.basic中间件:
$app->get('/test', ['middleware' => 'auth.basic', function() {
return "test stuff";
}]);
开bootstrap.php
我已注册中间件和身份验证服务提供商:
$app->routeMiddleware([
'auth.basic' => Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
]);
[...]
$app->register(App\Providers\AuthServiceProvider::class);
但是当我尝试通过访问http://lumen/test
来测试路线时,我收到以下错误:
Fatal error: Call to undefined method Illuminate\Auth\RequestGuard::basic() in C:\source\lumen\vendor\illuminate\auth\Middleware\AuthenticateWithBasicAuth.php on line 38
有谁知道如何获得基本身份验证的守卫代码?
感谢。
答案 0 :(得分:1)
遇到类似的问题,想为数据库中的用户使用基本身份验证,所以最后写了我自己的AuthServiceProvider并在bootstrap / app.php中注册了
这是课程,也许它可以帮助你。
<?php
namespace App\Providers;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\ServiceProvider;
class HttpBasicAuthServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Boot the authentication services for the application.
*
* @return void
*/
public function boot()
{
$this->app['auth']->viaRequest('api', function ($request) {
$email = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
if ($email && $password) {
$user = User::whereEmail($email)->first();
if (Hash::check($password, $user->password)) {
return $user;
}
}
return null;
});
}
}