【问题标题】:Laravel jwtauthLaravel jwtauth
【发布时间】:2016-08-23 04:03:02
【问题描述】:

我正在尝试在Laravel 5.2 中设置JWTAuth。我已经安装了所有东西,当我尝试获取令牌时它成功了。

路线文件:

$api = app('Dingo\Api\Routing\Router');

$api->version('v1',function($api)
{
    $api->group(['prefix' => 'v1'], function($api)
    {
        $api->post('login','App\Http\Controllers\Auth\AuthController@authenticate');
    });
});

AuthController@authenticate:

public function authenticate(Request $request)
    {
        // grab credentials from the request
        $credentials = $request->only('email', 'password');

        try {
            // attempt to verify the credentials and create a token for the user
            if (! $token = JWTAuth::attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        } catch (JWTException $e) {
            // something went wrong whilst attempting to encode the token
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        // all good so return the token
        return response()->json(compact('token'));
    }

结果:

{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL2xuanN1cHBvcnQuZGV2XC9hcGlcL3YxXC9sb2dpbiIsImlhdCI6MTQ2MTg0MzM1OCwiZXhwIjoxNDYxODQ2OTU4LCJuYmYiOjE0NjE4NDMzNTgsImp0aSI6IjEyMThiNWUxNTNmNTBhMTA1ZTBhYTE1ZTlhMjRiYjNlIn0.0MpOWMvd2swqI-3r9hNjkjmrpVgNIDds0srlgjXKFVg"}

然后当我尝试获得这样的用户时:

Route file(网址:/users)(带中间件):

$api = app('Dingo\Api\Routing\Router');

$api->version('v1',function($api)
{
    $api->group(['prefix' => 'v1', 'middleware' => 'api.auth'], function($api)
    {
        $api->post('login','App\Http\Controllers\Auth\AuthController@authenticate');
        $api->post('users','App\Http\Controllers\Auth\AuthController@index');
    });
});

AuthController@index

public function index()
{
    return User::all();
}

标题: 授权持有者 {token})

我收到错误:

NotFoundHttpException in RouteCollection.php line 161:

这里有什么问题?

--EDIT1--

当我给出错误的令牌时,我收到错误:

{"message":"Could not decode token: The token 

--EDIT2--

+------+--------+---------------+------+-------------------------------------------------------+-----------+------------+----------+------------+
| Host | Method | URI           | Name | Action                                                | Protected | Version(s) | Scope(s) | Rate Limit |
+------+--------+---------------+------+-------------------------------------------------------+-----------+------------+----------+------------+
|      | POST   | /api/v1/login |      | App\Http\Controllers\Auth\AuthController@authenticate | No        | v1         |          |            |
|      | POST   | /api/v1/users |      | App\Http\Controllers\Auth\AuthController@index        | No        | v1         |          |            |
+------+--------+---------------+------+-------------------------------------------------------+-----------+------------+----------+------------+

【问题讨论】:

  • 您是访问/v1/users 还是只是访问/users
  • 我访问 /api/v1/users 我使用相同的 url 来接收令牌,所以它应该可以工作。(在我的 dingo api 文件中,我设置了 api 的前缀)
  • 您能展示一下php artisan route:list 生成的内容吗?
  • 它显示您的应用程序没有任何路由。我猜是因为我用澳洲野狗?
  • 没错!它的api:routes这个命令

标签: php laravel-5.2 jwt


【解决方案1】:

使用GET方法:

$api->get('users','App\Http\Controllers\Auth\AuthController@index');

【讨论】:

  • 谢谢,不过没关系。仍然收到同样的错误。
【解决方案2】:

发现我的 AuthController 中激活了一个中间件:

public function __construct()
{
   $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}

【讨论】:

  • 我明白了,那是不可能的。
猜你喜欢
  • 2016-08-08
  • 2016-09-01
  • 2017-10-17
  • 2019-03-26
  • 2016-04-23
  • 2017-11-05
  • 2019-07-20
  • 2017-03-31
  • 2021-04-01
相关资源
最近更新 更多