繁体   English   中英

Symfony2 $ this-> getUser()在其他控制器上为空

[英]Symfony2 $this->getUser() is empty on other controllers

登录控制器

if ($request->getMethod() == 'POST') {

      if ($form->isValid()) {
        /*
         * TO DO: should trigger the api endpoint. Redirection should be handled base 
         * on the api response.
         */
        $request  = $this->get("request");

        if ($this->isAuthUser($request)) {

          $token = new UsernamePasswordToken($signIn, null, 'secured_area', $signIn->getRoles());
          $this->get("security.context")->setToken($token);      
          $request  = $this->get("request");
          $event    = new InteractiveLoginEvent($request, $token);
          $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

          //$user =  $this->get('security.context')->getToken();


          return $this->redirect($this->generateUrl('main_accounts_myaccount'));

        } else {

          $error = new FormError(ErrorMessages::USER_NOT_AUTHENTICATED);
          $form->addError($error);
        }
      } 

    }

    return $this->render('MainAccountsBundle:Signin:index.html.twig', 
      array('form'=>$form->createView()));
    }

MyAccountController

public function indexAction() {
    $user =  $this->get('security.context')->getToken();

    return $this->render('MainAccountsBundle:MyAccount:index.html.twig');
}

当我使用登录表单时,它可以正常工作,并将我重定向到MyAccounts页面。 我检查的时候

{% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
  {{ app.user.username }}
   <a href="{{ path('main_accounts_signout') }}">Signout</a>
{% endif %}

在MyAccount的树枝上也可以使用。

问题:但是它不打印用户名。 当我从MyAccountController使用print_r($ user)时,它是空的。 当我打印print_r($ token)时,显示用户为空。

SignInController中的$ user

    Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken Object
(
    [credentials:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] => 
    [providerKey:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] => secured_area
    [user:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Main\Bundle\AccountsBundle\Entity\User Object
        (
            [email:Main\Bundle\AccountsBundle\Entity\User:private] => abc@xyz.com
            [password:Main\Bundle\AccountsBundle\Entity\User:private] => operator123#46
            [id:Main\Bundle\AccountsBundle\Entity\User:private] => 1212121212
            [userId:Main\Bundle\AccountsBundle\Entity\User:private] => 
            [name:Main\Bundle\AccountsBundle\Entity\User:private] => 
            [username:Main\Bundle\AccountsBundle\Entity\User:private] => abc@xyz.com
        )

    [roles:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
        (
            [0] => Symfony\Component\Security\Core\Role\Role Object
                (
                    [role:Symfony\Component\Security\Core\Role\Role:private] => ROLE_USER
                )

        )

    [authenticated:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => 1
    [attributes:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
        (
        )

   ) 

来自MyAccountController的$ user

    Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken Object
(
    [credentials:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] => 
    [providerKey:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] => secured_area
    [user:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => 
    [roles:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
        (
            [0] => Symfony\Component\Security\Core\Role\Role Object
                (
                    [role:Symfony\Component\Security\Core\Role\Role:private] => ROLE_USER
                )

        )

    [authenticated:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => 1
    [attributes:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
        (
        )

    )

尝试了很多以找出答案。 无法找到。 请帮我。

安全性

security:
    providers:
        in_memory:
            memory: ~

    access_control: 
      - { path: ^/(signin|signup)?$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
      #- { path: ^/, roles: IS_AUTHENTICATED_FULLY }      

    firewalls:
      secured_area:
        pattern:   ^/
        anonymous: ~
        form_login:
          login_path: /signin
          check_path: _security_check
        access_denied_url: signin
#        logout:
#          path:   /signout
#          #target: /
#          invalidate_session: false
#          delete_cookies:
#              a: { path: null, domain: null }
#              b: { path: null, domain: null }
#          #handlers: [some.service.id, another.service.id]
#          #success_handler: some.service.id
      dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

      default:
        anonymous: true

UsernamePasswordToken对象的第一个和第二个参数需要包含用户对象和用户密码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM