SigninController
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.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: ~
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
答案 0 :(得分:0)
UsernamePasswordToken对象的第一个和第二个参数需要包含用户对象和用户密码。