Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony JWT令牌:令牌过期时异常_Symfony_Symfony 3.1_Lexikjwtauthbundle - Fatal编程技术网

Symfony JWT令牌:令牌过期时异常

Symfony JWT令牌:令牌过期时异常,symfony,symfony-3.1,lexikjwtauthbundle,Symfony,Symfony 3.1,Lexikjwtauthbundle,我正在使用JWT令牌包进行用户身份验证。当令牌过期时,我得到500服务器错误。与此相反,如何返回带有错误代码和消息的JsonResponse 这是我的验证器类: class JwtTokenAuthentication extends AbstractGuardAuthenticator { /** * @var JWTEncoderInterface */ private $jwtEncoder; /** * @var EntityManager */ private $em;

我正在使用JWT令牌包进行用户身份验证。当令牌过期时,我得到500服务器错误。与此相反,如何返回带有错误代码和消息的JsonResponse

这是我的验证器类:

 class JwtTokenAuthentication extends AbstractGuardAuthenticator
{
/**
 * @var JWTEncoderInterface
 */
private $jwtEncoder;

/**
 * @var EntityManager
 */
private $em;

public function __construct(JWTEncoderInterface $jwtEncoder, EntityManager $em)
{
    $this->jwtEncoder = $jwtEncoder;
    $this->em = $em;
}


public function getCredentials(Request $request)
{
    $extractor = new AuthorizationHeaderTokenExtractor(
        'Bearer',
        'Authorization'
    );
    $token = $extractor->extract($request);
    if (!$token) {
        return null;
    }

    return $token;
}

public function getUser($credentials, UserProviderInterface $userProvider)
{
    $data = $this->jwtEncoder->decode($credentials);
    if(!$data){
      return null;
    }
    $user = $this->em->getRepository("AlumnetCoreBundle:User")->find($data["email"]);
    return $user;
}

public function checkCredentials($credentials, UserInterface $user)
{
    return true;
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
    //todo
}

public function start(Request $request, AuthenticationException $authException = null)
{
    return new JsonResponse([
        'errorMessage' => 'auth required'
    ], Response::HTTP_UNAUTHORIZED);
}
}

您可以在try-catch中解码令牌:

try {
    $data = $this->jwtEncoder->decode($credentials);
} catch (\Exception $e) {
    throw new \Symfony\Component\Security\Core\Exception\BadCredentialsException($e->getMessage(), 0, $e);
}
但是您可能必须实现缺少的
onAuthenticationFailure
,因为抛出此异常将使其被调用。比如:

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
    return new JsonResponse([
        'errorMessage' => $exception->getMessage(),
    ], Response::HTTP_UNAUTHORIZED);
}

顺便说一句,LexikJWTAuthenticationBundle自其2.0版本以来就附带了内置的
JWTTokenAuthenticator
。我建议您在实现自己的验证器之前尝试使用它,或者至少。

我将我的整个代码放在一个try-catch块中,当生成JWT令牌过期错误消息时,它会被捕获在
catch
块中

{ "错误":一,, “状态”:400, “msg”:“过期的JWT令牌”, “数据”:[] }

/**
 * @Route("/api/tokens")
 * @Method("POST")
 */
public function newTokenAction(Request $request)
{
    try {
    $data['_username'] = $request->get('_username');
    $data['_password'] = $request->get('_password');
    if (empty($data['_username']) || empty($data['_password'])) {
        throw new \Exception('Username or password fields empty');
    }

    $user = $this->getDoctrine()->getRepository('AppBundle:User')->findOneBy(array('username' => $data['_username']));
    if (!$user) {
        throw new \Exception('Username or password does not exist');
    } else if ($user->hasRole('ROLE_SUPER_ADMIN')) {
        throw new \Exception('Admin is not allowed to login through app');
    } else if (!$user->getEnabled()) {
        throw new \Exception('User is not enabled');
    } else if ($user->getIsDeleted()) {
        throw new \Exception('User does not exist any more');
    }

    $isValid = $this->get('security.password_encoder')->isPasswordValid($user, $data['_password']);
    if (!$isValid) {
        throw new \Exception('Bad Credentials');
    }

    $token = $this->get('lexik_jwt_authentication.encoder')->encode(array(
        'username' => $data['_username'],
        'exp' => time() + 3600, 
        'secret_key' => ____________,
    ));

    $user->setAuthToken($token);
    $em = $this->getEntityManager();
    $em->persist($user);
    $em->flush();

    $json = $this->getJsonResponse(0, 200, 'User Logged In');

    $response = new Response($json);
    $response->headers->set('Content-Type', 'application/json');
    return $response;
} catch (\Exception $e) {
    // Using custom Execption class
    $customApiProblem = new CustomApiProblem(self::API_ERROR_TRUE, $httpStatusCode, $e->getMessage());
    $customApiProblem->set('data', $data);
    $serializer = $this->container->get('jms_serializer');
    $response_json = $serializer->serialize($customApiProblem->toArray(), 'json');
    return new Response($response_json, $statusCode);
}
}