Laravel 5.6 通过 oauth/令牌挂起 [英] Laravel 5.6 pass oauth/token hanging

查看:17
本文介绍了Laravel 5.6 通过 oauth/令牌挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Laravel Passport 安装设置了新的 Laravel 5.6 安装.如果我向 Postman 发送请求 http://127.0.0.1/oauth/token 我得到一个有效的令牌返回:

I have set up a new laravel 5.6 install with Laravel Passport install. If I make a post request to http://127.0.0.1/oauth/token with Postman I get a valid token back:

请求

POST /oauth/token HTTP/1.1
Host: 127.0.0.1:8000
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Cache-Control: no-cache
Postman-Token: 99529c07-0fe3-38a8-54cf-8b80a9dd5fbd

{ 
    "grant_type" : "password",
    "client_id" : 4, 
    "client_secret" : "Ib1UOS7BK12tFxOilqwea1XGJhrExbVYe8B7r8wK",
    "username" : "mail@mail.com",
    "password" : "password"
}

回复:

{
    "token_type": "Bearer",
    "expires_in": 31536000,
    "access_token": "eyJ0eXAiOiJKV1.....",
    "refresh_token": "def5020026dfeb6f91f6a9....."
}

我不希望我的用户直接访问它,所以我在 routes/web.php 文件中设置了一个路由:

I don't want my users accessing this directly so I have set up a route in the routes/web.php file:

Route::post('login', 'APIAuthController@login');

我的 AuthController 看起来像:

My AuthController looks like:

<?php

namespace AppHttpControllersAPI;

use AppOAuth;
use IlluminateHttpRequest;
use AppHttpControllersController as Controller;
use IlluminateSupportFacadesAuth;
use CarbonCarbon;
use AppUser;

class AuthController extends Controller
{

public function __construct()
{
    $this->middleware('auth:api', ['except' => ['login']]);
}

/**
 * Login user and create token
 *
 * @param  [string] email
 * @param  [string] password
 * @param  [boolean] remember_me
 * @return [string] access_token
 * @return [string] token_type
 * @return [string] expires_at
 */
public function login(Request $request)
{

    $request->validate([
        'username' => 'required|string|email',
        'password' => 'required|string'
    ]);

    $credentials = request(['username', 'password']);

    return OAuth::login($credentials['username'], $credentials['password']);

}

这会调用我的 OAuth 类的登录方法:

This calls the login method of my OAuth class:

namespace App;

use GuzzleHttp;

class OAuth
{

public static function login($username, $password)
{
    $http = new GuzzleHttpClient;

    $response = $http->post('http://127.0.0.1:8000/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => 4,
            'client_secret' => 'Ib1UOS7BK12tFxOilqwea1XGJhrExbVYe8B7r8wK',
            'username' => $username,
            'password' => $password
        ],
    ]);

    return json_decode((string) $response->getBody(), true);
}

}

当我使用 Postman 发出帖子请求以获取令牌时:

When I use Postman make a post request to get a token:

POST /login HTTP/1.1
Host: 127.0.0.1:8000
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Cache-Control: no-cache
Postman-Token: 94469bd8-a011-7e04-5f20-b835a5b1cac4

{
    "username" : "mail@mail.com",
    "password" : "password"
}

请求只是挂起.它不会以任何形式的响应或超时返回,它似乎永远加载.我不确定出了什么问题.如果我从邮递员那里向 oauth/token 发出 post 请求,它会起作用,但是如果我向控制器方法发出 post 请求,而控制器方法又向 oauth/token 发出 post 请求,则它不起作用.我什么也得不到.

The request just hangs. It doesn't come back with any sort of response or timeout, it just seems to load forever. I'm not sure what is going wrong. If I make a post request to oauth/token from postman it works, but if I make a post request to a controller method which in turn makes a post request to oauth/token, it doesn't work. I'm not getting anything back.

推荐答案

这是因为内置的 PHP 服务器是单线程.因此,您对 OAuth 服务器的第二个(内部)请求正在终止第一个.

This is because the built-in PHP server is single threaded. Hence your second (internal) request to the OAuth server is killing the first one.

见我的评论 此处.

这篇关于Laravel 5.6 通过 oauth/令牌挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆