Handler.php 第 133 行中的 HttpException:此操作未经授权

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42215055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:33:56  来源:igfitidea点击:

HttpException in Handler.php line 133: This action is unauthorized

phplaravellaravel-5.3

提问by Kvnamo

I have created an application using laravel 5.3 and it is working fine on localhost but after I uploded all my code on a server I have this error:

我已经使用 laravel 5.3 创建了一个应用程序,它在 localhost 上运行良好,但是在我将所有代码上传到服务器上后,出现此错误:

Symfony\Component\HttpKernel\Exception\HttpException in /home/project/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php line 133: This action is unauthorized.

This is happening when I try to call functions whithin my controllers using post.

当我尝试使用 post 在我的控制器中调用函数时会发生这种情况。

This is one example:

这是一个例子:

Route

路线

Route::group(['middleware' => 'auth'], function () {
    Route::group(['middleware' => 'admin'], function () {
         Route::post('admin/store/', 'Admin\AnnouncementController@store');
    });
});

Controller

控制器

protected function store(AnnouncementRequest $request) {
    return Auth::user()->id;
}

How can I fix this? Why is this not happening on my localhost?

我怎样才能解决这个问题?为什么这不会发生在我的本地主机上?

Thanks in advance.

提前致谢。

回答by user7830484

Check that your AnnouncementRequestfile is set to return true from authorize function. The default is to return false.

检查您的AnnouncementRequest文件是否设置为从授权函数返回 true。默认是返回false。

回答by Kaushik shrimali

If you can use CustomRequest method for validation then make sure to your authorize() return true. If you can set false then its never call your function as well throw the error This action is unauthorized

Solution

如果您可以使用 CustomRequest 方法进行验证,请确保您的 authorize() 返回 true。如果您可以设置 false 则它永远不会调用您的函数以及抛出错误 此操作未经授权

解决方案

class CopyRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
         return true;   //Default false .Now set return true;
    }
}

回答by Rashi Goyal

Default function return false so change it as shown below

默认函数返回false所以改变它如下所示

public function authorize()
    {
        return true;
    }

or also can use Auth in the Request

或者也可以在请求中使用 Auth

use Illuminate\Support\Facades\Auth;
 public function authorize()
    {
        return Auth::check();
    }

回答by Kvnamo

Well, for what I saw, there can be a lot of situations for this scenario. In my case, I was using a custom FormRequest named AnnouncementRequest. Inside that class I was checking for a role property on the auth user.

好吧,就我所见,这种情况可能有很多情况。就我而言,我使用的是自定义的FormRequest命名AnnouncementRequest。在该课程中,我正在检查 auth 用户的角色属性。

// before
public function authorize() {
    if(Auth::user()->role_id === 1) {
        return true;
    }

    return false;
}

My mistake was to use === instead == for validation. So after fixing that everything is working just fine.

我的错误是使用 === 代替 == 进行验证。所以在修复后一切正常。

// after
public function authorize() {
    if(Auth::user()->role_id == 1) {
        return true;
    }

    return false;
}

Anyway why did it worked on localhost but did not on the server remains a mystery for me...

无论如何,为什么它在本地主机上有效,但在服务器上却没有,这对我来说仍然是个谜......

回答by Whisky Dollar

The authorize() function by default it returns false, return true, your issue will be resolved

authorize() 函数默认返回false,返回true,你的问题就解决了

回答by Kumaravel K

In your Request file default not enable authorisation

在您的请求文件中默认不启用授权

public function authorize()
    {
        return false;
    }

if you enable your request file here showing that code.

如果您在此处启用显示该代码的请求文件。

public function authorize()
    {
        return true;
    }