无法从 laravel 5 应用程序中的请求解析 jwt 令牌
jwt token could not be parsed from the request in laravel 5 application
我正在为我的应用开发基于令牌的授权。我正在关注 -
https://www.codetutorial.io/laravel-and-angularjs-token-based-auth-part1/
我设置了不同的前端文件夹,laravel 作为后端。
用户能够注册并且凭据确实保存在数据库中但是当我需要创建一个 jwt 令牌时,我收到一个错误:-
Tymon\JWTAuth\Exceptions\JWTException' with message '
The token could not be parsed from the request
RegisterController.php
class RegisterController extends Controller
{
public function register(Request $request)
{
$newuser= $request->all();
$password=Hash::make($request->input('password'));
$newuser['password'] = $password;
return Register::create($newuser);
}
}
LoginController.php
class LoginController extends Controller
{
public function authenticate(Request $request){
$credentials = $request->only('email', 'password');
try{
if(! $token = JWTAuth::attempt($credentials)){
return response()->json(['error' => 'invalid_credentials'], 401);
}
}
catch(JWTException $e){
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json(compact('token'));
}
public function getAuthenticatedUser(){
try{
if(! $user = JWTAuth::parseToken()->authenticate()){
return response()->json(['user_not_found'], 404);
}
}
catch(Tymon\JWTAuth\Exceptions\TokenExpiredException $e){
return response()->json(['token_expired'], $e->getStatusCode());
}
catch(Tymon\JWTAuth\Exceptions\TokenInvalidException $e){
return response()->json(['token_invalid'], $e->getStatusCode());
}
catch(Tymon\JWTAuth\Exceptions\JWTException $e){
return response()->json(['token_absent'], $e->getStatusCode());
}
return response()->json(compact('user'));
}
}
routes.php
Route::post('/api/register', 'RegisterController@register');
Route::post('api/authenticate', 'LoginController@authenticate');
Route::get('api/authenticate/user', 'LoginController@getAuthenticatedUser');
我没有发现任何问题,它应该可以正常工作,但不明白问题出在哪里。
我终于通过将 .htaccess 文件更新为
解决了这个问题
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/ [L]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</IfModule>
我正在为我的应用开发基于令牌的授权。我正在关注 -
https://www.codetutorial.io/laravel-and-angularjs-token-based-auth-part1/
我设置了不同的前端文件夹,laravel 作为后端。
用户能够注册并且凭据确实保存在数据库中但是当我需要创建一个 jwt 令牌时,我收到一个错误:-
Tymon\JWTAuth\Exceptions\JWTException' with message '
The token could not be parsed from the request
RegisterController.php
class RegisterController extends Controller
{
public function register(Request $request)
{
$newuser= $request->all();
$password=Hash::make($request->input('password'));
$newuser['password'] = $password;
return Register::create($newuser);
}
}
LoginController.php
class LoginController extends Controller
{
public function authenticate(Request $request){
$credentials = $request->only('email', 'password');
try{
if(! $token = JWTAuth::attempt($credentials)){
return response()->json(['error' => 'invalid_credentials'], 401);
}
}
catch(JWTException $e){
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json(compact('token'));
}
public function getAuthenticatedUser(){
try{
if(! $user = JWTAuth::parseToken()->authenticate()){
return response()->json(['user_not_found'], 404);
}
}
catch(Tymon\JWTAuth\Exceptions\TokenExpiredException $e){
return response()->json(['token_expired'], $e->getStatusCode());
}
catch(Tymon\JWTAuth\Exceptions\TokenInvalidException $e){
return response()->json(['token_invalid'], $e->getStatusCode());
}
catch(Tymon\JWTAuth\Exceptions\JWTException $e){
return response()->json(['token_absent'], $e->getStatusCode());
}
return response()->json(compact('user'));
}
}
routes.php
Route::post('/api/register', 'RegisterController@register');
Route::post('api/authenticate', 'LoginController@authenticate');
Route::get('api/authenticate/user', 'LoginController@getAuthenticatedUser');
我没有发现任何问题,它应该可以正常工作,但不明白问题出在哪里。
我终于通过将 .htaccess 文件更新为
解决了这个问题<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/ [L]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</IfModule>