我遇到了FOSUserBundle問題。RedirectionListener.php中的FatalErrorException第46行:錯誤:調用非對象的成員函數getUser()
我有一個重定向偵聽器,以防止用戶使用的應用程序沒有被登錄,下面的代碼:
class RedirectionListener
{
/**
* RedirectionListener constructor.
* @param ContainerInterface $container
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
*/
public function __construct(ContainerInterface $container)
{
$this->router = $container->get('router');
$this->securityTokenStorage = $container->get('security.token_storage');
}
/**
* @param GetResponseEvent $event
* @throws \InvalidArgumentException
*/
public function onKernelRequest(GetResponseEvent $event){
$route = $event->getRequest()->attributes->get('_route');
if($route !== 'fos_user_security_login' &&
$route !== 'fos_user_resetting_reset' &&
$route !== 'fos_user_resetting_request' &&
$route !== 'fos_user_resetting_send_email' &&
$route !== 'fos_user_resetting_check_email' &&
$route !== 'fos_user_change_password' &&
!is_object($this->securityTokenStorage->getToken()->getUser())) //this is line 46
{
$event->setResponse(new RedirectResponse($this->router->generate('fos_user_security_login')));
}
}
}
這是我的服務:
services:
redirectonListener:
class: Projects\ProjectsBundle\Listener\RedirectionListener
arguments: ['@service_container']
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
而且這裏離我的防火牆設置FOSUserBundle Docs:
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
# if you are using Symfony < 2.8, use the following config instead:
# csrf_provider: form.csrf_provider
logout: true
anonymous: true
我正在使用Symfony 2.8版本。誰能幫忙!
在構造函數中:var_dump($ container-> get('security.token_storage'));什麼說? – Deep
我可能在這裏錯了,但你不能像這樣檢查嗎? if($ this-> get('security.authorization_checker') - > isGranted('IS_AUTHENTICATED_FULLY')=== false) – Thomas
@Deep這裏是var_dump的結果:'object(Symfony \ Component \ Security \ Core \ Authentication \ Token \ Storage \ TokenStorage)#264(1){[「token」:「Symfony \ Component \ Security \ Core \ Authentication \ Token \ Storage \ TokenStorage」:private] => NULL} –