2016-04-06 30 views
2

ルーメンプロジェクトでLaravelのHTTP基本認証を使用しようとしています。ルーメンHTTP基本認証

routes.phpファイルで、私は私が認証する必要が敗走ためauth.basicミドルウェアを設定します。

$app->routeMiddleware([ 
    'auth.basic' => Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 
]); 
:私はミドルウェアおよび認証サービスプロバイダを登録している bootstrap.php

$app->get('/test', ['middleware' => 'auth.basic', function() { 
    return "test stuff"; 
}]); 

[...]

$app->register(App\Providers\AuthServiceProvider::class); 

しかし、試してみると誰もが、私は基本的な認証のためのガードのコードを取得する方法を知ってい

Fatal error: Call to undefined method Illuminate\Auth\RequestGuard::basic() in C:\source\lumen\vendor\illuminate\auth\Middleware\AuthenticateWithBasicAuth.php on line 38

http://lumen/testを訪問した経路は、私は次のエラーを取得しますか?

ありがとうございました。

答えて

0

は、同様の問題に遭遇したデータベース内のユーザーのための基本的な認証を使用していたので、私自身のAuthServiceProviderを書き終わったと登録されているブートストラップで/ 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; 
     }); 
    } 
}