簡體   English   中英

如果不允許路線,則返回403

[英]Return 403 if route is not allowed

我有帶有中間件的Route組(我使用Zizaco / entrust包):

Route::group(['as' => 'admin.', 'prefix' => 'admin', 'middleware' => ['role:admin']], function (){
    ...
});

當我未經身份驗證而嘗試輸入http://mysite/admin時,出現異常

Symfony \\組件\\ HttpKernel \\異常\\ HttpException
沒有訊息

但我想返回403。
我試圖這樣做:

Route::fallback(function(){
    abort(403);
});

但這沒有幫助。


編輯1:這里我們在Laravel 5.5.28中有異常句柄

public function abort($code, $message = '', array $headers = [])
    {
        if ($code == 404) {
            throw new NotFoundHttpException($message);
        }

        throw new HttpException($code, $message, null, $headers);
    }

如您所見,沒有403句柄。

您必須修改角色中間件的句柄方法

public function handle($request, Closure $next, $roles)
{
    if (!is_array($roles)) {
        $roles = explode(self::DELIMITER, $roles);
    }
    if ($this->auth->guest() || !$request->user()->hasRole($roles)) {
        abort(403); // notice here it returns 403
    }
    return $next($request);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM