- 我有一个用户可以登录的表单。
- 登录后,我会将用户重定向到仪表板页面。
问题:在我的登录 Controller 中,我得到了用户对象。但是当我转到仪表板时,用户对象是空的。
登录 Controller
if ($this->isAuthUser($request)) {
$signinUser = $request->get('signin');
$token = new UsernamePasswordToken($signinUser, null, 'secured_area', $signinUser->getRoles());
$this->get("security.context")->setToken($token);
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
$session = $this->get('session');
$session->set('user', $signinUser->getUsername());
}
安全.yml
security:
providers:
in_memory:
memory: ~
access_control:
#- { path: ^/(signin|signup)?$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
#- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
firewalls:
secured_area:
pattern: ^/
anonymous: true
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
token 对象:Signincontroller
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] => Headset\Bundle\AccountsBundle\Entity\User Object
(
[email:Headset\Bundle\AccountsBundle\Entity\User:private] => operator1@flavr.com
[password:Headset\Bundle\AccountsBundle\Entity\User:private] => operator123#46
[oldPassword:Headset\Bundle\AccountsBundle\Entity\User:private] =>
[id:Headset\Bundle\AccountsBundle\Entity\User:private] =>
[userId:Headset\Bundle\AccountsBundle\Entity\User:private] =>
[firstName:Headset\Bundle\AccountsBundle\Entity\User:private] =>
[lastName:Headset\Bundle\AccountsBundle\Entity\User:private] =>
[username:Headset\Bundle\AccountsBundle\Entity\User:private] => operator1@flavr.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
(
)
)
仪表板 Controller
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: ~
查看关于 users loading 的 symfony 文档
关于交响乐 2.5 : $this->getUser() is empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26354920/