【问题标题】:Lumen JWT send token with requestsLumen JWT 通过请求发送令牌
【发布时间】:2017-10-21 15:43:27
【问题描述】:

身份验证正在运行,我在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,
    ]);
}

【问题讨论】:

  • 你是如何调用 api 的?您必须由调用者在标题或查询字符串中进行设置。尝试将?token={tokenvalue} 添加到您的 api 链接并进行测试。
  • @sandeesh 是的,我正在使用postman,我手动将令牌放入标题中。
  • 那你的问题到底是什么?如果它在邮递员中有效,那么在以其他方式调用它时也应该这样做。除了邮递员之外,您打算如何调用 api?请在您的问题中更具体。
  • @sandeesh 用户无法给出token i地址,有一种方法可以让api自动获取认证用户的token!
  • 这不是 api 的工作方式。您需要随每个请求发送令牌。你还是没有回答我的问题。除了邮递员,你打算如何使用 api?通过ajax前端?移动应用?第三方api?提问时请具体。通常,当用户执行登录或注册时会生成一个令牌,并且此令牌会在 api 响应中发回。 api 的调用者需要在本地存储此令牌,具体取决于调用它的平台。然后在每个请求中使用这个本地存储的令牌。

标签: laravel api authorization jwt lumen


【解决方案1】:

这个问题很难回答。请从下一次更具体。从您的 cmets 中,我终于意识到您想从移动应用程序中使用 api。

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

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

【讨论】:

    【解决方案2】:

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

    要求: Laravel 4 或 5(参见兼容性表) PHP 5.4 + 脚步: 1:在 composer.json 的 require 数组中添加以下行 “tymon/jwt-auth”:“0.5.*” 2:在终端中运行“composer update” 3:在此之后您必须注册服务提供商 转到配置/app.php 并在提供程序数组中添加 'Tymon\JWTAuth\Providers\JWTAuthServiceProvider' 和 'JWTAuth' => 'Tymon\JWTAuth\Facades\JWTAuth' , 'JWTFactory' => 'Tymon\JWTAuth\Facades\JWTFactory' 这个别名数组 4:发布包: "php 工匠供应商:publis --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider" 5:在配置文件中生成secrate key 'php 工匠 jwt: 生成' 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

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 2018-04-09
      • 2020-08-14
      相关资源
      最近更新 更多