【问题标题】:Jwt Token decode - Symfony 4Jwt 令牌解码 - Symfony 4
【发布时间】:2020-05-23 01:51:39
【问题描述】:

我正在尝试解决与令牌请求相关的问题。这是我在控制器中的 newArticle 功能(添加新文章):

public function newArticle(Request $request, EntityManagerInterface $entityManager): View
    {
        $data = json_decode($request->getContent(), true);
        $title = $data['title'];
        $content = $data['content'];
        //$published_at = $data['published_at'];
        $authorizationHeader = $request->headers->get('Authorization');
        list(,$token) = explode(' ', $authorizationHeader);
        $jwtToken = $this->JWTEncoder->decode($token);
        $user_id = $data[$jwtToken];
        $userId = $this->userRepository->findOneBy(['id' => $user_id['id']]);
        $article = new Article();
        $article->setTitle($title);
        $article->setContent($content);
        $article->setPublishedAt(new \DateTime());
        $article->setUser($userId);
        // Todo: 400 response - Invalid input
        // Todo: 404 response - Response not found
        // Incase our Post was a success we need to return a 201 HTTP CREATED response with the created object
        if(in_array('ROLE_USER', $article->getUser()->getRoles(), true)) {
            $entityManager->persist($article);
            $entityManager->flush();
            return View::create("You added an article successfully!", Response::HTTP_OK);
        } else {
            return View::create(["You are not a user! So please register to add an article!"], Response::HTTP_BAD_REQUEST);
        }
    } 

在添加令牌头授权之前它正在工作,现在我收到了这个错误:

"error": {
   "code": 500,
  "message": "Internal Server Error",
 "message": "Notice: Undefined offset: 1", 

谁能给我一些建议?

【问题讨论】:

    标签: php symfony4 jwt-auth


    【解决方案1】:

    我觉得你的问题是这一行:

    $user_id = $data[$jwtToken];
    

    你应该重构它,认为它应该是这样的:

    $user_id = $data['user_id'];
    

    $user_id = $jwtToken['user_id'];
    

    根据每个对象/数组包含的数据,您应该寻找的位置。首先,错误是调用数组上的偏移量,因此修复它应该没问题(或者从日志中获得更清晰的错误消息)

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 2018-10-05
      • 2019-05-19
      • 2019-05-18
      • 2018-08-14
      • 2016-11-15
      • 2021-06-30
      • 2019-10-11
      相关资源
      最近更新 更多