简体   繁体   English

Lumen / Laravel-自定义身份验证中间件错误

[英]Lumen/Laravel - Custom authentication middleware erroring out

I've created authentication middleware for a lumen project. 我已经为流明项目创建了身份验证中间件。 The authentication uses an external API, but unless I'm mistaken, that's not the issue here. 身份验证使用外部API,但是除非我弄错了,否则这不是问题。 The middleware correctly redirects me to the login page if I'm not authenticated, and correctly logs me in and saves it in the session when I put in good credentials. 如果我未通过身份验证,中间件会正确地将我重定向到登录页面,并在我输入良好凭据后正确地将我登录并保存在会话中。 But it won't display the internal homepage as long as I'm using the middleware. 但是,只要我使用中间件,它就不会显示内部主页。 The "handle" method in App\\Http\\Middleware\\Authenticate.php seems to have no Response in $next($request) to return and I'm not sure why. App \\ Http \\ Middleware \\ Authenticate.php中的“ handle”方法似乎在$ next($ request)中没有响应以返回,我不确定为什么。

If my UserProvider implementation or AuthController is needed, let me know. 如果需要我的UserProvider实现或AuthController,请告诉我。

Error Message: 错误信息:

ReflectionException in Container.php line 554: Function () does not exist

in Container.php line 554
at ReflectionFunction->__construct('') in Container.php line 554
at Container->getCallReflector(null) in Container.php line 531
at Container->getMethodDependencies(null, array()) in Container.php line 500
at Container->call(null, array()) in Application.php line 1286
at Application->callActionOnArrayBasedRoute(array(true, array('middleware' => 'bankerAuth'), array())) in Application.php line 1255
at Application->Laravel\Lumen\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Authenticate.php line 44
at Authenticate->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Authenticate), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Application.php line 1411
at Application->sendThroughPipeline(array('App\Http\Middleware\Authenticate'), object(Closure)) in Application.php line 1256
at Application->handleFoundRoute(array(true, array('middleware' => 'bankerAuth'), array())) in Application.php line 1179
at Application->Laravel\Lumen\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 54
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Application.php line 1411
at Application->sendThroughPipeline(array('Illuminate\Cookie\Middleware\EncryptCookies', 'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse', 'Illuminate\Session\Middleware\StartSession', 'Illuminate\View\Middleware\ShareErrorsFromSession'), object(Closure)) in Application.php line 1185
at Application->dispatch(null) in Application.php line 1125
at Application->run() in index.php line 28

Relevant code: 相关代码:

.env .env

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=database
AUTH_DRIVER=bankerAuth

app.php app.php

$app->middleware([
    Illuminate\Cookie\Middleware\EncryptCookies::class,
    Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    Illuminate\Session\Middleware\StartSession::class,
    Illuminate\View\Middleware\ShareErrorsFromSession::class,
]);

$app->routeMiddleware([
    'bankerAuth' => \App\Http\Middleware\Authenticate::class,
//    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class
]);

$app['auth']->extend('bankerAuth', function()
{
    return new App\Banker\BankerUserProvider;
});

routes.php routes.php

$app->get('/', ['middleware' => 'bankerAuth'], function () use ($app) {
    return 'Home page';
});


$app->get('/login', function () {

    return view('login');
});


$app->post('/login', ['uses' => 'AuthController@postLogin'], function() {

});

App\\Http\\Middleware\\Authenticate.php App \\ Http \\ Middleware \\ Authenticate.php

<?php


namespace App\Http\Middleware;


use Closure;
use Illuminate\Contracts\Auth\Guard;

class Authenticate
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest()) {
            session()->flash('url.intended', app('url')->full());
            return redirect()->to('/login', 302);
        }

        return $next($request);
    }
}

Your syntax in the route.php file is wrong. 您在route.php文件中的语法错误。

If you use a middleware in the route the function has to be in the array too: 如果在路由中使用中间件,则该功能也必须位于数组中:

$app->get('/', ['middleware' => 'bankerAuth', function () use ($app) {
    return 'Home page';
}]);

http://laravel.com/docs/5.1/middleware#registering-middleware http://laravel.com/docs/5.1/middleware#registering-middleware

Tested and absolutely working. 经过测试,绝对可以正常工作。 I added this in my dir/app/Http/routes.php file. 我将此添加到了dir / app / Http / routes.php文件中。

$app->get('/', function() use ($app) {
return $app->welcome(); });

$app->get('/home', function() use ($app) { return "Welcome to Homepage"; }); $app->get('/home', function() use ($app) { return "Welcome to Homepage"; });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM