Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

php - Catchable Fatal Error: Argument 1 passed to ...CsrfTokenManager::isTokenValid() must be an instance of ...CsrfToken, string given

Currently, I am updating the system running on the existing Symfony 2.3 (currently 3.0.9), and I am verifying the operation. When I tried the ability to change the status of an article to the selected status, I got an error. Do you have any advice on how to determine the cause?

Code BaseArticleController.php

    /**
     * Article status change
     */
    protected function updateArticleStatusAction(Request $request, $ids)
    {
        // CSRF token check
        $token = $request->request->get('_csrf_token');

        if (!$this->get('security.csrf.token_manager')->isTokenValid('authenticate', $token))
 {
            throw new HttpException("400", "The CSRF token is invalid. Please try to resubmit
 the form.");
        }

        // Check status
        $articleStatus = $request->request->get("articleStatus");
        if (!in_array($articleStatus, Parameters::getArticleStatusKeys())) {
            throw new HttpException("400", "articleStatus is invalid.");
        }

        // Status change
        try {
            $ids = explode(',', $ids);
            $count = $this->getArticleService()->updateArticleStatus($ids, $articleStatus, $t
his->getShop());
            if ($count) {
                $this->get('session')->getFlashBag()->add('success', "{$count}The status of the article has changed.");
            }
        } catch (ArticleValidationException $e) {
            $article = $e->getArticle();
            $statusArray = Parameters::getArticleStatus();
            $this->get('session')->getFlashBag()->add(
                'error',
                sprintf(
                    "Article ID:% d could not be "% s". Please check your input.",
                    $article->getId(),
                    $statusArray[$article->getArticleStatus()]
                )
            );
        }

        // redirect
        $backurl = $request->query->get("backurl");
        if (!$backurl) {
            $backurl = $this->generateUrl($this->indexRoute);
        }
        return $this->redirect($backurl);
    }

ArticleController.php

    /**
     * Article status change
     *
     * @Method("POST")
     * @Route("/article/{ids}/articleStatus")
     * @Secure(roles="ROLE_HQ_MANAGE")
     */
    public function updateArticleStatusAction(Request $request, $ids)
    {
        return parent::updateArticleStatusAction($request, $ids);
    }

index.html.twig

    {# Status change form #}
    <form method="post" class="updateArticleStatus" data-url="{{ path("ahi_sp_admin_hq_article_updatearticlestatus", {"ids": "__ids__"}) }}">
        <input type="hidden" name="methods" value="POST">
        <input type="hidden" name="_csrf_token" value="{{ csrf_token("authenticate") }}">
        <input type="hidden" name="articleStatus" value="">
    </form>

security.yml

security:
    firewalls:
        secured_area2:
            pattern:    ^/admin/sp/
            anonymous: ~
            form_login:
                login_path:  /admin/sp/login
                check_path:  /admin/sp/login_check
                csrf_token_generator: security.csrf.token_manager
                always_use_default_target_path: true
                default_target_path:            /admin/sp/
                target_path_parameter:          _target_path
                use_referer:                    false

            logout:
                path:   /admin/sp/logout
                target: /admin/sp/login

            remember_me:
                secret:      "%secret%"
                lifetime: 2592000 # 30 days in seconds
                path:     /
                domain:   ~ # Defaults to the current domain from $_SERVER
                always_remember_me: true

        secured_area:
            pattern:    ^/admin/
            anonymous: ~
            form_login:
                login_path:  /admin/login
                check_path:  /admin/login_check

                csrf_token_generator: security.csrf.token_manager
                always_use_default_target_path: true
                default_target_path:            /admin/
                target_path_parameter:          _target_path
                use_referer:                    false

            logout:
                path:   /admin/logout
                target: /admin/login
question from:https://stackoverflow.com/questions/65547092/catchable-fatal-error-argument-1-passed-to-csrftokenmanageristokenvalid

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The message also give enough detail for you to debug, argument 1 (in your case is "authenticate") must be an crsf Token. Try this:

$csrf_token = new CsrfToken('authenticate', $token);

$this->get('security.csrf.token_manager')->isTokenValid($csrf_token)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...