Lumen JWT 发送带有请求的令牌
Lumen JWT send token with requests
身份验证有效,我在 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。
您需要 return 在登录或注册期间或您拥有的任何其他身份验证期间为用户生成的令牌 method/route。移动应用程序需要读取此响应并将令牌存储在本地。然后应用程序需要在每个请求的请求 header 中注入此令牌。这是正常的 api 令牌工作流程。
应用程序还应该被编码以读取来自请求的错误响应,如果它 returns 过期或无效令牌的错误,应用程序需要清除本地存储的令牌,然后请求用户登录再次生成一个新的令牌。
您可以使用:https://github.com/tymondesigns/jwt-auth
要求:
Laravel 4 或 5(参见兼容性 table)
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:发布包:
"php artisan vendor:publis --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
5:在配置文件中生成secrate key
'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
身份验证有效,我在 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。
您需要 return 在登录或注册期间或您拥有的任何其他身份验证期间为用户生成的令牌 method/route。移动应用程序需要读取此响应并将令牌存储在本地。然后应用程序需要在每个请求的请求 header 中注入此令牌。这是正常的 api 令牌工作流程。
应用程序还应该被编码以读取来自请求的错误响应,如果它 returns 过期或无效令牌的错误,应用程序需要清除本地存储的令牌,然后请求用户登录再次生成一个新的令牌。
您可以使用:https://github.com/tymondesigns/jwt-auth
要求: Laravel 4 或 5(参见兼容性 table) 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:发布包: "php artisan vendor:publis --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider" 5:在配置文件中生成secrate key '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