【Laravel5.4】FormRequestを使うとHttpException This action is unauthorized が表示される。


【開発環境】

Amazon EC2 Linux
Windows 10 HOME
Apache/2.4.43
Laravel Framework 5.4.36
vsftpd: Ver 3.0.2

Tera Term 4.1.105
FFFTP Ver 4.7

Laravel で作成した画面で登録ボタンを押すと、、

こんなエラーメッセージが。。

【エラーメッセージ】

 (1/1) HttpException

This action is unauthorized.
in Handler.php line 133
at Handler->prepareException(object(AuthorizationException))in Handler.php line 109
at Handler->render(object(Request), object(AuthorizationException))in Handler.php line 47
at Handler->render(object(Request), object(AuthorizationException))in Pipeline.php line 82
at Pipeline->handleException(object(Request), object(AuthorizationException))in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure))in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure))in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in Pipeline.php line 102
at Pipeline->then(object(Closure))in Router.php line 574
at Router->runRouteWithinStack(object(Route), object(Request))in Router.php line 533
at Router->dispatchToRoute(object(Request))in Router.php line 511
at Router->dispatch(object(Request))in Kernel.php line 176
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request))in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure))in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure))in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure))in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure))in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))in Pipeline.php line 102
at Pipeline->then(object(Closure))in Kernel.php line 151
at Kernel->sendRequestThroughRouter(object(Request))in Kernel.php line 116
at Kernel->handle(object(Request))in index.php line 53

【対応】

調べていくとどうやら下記の passesAuthorization関数で

/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php

    protected function passesAuthorization()
    {
        if (method_exists($this, 'authorize')) {
            return $this->container->call([$this, 'authorize']);
        }
//#2020708 Edited by 
//         return false;
       return true;
    }

artisan make:requestコマンド実行時に入力パラメータをバリデーションする
FormRequestクラスを作成できます。

ですがそのままだと「false」を無条件に返しているらしく
ここを修正する必要があります。

無事画面上で登録処理が完了しました。
時間があればもう少し深く調査見てみようかな。

【参考】

【Laravel5.8】FormRequestを使うとThis action is unauthorized.が吐き出される

[Laravel] artisan make:requestでFormRequest使用時にThis action is unauthorized.エラーが出る時