Lumen JWT发送带有请求的令牌

时间:2017-05-22 05:20:17

标签: laravel api authorization jwt lumen

身份验证正在运行,我在auth中间件下有几条路由,每当我请求它时抛出:

{
  "message": "Failed to authenticate because of bad credentials or an invalid authorization header.",
  "status_code": 401
}

如何使用以下请求发送令牌:

Authorization bearer {{Long token}}

It works with `postman`, How can i send the token with request header, Or in any other best way.

路线:

$api->get('/categories', [
    'uses' => 'App\Http\Controllers\CategoryController@index',
    'as' => 'api.categories',
]);

方法:

public function index() {
    $lessons = \App\Category::all();
    $token = JWTAuth::getToken(); // $token have jwt token

    return response()->json([
        'data' => $lessons,
        'code' => 200,
    ]);
}

2 个答案:

答案 0 :(得分:1)

这个问题很难回答。请从下次更具体。根据您的评论,我终于可以意识到您想要从移动应用程序中使用api。

您需要在登录期间或注册期间或您拥有的任何其他身份验证方法/路由时返回为用户生成的令牌。移动应用程序需要读取此响应并在本地存储令牌。然后,应用程序需要在每个请求的请求标头中注入此标记。这是正常的api令牌工作流程。

应该对应用程序进行编码以读取来自请求的错误响应,如果它返回过期或无效令牌的错误,则应用程序需要清除本地存储的令牌,然后请求用户再次登录以生成新令牌。

答案 1 :(得分:0)

您可以使用:https://github.com/tymondesigns/jwt-auth

要求:     Laravel 4或5(见兼容性表)     PHP 5.4 +  脚步:     1:在require数组中的composer.json中添加以下行          “tymon / jwt-auth”:“0.5。*”     2:在终端中运行“composer update”     3:在此之后你必须注册服务提供商         转到config / app.php         并在提供程序数组中添加“Tymon \ JWTAuth \ Providers \ JWTAuthServiceProvider”         和'JWTAuth'=> 'Tymon \ JWTAuth \ Facades \ JWTAuth','JWTFactory'=> 'Tymon \ JWTAuth \ Facades \ JWTFactory'这个别名数组     4:发布pacakge:         “php artisan vendor:publis --provider =”Tymon \ JWTAuth \ Providers \ JWTAuthServiceProvider“     5:在配置文件中生成secrate键         'php artisan jwt:generate'     6:对于添加配置:https://github.com/tymondesigns/jwt-auth/wiki/Configuration

Usage : 

AuthenticateController.php

use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class AuthenticateController extends Controller
{
    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'));
    }
}


You can also skip user authentication and just pass in a User object. e.g.
// grab some user
$user = User::first();

$token = JWTAuth::fromUser($user);

The above two methods also have a second parameter where you can pass an array of custom claims. e.g.
$customClaims = ['foo' => 'bar', 'baz' => 'bob'];

JWTAuth::attempt($credentials, $customClaims);
// or
JWTAuth::fromUser($user, $customClaims);

create token based on anything 
$customClaims = ['foo' => 'bar', 'baz' => 'bob'];

$payload = JWTFactory::make($customClaims);

$token = JWTAuth::encode($payload);

d