我正在使用Symfony通过restful api开发一个post登录方法,这是我的代码:
/**
* @Rest\View()
* @Rest\Post("/login")
*/
public function loginAction(Request $request)
{
$username = $request->request->get('username');
$password = $request->request->get('password');
if(is_null($username) || is_null($password)) {
return new Response(
'Please verify all your inputs.',
Response::HTTP_NOT_FOUND,
array('Content-type' => 'application/json')
);
}
$user_manager = $this->get('fos_user.user_manager');
$factory = $this->get('security.encoder_factory');
$user = $user_manager->findUserByUsername($username);
if (!$user) {
throw $this->createNotFoundException();
}
$encoder = $factory->getEncoder($user);
$salt = $user->getSalt();
if($encoder->isPasswordValid($user->getPassword(), $password, $salt)) {
$token = $this->get('lexik_jwt_authentication.encoder')
->encode([
'username' => $user->getUsername(),
'exp' => time() + 3600 // 1 hour expiration
]);
} else {
$response = new Response(
'Username or Password not valid.',
Response::HTTP_NOT_FOUND,
array('Content-type' => 'application/json')
);
}
return new JsonResponse(['token' => $token]);
}
我正在尝试使用jwt身份验证包检索令牌,以将其存储在angularjs前端的localstorage中,并将其与用户发出的每个请求一起发送。 问题是,当我使用邮递员测试其余方法时,它会产生500错误,仅此而已。有人可以帮我弄清楚错误可能在哪些地方,特别是错误呈现的是非常简短和不清楚。我花了最近4个小时搜索但没有找到解决方案。