如果登录用户进入登录操作,我想将其重定向到另一个页面。但我无法弄清楚如何在loginAction
方法内部检测用户是否已登录。登录操作中的安全上下文使我似乎在我不在时退出。
作为测试,我在登录网站时请求以下两个页面。为什么我无法在登录操作中访问用户?
这是我的登录操作:
public function loginAction()
{
$token = $this->get('security.context')->getToken();
print_r(get_class($token));
// Outputs "Symfony\Component\Security\Core\Authentication\Token\AnonymousToken"
print_r($token->getUser());
// Outputs "anon."
}
以下是应用程序中的一般操作,受login:
保护public function regularAction()
{
$token = $this->get('security.context')->getToken();
print_r(get_class($token));
// Outputs "Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken"
print_r(get_class($token->getUser()));
// Outputs "Company\BaseBundle\Entity\User"
}
这是我的security.yml
:
security:
encoders:
Company\BaseBundle\Entity\User:
algorithm: sha1
iterations: 1
encode_as_base64: false
providers:
main:
entity: { class: Company\BaseBundle\Entity\User, property: user_name }
firewalls:
login_firewall:
pattern: ^/login$
anonymous: ~
main:
pattern: ^/
form_login:
login_path: /login
check_path: /login_check
post_only: true
always_use_default_target_path: false
default_target_path: /
use_referer: true
failure_path: null
failure_forward: false
username_parameter: user_name
password_parameter: password_hash
csrf_parameter: _csrf_token
intention: authenticate
logout:
path: /logout
target: /
acl:
connection: default
编辑:我不认为我的其他防火墙是相关的,但在阅读了ilanco的回答后,我认为它们可能
security:
encoders:
Company\BaseBundle\Entity\User:
algorithm: sha1
iterations: 1
encode_as_base64: false
providers:
main:
entity: { class: Company\BaseBundle\Entity\User, property: user_name }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login_firewall:
pattern: ^/login$
anonymous: ~
password_reset:
pattern: ^/passwordreset/*$
anonymous: ~
error_firewall:
pattern: ^/error/.*$
anonymous: ~
unsupported_broswers:
pattern: ^/unsupported$
anonymous: ~
security_question_firewall:
pattern: ^/user/(locked|security_question)/(new)*$
anonymous: ~
api_firewall:
pattern: ^/api/.*$
provider: main
http_basic:
realm: "Secured API Area. Login with your regular credentials"
provider: main
main:
pattern: ^/
form_login:
login_path: /login
check_path: /login_check
post_only: true
always_use_default_target_path: false
default_target_path: /
use_referer: true
failure_path: null
failure_forward: false
username_parameter: user_name
password_parameter: password_hash
csrf_parameter: _csrf_token
intention: authenticate
logout:
path: /logout
target: /
acl:
connection: default
根据ilanco的建议,我删除了这个:
login_firewall:
pattern: ^/login$
anonymous: ~
并将其直接添加到providers
部分:
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
但是当我访问/登录时,我遇到了重定向循环错误。
答案 0 :(得分:1)
我也一直在努力解决这个问题。
/login
不属于主防火墙,因此用户无法访问。
解决此问题的方法是删除您调用login_firewall
的自定义防火墙,并允许通过ACL访问/login
。
将以下代码添加到security.yml
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
答案 1 :(得分:0)
管理来解决这个问题 - 重定向循环的问题是由于缺少对/ login页面的访问而引起的。我只做了一个防火墙,为匿名设置了访问权限:〜,为非用户定义了access_control,瞧!
security:
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: true
anonymous: ~
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: /login
check_path: /login_check
always_use_default_target_path: true
default_target_path: /
logout:
path: /logout
target: /
providers:
main:
entity: { class: Core\UserBundle\Entity\User, property: username }
encoders:
Core\UserBundle\Entity\User:
algorithm: sha256
iterations: 10
encode_as_base64: true
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_SUPERADMIN }
- { path: ^/user, roles: ROLE_USER }
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }