令牌存储不包含身份验证令牌

时间:2017-03-14 11:38:58

标签: symfony

我是安全的堆栈,请帮帮我!  我在2天前收到此错误并无法解决:“令牌存储不包含身份验证令牌。一个可能的原因可能是没有为此URL配置防火墙。”我在索引页面登录,登录和login_check属于同一个函数(indexAction)。这是我的security.yml文件,我的控制器和我的实体“user”:

                # you can read more about security in the related section of the documentation
                # http://symfony.com/doc/current/book/security.html
                security:
                    # http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password


                  encoders:
                        #Symfony\Component\Security\Core\User\User: plaintext
                        CNAM\CMSBundle\Entity\user: bcrypt

                    # http://symfony.com/doc/current/book/security.html#hierarchical-roles
                    role_hierarchy:
                        ROLE_ADMIN:       ROLE_USER
                        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

                    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
                    providers:
                        in_memory:
                            memory:
                                users:
                                    user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                                    admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
                        database:
                            entity:
                                class: CNAM\CMSBundle\Entity\user
                                property: username
                    # the main part of the security, where you can set up firewalls
                    # for specific sections of your app
                    firewalls:
                        # disables authentication for assets and the profiler, adapt it according to your needs
                        dev:
                            pattern:  ^/(_(profiler|wdt)|css|images|js)/
                            security: false


 admin_area:
        pattern:    ^/admin
        form_login:
           check_path: _default_index
           login_path: _default_index

                    access_control:
                        #- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY}
                        - { path: ^/admin, roles: ROLE_ADMIN}
        <?php

        namespace CNAM\CMSBundle\Controller;

        use Symfony\Bundle\FrameworkBundle\Controller\Controller;
        use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
        use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
        use Symfony\Component\HttpFoundation\Request;
        use CNAM\CMSBundle\Entity\user;
        use CNAM\CMSBundle\Entity\userprof;
        use CNAM\CMSBundle\Entity\profil;
        use CNAM\CMSBundle\Entity\privilege;
        use Symfony\Component\BrowserKit\Response;
        use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
        use Symfony\Component\Form\AbstractType;
        use Symfony\Component\Form\FormBuilder;
        use Symfony\Component\Security\Core\Security;


        class DefaultController extends Controller
        {
            /**
             * @Route("/")
             * @Template()
             */
            public function indexAction(Request $request)
            {
                $user = new user();
                $form = $this->createFormBuilder($user)
                    ->add('id', 'text',array('attr'=>array('name'=>'login_user','required'=>'required',
                        'maxlength'=>'255','placeholder'=>'Votre matricule','id'=>'login_user')))
                    ->add('password', 'password',array('attr'=>array('name'=>'login_password','required'=>'required',
                        'maxlength'=>'20','placeholder'=>'Mot de passe','id'=>'login_password')))
                    ->add('Connexion', 'submit',array('attr'=>array('class'=>'btn btn-primary btn-block rounded_btn','id'=>'login_btn',
                        'style'=>"width:8vw;height:5vh;padding:0px 0px; position:relative;left:5vmin;top:1vmin;font-size:2vmin;")))
                    ->getForm();
                $form->handleRequest($request);
                //$b_search=$this->get('session')->get('search');
                $id = $request->request->get('id');
                $session = $request->getSession();

                if ($form->isValid()) {
                    $data = $form->getData();
                    $repository = $this
                        ->getDoctrine()
                        ->getManager()
                        ->getRepository("CNAMCMSBundle:user");
                    $rep = $this
                        ->getDoctrine()
                        ->getManager()
                        ->getRepository("CNAMCMSBundle:userprof");
                    $search = $repository->find($data);
                    $p_search=$rep->find($data);
                    $helper = $this->get('security.authentication_utils');
                   if (!$search) {
                       //throw $this->createNotFoundException('Utilisateur introuvable!');

                     }
                   else {
                        //$session=$this->get("session");
                        //$session->start();
                        // $session->set('search', $search);
                        $user->setEtat(1);
                        $em = $this->getDoctrine()->getManager();
                        $user=$em->merge($user);
                        $em->flush();

                        $id_prof=$p_search->getIdProfil();

                        switch ($id_prof)
                        {
                            case 1: return $this->redirect($this->generateUrl('cnam_cms_default_webmaster'),301);break;
                            case 2: $user->setRole("ROLE_ADMIN");$em = $this->getDoctrine()->getManager();$user=$em->merge($user);
                                $em->flush();return $this->redirect($this->generateUrl('cnam_cms_default_admin'),301);break;
                            case 3: return $this->redirect($this->generateUrl('cnam_cms_default_sup_med'),301);break;
                            case 4: return $this->redirect($this->generateUrl('cnam_cms_default_med'),301);break;
                            case 5: return $this->redirect($this->generateUrl('cnam_cms_default_gest_mp'),301);break;
                        }
                    }

                    //return $this->render('CNAMCMSBundle:Default:profile.html.twig', array(
                    //'search' => $search,
                    //'b_search'=>$b_search
                    // ));
                }
                return array('form'=>$form->createView());
               }
                /**
                 * @Route("/admin")
                 * @Template()
                 */
                public function adminAction()
               {
                   return $this->render('CNAMCMSBundle:Default:admin.html.twig', array());
               }

                /**
                 * @Route("/admin/gestEtat",name="gestEtat")
                 * @Template()
                 */
                public function gestEtatAction()
               {
                   return $this->render('CNAMCMSBundle:Default:gestEtat.html.twig', array());
               }

        }

    <?php

    namespace CNAM\CMSBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\Security\Core\User\UserInterface;

    /**
     * user
     *
     * @ORM\Table(name="user")
     * @ORM\Entity
     */
    class user implements UserInterface
    {
        /**
         * @var integer
         *@Assert\NotBlank()
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         */
        private $id;

        /**
         * @var string
         *@Assert\NotBlank()
         * @ORM\Column(name="password", type="string", length=40)
         */
        private $password;

        /*
         * @ORM\ManyToOne(targetEntity="profil" , inversedBy="users")
         * @ORM\JoinColumn(name="id_profil", referencedColumnName="id_profil")
         */
        private $profil;

        public function __construct()
        {
            $this->profil = new ArrayCollection();
        }

        /**
         * @var boolean
         *
         * @ORM\Column(name="etat", type="boolean")
         */
        private $etat;


        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
        /**
         * Set id
         *
         * @param integer $id
         * @return user
         */
        public function setId($id)
        {
            $this->id = $id;

            return $this;
        }

        /**
         * Set password
         *
         * @param string $password
         * @return user
         */
        public function setPassword($password)
        {
            $this->password = $password;

            return $this;
        }

        /**
         * Get password
         *
         * @return string 
         */
        public function getPassword()
        {
            return $this->password;
        }

        /**
         * Set etat
         *
         * @param boolean $etat
         * @return user
         */
        public function setEtat($etat)
        {
            $this->etat = $etat;

            return $this;
        }

        /**
         * Get etat
         *
         * @return boolean 
         */
        public function getEtat()
        {
            return $this->etat;
        }

        /**
         * Get profil
         *
         * @return \Doctrine\Common\Collections\Collection
         */
        public function getProfil()
        {
            return $this->profil;
        }


        /**
         * Add profil
         *
         * @param \CNAM\CMSBundle\Entity\user $profil
         * @return user
         */
        public function addProfil(\CNAM\CMSBundle\Entity\profil $profil)
        {
            $this->profil[] = $profil;

            return $this;
        }

        /**
         * Remove profil
         *
         * @param \CNAM\CMSBundle\Entity\profil $profil
         */
        public function removeProfil(\CNAM\CMSBundle\Entity\profil $profil)
        {
            $this->profil->removeElement($profil);
        }
        public function getUsername()
        {
            return $this->id;
        }
        public function getRoles()
        {
            return array('ROLE_USER');
        }

        public function getSalt()
        {
            return null;
        }

        public function eraseCredentials()
        {

        }

        public function equals(UserInterface $user)
        {
            return $user->getId() == $this->getId();
        }
    }

0 个答案:

没有答案