Php 登录用户,无需提交表单(Symfony 3)
我有一个Php 登录用户,无需提交表单(Symfony 3),php,security,authentication,doctrine,symfony,Php,Security,Authentication,Doctrine,Symfony,我有一个User实体,它实现了UserInterface,\Serializable,equalTableInterface 它是在安全设置中设置的: security: encoders: AppBundle\Entity\User: algorithm: sha512 providers: our_db_provider: entity: class: AppB
User
实体,它实现了UserInterface,\Serializable,equalTableInterface
它是在安全设置中设置的:
security:
encoders:
AppBundle\Entity\User:
algorithm: sha512
providers:
our_db_provider:
entity:
class: AppBundle:User
property: email
firewalls:
main:
anonymous: ~
provider: our_db_provider
form_login:
login_path: login
check_path: login
access_control:
- { path: ^/ , roles: IS_AUTHENTICATED_ANONYMOUSLY}
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/registration, roles: IS_AUTHENTICATED_ANONYMOUSLY }
#...
我想从数据库中选择一个用户,并以所选用户的身份登录当前访问者
$user = $em->getRepository('App\Entity\User')->find($user_id);
我不知道该怎么办。我没有使用
FOSUserBundle
。我已经搜索了解决方案,但没有找到任何内容,只有AuthenticationUtils
,它只适用于表单提交。在\Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener中,您可以找到显示如何在不执行表单提交的情况下手动进行授权的行:
$request->getSession()->set(Security::LAST\u USERNAME,$USERNAME)代码>
return$this->authenticationManager->authenticate(新用户名密码令牌($username,$password,$this->providerKey))代码>
$this->providerKey
可以从security.yml
中获取,作为防火墙部分的密钥
如果防火墙部分看起来像
firewalls:
secured_area:
pattern: ^/
form_login:
check_path: login_check
login_path: login
logout:
path: /logout
然后providerKey=安全的\u区域
AuthenticationManager可以从security.authentication.manager服务获取
希望它能起作用 可能重复您询问如何切换到用户: