如何以编程方式登录/验证用户身份? [英] How to programmatically login/authenticate a user?
问题描述
我想记录用户在注册过程之后,而不被登录表单传递。
这可能吗?我发现与 FOSUserBundle
一个解决方案,但我不使用它实际上我工作的项目。
下面是我security.yml,我与两个防火墙工作。
纯文本连接codeR只是用于测试。
安全性:
EN codeRS:
Symfony的\\分量\\安全\\核心\\用户\\用户名:明文
雷\\ CentralBundle \\实体\\客户:MD5 role_hierarchy:
ROLE_ADMIN:ROLE_USER
ROLE_SUPER_ADMIN:[ROLE_USER,ROLE_ADMIN,ROLE_ALLOWED_TO_SWITCH] 供应商:
在记忆中:
用户:
管理员:{密码:admin,角色:['ROLE_ADMIN']}
实体:
实体:{类:雷\\ CentralBundle \\实体\\客户,物业:电子邮件} 防火墙:
开发:
模式:^ /(_(分析器| WDT)| CSS |图片| JS)/
安全性:假的 用户登录:
模式:^ /用户/ $登录
匿名:〜 admin_login:
模式:^ /管理/登入$
匿名:〜 管理员:
模式:^ /管理
供应商:in_memory
form_login:
check_path:/管理/登录/过程
login_path:/管理/登入
default_target_path:/管理/仪表板
登出:
路径:/管理/注销
目标:/ 现场:
模式:^ /
供应商:实体
匿名:〜
form_login:
check_path:/用户/登录/过程
login_path:/用户/登录
default_target_path:/用户
登出:
路径:/用户/注销
目标:/ 访问控制:
- {路径:^ /用户/登录,角色:IS_AUTHENTICATED_ANONYMOUSLY}
- {路径:^ /管理/登录,角色:IS_AUTHENTICATED_ANONYMOUSLY}
- {路径:^ /用户,角色:ROLE_USER}
- {路径:^ /管理,角色:ROLE_ADMIN}
- {路径:^ /,角色:IS_AUTHENTICATED_ANONYMOUSLY}
是的,你可以通过类似下面的东西做到这一点:
使用的Symfony \\分量\\的EventDispatcher \\的EventDispatcher,
Symfony的\\分量\\安全\\核心\\认证\\令牌\\ UsernamePasswordToken,
Symfony的\\分量\\安全\\ HTTP \\事件\\ InteractiveLoginEvent;公共职能registerAction()
{
// ...
如果($这个 - >获得(请求) - GT;实现getMethod()==POST)
{
// ...是否有任何密码设定等在这里 $的EM>坚持($用户);
$的EM>的flush(); //这里,公是你security.yml防火墙的名称
$令牌=新UsernamePasswordToken($用户,$用户可> getPassword来(),公开,用户可$> getRoles()); //对于旧版本的Symfony的,使用security.context这里
$这个 - >获得(security.token_storage) - > setToken($令牌); //火登录事件
//记录用户在我们这样做不会自动做到这一点,远高于
$事件=新InteractiveLoginEvent($要求,$令牌);
$这个 - >获得(event_dispatcher) - >讯(security.interactive_login,$事件); //也许重定向在这里
}
}
在年底触发事件,当你设置令牌进入上下文不会自动完成,而使用它时,如登录表单或类似的将正常进行。因此,在这里,包括它的原因。您可能需要调整使用的标记的类型,这取决于你的使用情况 - 在上面显示 UsernamePasswordToken
是一个核心的道理,但如果需要,你可以用别人。
修改:调整上述code解释公众的参数,也增加了用户的角色进入令牌创建的基础上,下面佛朗哥的评论
。I'd like to log the user in right after the registration process, without passing by the login form.
Is this possible ? I've found a solution with FOSUserBundle
, but I'm not using it on the project I'm actually working on.
Here is my security.yml, I'm working with two firewalls. The plain text encoder is just for testing.
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Ray\CentralBundle\Entity\Client: md5
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
users:
admin: { password: admin, roles: [ 'ROLE_ADMIN' ] }
entity:
entity: { class: Ray\CentralBundle\Entity\Client, property: email }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
user_login:
pattern: ^/user/login$
anonymous: ~
admin_login:
pattern: ^/admin/login$
anonymous: ~
admin:
pattern: ^/admin
provider: in_memory
form_login:
check_path: /admin/login/process
login_path: /admin/login
default_target_path: /admin/dashboard
logout:
path: /admin/logout
target: /
site:
pattern: ^/
provider: entity
anonymous: ~
form_login:
check_path: /user/login/process
login_path: /user/login
default_target_path: /user
logout:
path: /user/logout
target: /
access_control:
- { path: ^/user/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/user, roles: ROLE_USER }
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
Yes, you can do this via something similar to the following:
use Symfony\Component\EventDispatcher\EventDispatcher,
Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken,
Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
public function registerAction()
{
// ...
if ($this->get("request")->getMethod() == "POST")
{
// ... Do any password setting here etc
$em->persist($user);
$em->flush();
// Here, "public" is the name of the firewall in your security.yml
$token = new UsernamePasswordToken($user, $user->getPassword(), "public", $user->getRoles());
// For older versions of Symfony, use security.context here
$this->get("security.token_storage")->setToken($token);
// Fire the login event
// Logging the user in above the way we do it doesn't do this automatically
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
// maybe redirect out here
}
}
The event firing at the end isn't automatically done when you set a token into the context, whereas it would be normally when using eg a login form or similar. Hence the reason for including it here. You may need to adjust the type of token used, depending on your use case - the UsernamePasswordToken
shown above is a core token, but you can use others if required.
Edit: Adjusted the above code to explain the 'public' parameter and also add in the roles of the user into the token creation, based on Franco's comment below.
这篇关于如何以编程方式登录/验证用户身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!