Laravel 403 之前有效的 API 错误

Pet*_*ete 5 php wampserver cors laravel ionic-framework

截至昨天,下面的代码有效。然而今天,我必须在 laravel 中运行 php artisan config:cache 命令,因为我添加了一个包,现在我漂亮的 ionic 应用程序不想运行连接到任何东西,因为我不断收到此 403 错误。

在我安装“rap2hpoutre/laravel-log-viewer”:“^0.19.1”并缓存后开始出现错误,但我认为这与它没有任何关系。我很确定我的缓存在此之前是最新的。

jwt 产生相同的错误。

以前,该应用程序无需 cors 插件即可运行。

它在本地和我的服务器上向我提供了此错误(因为我也必须在那里缓存)。

这个错误与我之前调试时遇到的错误不同。

当我在 chrome 中正常拉取路线http://xxx/api/home时- 它返回正常...在邮递员中相同

感谢您的帮助!

错误

选项http://xxx/api/home 403(禁止)无法加载http://xxx/api/home:预检响应没有 HTTP 正常状态。

离子

basicGet_no_token(rl){
        console.log('basicGet - ' + this.base_url + rl);
        return new Promise((resolve, reject) => {
            this.http.get(this.base_url + rl, 
                {headers: new HttpHeaders({
                    'Content': 'application/json',
                    'Accept': 'application/json',
                })})
                .subscribe(res => {console.log(res);resolve(res);}, (err) => {this.handleError(err);console.log(err);reject(err);});
        }); 
    }
Run Code Online (Sandbox Code Playgroud)

拉拉维尔

Route::group(['middleware' => ['addHeaders']], function () {
    Route::get('home', 'api\WelcomeController@home');
});

class addHeaders
{
    public function handle($request, Closure $next)
    {
    if ($request->getMethod() == "OPTIONS") {
      return response(['OK'], 200)
        ->withHeaders([
          'Access-Control-Allow-Origin' => '*',
          'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE',
          'Access-Control-Allow-Credentials' => true,   
          'Access-Control-Allow-Headers' => 'Origin, Content-Type, X-Auth-Token, authorization, X-Requested-With'
        ]);
    }

    return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
        ->header('Access-Control-Allow-Credentials', true)
        ->header('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Auth-Token, authorization, X-Requested-With');

    }
}

class WelcomeController extends Controller
{
  public function home()
  {
        $r['message']="Welcome!";
    $r['allow']=true;
    $p=compact('r');
    return response()->json($p, 200);
  }
}


class Kernel extends HttpKernel
{

    protected $middlewareGroups = [
          'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    protected $routeMiddleware = [
        'auth'      => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic'  => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings'    => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can'       => \Illuminate\Auth\Middleware\Authorize::class,
        'guest'     => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'jwt'           => \App\Http\Middleware\JWT::class,
        'addHeaders'    => \App\Http\Middleware\addHeaders::class,
      'jwt.auth'    => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
      'jwt.refresh'   => \Tymon\JWTAuth\Middleware\RefreshToken::class,        
        'throttle'    => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];
}
Run Code Online (Sandbox Code Playgroud)

Teo*_*gır 11

这个答案可能不适合所有人,但如果您创建了自定义请求并忘记授权方法返回 true,您可能会遇到此错误。

我正在处理策略授权,经常忘记请求验证。创建自定义请求时请仔细检查

注意:另请注意,如果您正在使用策略,您可能会忘记将策略映射到模型中AuthServiceProvider


Bar*_*art 4

正如一开始所述,您正在使用缓存 - 至少其中一个用于config.

有些包与 Laravel 的缓存机制不能很好地配合,有时你只是忘记你正在使用任何缓存 - 你应该永远记住这一点!

因此,不要在开发中使用任何缓存(这是我个人的偏好,不会因为不存在的问题而浪费时间)。

在生产中,在部署时,您需要绝对确保所有缓存都将被重新创建。

这些功能对您来说可能很困难:

php artisan cache:clear

php artisan route:clear

php artisan config:clear

php artisan view:clear

所以请检查一下它们...