我的带有令牌授权标头的angular 6 get HTTP请求不起作用

时间:2019-03-27 17:17:16

标签: angular laravel api authorization laravel-passport

我正在尝试向生产服务器上的API发出角度为6的get请求,并且我与它一起发送了授权令牌标头,但我一直在获取

错误

Here is my code 

authheader = {
    headers: new HttpHeaders({
      'Authorization':  'Bearer '+this.tokenr,
      'Content-Type': 'text/plain'
    })
  }

 public async getUserInfo() {
    return new Promise(async resolve => {
      this.http.get(this.realapi + '/user', this.authheader)
        .map(data => <any>data)
      .subscribe(data => {
        resolve(data);
        console.log(data, 'data here');
      }, err => {
        console.log(err,'errorrr');
      });
    });
  }

从laravel(passport)5.7 api成功登录后,我应该使用此端点来获取用户详细信息

下面是我在laravel中处理CORS和Preflight请求的方式

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Response;
class PreflightResponse
{
    /**
    * Handle an incoming request.
    *
    * @param \Illuminate\Http\Request $request
    * @param \Closure $next
    * @return mixed
    */
    public function handle($request, Closure $next )
    {
        if ($request->getMethod() == "OPTIONS") {
            return Response::make('OK', 200, $headers);
        }
        $response = $next($request);
        $response->header('Access-Control-Allow-Origin', '*');
        $response->header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, DELETE, OPTIONS');
        $response->header('Access-Control-Allow-Headers', 'Content-Type, Authorization, application/json');
        return $response;
    }
 }

我将其添加到了kernel.php文件中受保护的中间件中(因此它会通过所有请求)

很有趣,POST http请求有效,但GET却不行((

POST request carries the data/parameter and 'Content-Type':  'application/x-www-form-urlencoded' header but the GET request just using the 'Authorization'; 'Bearer '+token

0 个答案:

没有答案