如何在 Laravel 5.8 中附加授权 Header
How to append Authorization Header in Laravel 5.8
我想在 Laravel 中添加 Authorization: Bearer {yourtokenhere}
。如果在
邮递员我将授权放在 Headers 选项卡上,并通过编写 Bearer {token}
手动赋予令牌值,这样我就可以保护特定路由,但是如何在 Laravel 源代码中执行此操作?我应该在我的控制器中做什么,或者我应该在中间件或内核或其他地方添加另一个方法?
这是因为我得到 {"error":"token_not_provided"}
每次访问受 jwt.auth middleware
.
保护的路由
这是我的受保护路线 {"error":"token_not_provided"}
:
Route::group(['middleware' => ['jwt.auth']], function(){
Route::get('/dashboard', 'AppController@dashboard');
});
这是我在 AuthController 中的登录方法:
public function signin(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required'
]);
// grab credentials from the request
$credentials = $request->only('username', '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, username and password dismatches. Or username may not registered.',
'status' => '401'
], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json([
'user_id' => $request->user()->id,
'token' => $token
]);
}
你可以这样设置
$request = Request::create(route('abc', 'GET');
$request->headers->set('X-Authorization', 'xxxxx');
有关更多信息,您可以关注此 Whosebug 答案
如果你使用 jwt,你甚至可以在 url 中传递你的令牌
喜欢 www.example.com/post?token=kjdhfkjsffghrueih
就像你说的,你在 AuthAcontroller
中需要这个然后你必须在客户端设置它。
首先将该令牌存储在 localstorage 中,并将该令牌设置为来自 localstorage 的 http 调用(我猜是通过 ajax 或 axios),然后将请求发送到 laravel.
要在 ajax 调用中访问,您可以使用以下代码
headerParams = {'Authorization':'bearer t-7614f875-8423-4f20-a674-d7cf3096290e'}; //token form localstorage
然后像
一样在ajax中使用它
type: 'get',
url: 'https://api.sandbox.slcedu.org/api/rest/v1/students/test1',
headers: headerParams,
或者如果您使用的是 axios,您可以通过
进行设置
axios.defaults.headers.common['Authorization'] = 'Bearer ' + localStorage.getItem('SecurityKey');
我想在 Laravel 中添加 Authorization: Bearer {yourtokenhere}
。如果在
邮递员我将授权放在 Headers 选项卡上,并通过编写 Bearer {token}
手动赋予令牌值,这样我就可以保护特定路由,但是如何在 Laravel 源代码中执行此操作?我应该在我的控制器中做什么,或者我应该在中间件或内核或其他地方添加另一个方法?
这是因为我得到 {"error":"token_not_provided"}
每次访问受 jwt.auth middleware
.
这是我的受保护路线 {"error":"token_not_provided"}
:
Route::group(['middleware' => ['jwt.auth']], function(){
Route::get('/dashboard', 'AppController@dashboard');
});
这是我在 AuthController 中的登录方法:
public function signin(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required'
]);
// grab credentials from the request
$credentials = $request->only('username', '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, username and password dismatches. Or username may not registered.',
'status' => '401'
], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json([
'user_id' => $request->user()->id,
'token' => $token
]);
}
你可以这样设置
$request = Request::create(route('abc', 'GET');
$request->headers->set('X-Authorization', 'xxxxx');
有关更多信息,您可以关注此 Whosebug 答案
如果你使用 jwt,你甚至可以在 url 中传递你的令牌
喜欢 www.example.com/post?token=kjdhfkjsffghrueih
就像你说的,你在 AuthAcontroller
中需要这个然后你必须在客户端设置它。
首先将该令牌存储在 localstorage 中,并将该令牌设置为来自 localstorage 的 http 调用(我猜是通过 ajax 或 axios),然后将请求发送到 laravel.
要在 ajax 调用中访问,您可以使用以下代码
headerParams = {'Authorization':'bearer t-7614f875-8423-4f20-a674-d7cf3096290e'}; //token form localstorage
然后像
一样在ajax中使用它type: 'get',
url: 'https://api.sandbox.slcedu.org/api/rest/v1/students/test1',
headers: headerParams,
或者如果您使用的是 axios,您可以通过
进行设置axios.defaults.headers.common['Authorization'] = 'Bearer ' + localStorage.getItem('SecurityKey');