【问题标题】:Lumen HTTP Basic AuthenticationLumen HTTP 基本认证
【发布时间】:2016-07-27 10:41:34
【问题描述】:

我正在尝试在 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

有谁知道我怎样才能获得基本认证的守卫代码?

谢谢。

【问题讨论】:

    标签: php laravel authentication lumen basic-authentication


    【解决方案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;
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-06
      • 1970-01-01
      • 2012-01-02
      • 2011-05-06
      • 1970-01-01
      • 2011-05-03
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多