2017-04-27 118 views
3

我敢出任何想法現在。使用自定義中間件重定向我到/ home,即使中間件爲空Laravel

的情況是:我有一個API-端點的路徑(工作正常,響應JSON等)。

如果我現在內置的「權威性」中間件應用到路線,我總是重定向到/ home路線。看起來我正在做某事。 auth錯誤?我想錯了,這是因爲:

這種奇怪的重定向也踢,如果我不使用「權威性」,但一個自定義的中間件,沒有包含任何內容,但

public function handle($request, Closure $next) 
{ 
    print "WTF"; 
    throw new AuthenticationException('Unauthenticated.'); 
} 

[打印和異常從不拋出!我在/ home上再次登陸沒有錯誤]

並非所有的中間件生產這種錯誤。例如,「auth.basic」和「網絡」只是做工精細用這條路線。

我根據一些結果我發現,說,使用「網」解決了類似的問題對他們來說同樣適用「網絡」和我自己的中間件既路線,但你猜怎麼着,什麼都沒有改變。

TL:DR:如果我使用我自己的中間件或'auth',我會在中間件本身執行之前得到重定向。

Update: After fiddling around with the code and the great tipp from mnies, I found this very curious Bug:

如果我只是取消註釋的AuthenticationException,突然我的代碼如預期運行。這可能是加載Exception-Class調用RedirectIfAuthenticated Middleware ?! - 這絕對是所謂的!

現在簡單的解決方案是,對我的自定義中間件使用不同的異常,但問題是默認的'auth'-MW也使用此異常,因此導致相同的Bug。

記住:我沒有使用其他中間件不僅僅是這個自己一個人,這個bug似乎真的加載異常,像跆拳道?

所以我仍然需要幫助爲什麼會這樣!

Bug: using

throw new AuthenticationException('Unauthenticated.', []); 

原因「guest'兆瓦(RedirectIfAuthenticated)被調用,而不是預期的MW-堆! (原MW-堆的什麼都沒有執行,而不管訂單。

Update 2: It seems that RedirectIfAuthenticated is thrown only because I got redirected before to the /login route (and from there as described to /home through it), but that doesn't change the issue that this random redirect occurs.

[我想ATM重現全新安裝這個Bug]

Update 3: I was not able to reproduce the bug in a fresh installation with Laravel 5.4.19.... Trying to compare both installations now. D:

使用Laravel 30年3月5日。

我的一些上下文代碼:

路線:

Route::get('/admin/user/get', ['middleware' => ['simpleauth', 'web'], 'uses' => 'UserEditAdmin\Controllers\[email protected]']); 

定製中間件:

class SimpleAuth 
{ 

    public function __construct(){} 

    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    * @throws AuthenticationException 
    */ 
    public function handle($request, Closure $next) 
    { 
     print "WTF"; 
     throw new AuthenticationException('Unauthenticated.'); 
    } 
} 

從Kernel.php '網絡':

protected $middlewareGroups = [ 
    'web' => [ 
     \App\Http\Middleware\EncryptCookies::class, 

    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, 
     \Illuminate\Session\Middleware\StartSession::class, 
     \Illuminate\View\Middleware\ShareErrorsFromSession::class, 
     \App\Http\Middleware\VerifyCsrfToken::class, 
     \Illuminate\Routing\Middleware\SubstituteBindings::class, 
    ], 

    'api' => [ 
     'throttle:60,1', 
     'bindings', 
    ], 
]; 
+0

如果你可以在Github上放一個這個問題的最簡單的例子,那將是非常好的。 – mniess

回答

1

看看你的\App\Http\Kernel.php。看起來你總是打電話給\App\Http\Middleware\RedirectIfAuthenticated中間件(別名爲guest)。你想調試它,你可以在該中間件中引發一個異常,以獲得什麼時候調用的棧跟蹤。

+0

這真的好像是問題,從RedirectIfAuthentificated引發異常 - 但我沒有將它追加到路由...?我現在要去搜索,但是非常感謝:) - 我從上面的Kernel.php中添加了我的'web',我從來沒有調用RedirectIfAuthentificated ... –

+0

stacktrace應該告訴你更多。我的猜測是你在某處定義了「客人」,例如在你的控制器中。 – mniess

+0

可悲的是沒有堆棧跟蹤。沒有錯誤拋出任何地方。你可以嘗試一下自己,AuthenticatedException在內部被laravel捕獲,重定向是結果(明顯不是預期的結果)...如果我現在在這裏寫下來,我可以在那裏搜索 - 明天我猜... 但我肯定知道:客人永遠不會在任何地方定義!我今天在這個問題上搜索了大約5個小時^^ [btw。你的想法] –