我正在使用Lumen创建一个简单的API,我使用JWTAuth库来制作令牌。
我的登录信息如下:
try
{
$this->validate($request, [
'email' => 'required|email|max:255',
'password' => 'required'
]);
try
{
if (!$this->token = $this->jwt->attempt($request->only('email', 'password'))):
throw new HttpResponseException(response()->json(['message' => 'Email/Contraseña invalidos'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
endif;
}
catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e)
{
throw new HttpResponseException(response()->json(['message' => 'Token expirado'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
}
catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e)
{
throw new HttpResponseException(response()->json(['message' => 'Token invalido'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
}
catch (\Tymon\JWTAuth\Exceptions\JWTException $e)
{
throw new HttpResponseException(response()->json(['message' => 'Sin token'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
}
return response()->json([
"token" => $this->token,
"email" => $this->jwt->user()->email,
"rfc" => $this->jwt->user()->rfc,
"nombre" => $this->jwt->user()->nombre,
"_uof" => $this->Cipher->EncryptCipher($this->jwt->user()->id),
"_rof" => $this->Cipher->EncryptCipher($this->jwt->user()->rfc)
], 200);
}
catch(\Exception $error)
{
throw new HttpResponseException(response()->json(['message' => 'Error al iniciar sesión el usuario'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
}
当我尝试在我的本地环境中使用curl或postman验证令牌时,返回我在React前端应用程序上设置的令牌:
try
{
$this->payload = $this->jwt->getPayload($this->jwt->getToken())->toArray();
var_dump($this->payload);
}
catch(\Exception $error)
{
var_dump($error->getMessage());
}
我的请求如下:
curl -X POST \
http://localhost:8000/debtor/validateLogin/ \
-H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9hcGltcGFsYS5yZWFsaG9zdC5jbG91ZFwvYXV0aFwvbG9naW4iLCJpYXQiOjE1MjcyODcyMTEsImV4cCI6MTUyNzI5MDgxMSwibmJmIjoxNTI3Mjg3MjExLCJqdGkiOiJyc3duZFh0NndzMXdhRGRvIiwic3ViIjoxLCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIiwibmFtZSI6IlVsaXNlcyBDYXJyZW9uIiwicmZjIjoiWEFYWDAxMDEwMTAwMCIsImF2YXRhciI6bnVsbH0.Ey1lMFYtSAE5f8pSrXjxnrMLGv2yOhl3DvwJo1qiTCI' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: 29819986-830d-48f8-8724-25ce2fbf0af2' \
-d '{}'
令牌位于请求的标头上但是当我发出请求时会抛出此错误"需要令牌"。
我已将.htaccess更改为:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
#RewriteCond %{HTTP:Authorization} .
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Fix authentication headers
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
但仍然没有工作,有什么建议吗?
此致