
首先自己实现一个用户实体(实现 SymfonyComponentSecurityCoreUserUserInterface 中的方法),
1 2 3 4 5 6 7
|
use ComponentSecurityCoreUserUserInterface;
class UserEntity implements UserInterface { }
|
在 security.yml 定义Encoder(密码加密用)和Providers(验证令牌提供类),
1 2 3 4 5 6 7 8 9 10 11 12
|
security: ... encoders: DemoDemoBundleEntityUser: sha512
providers: demo_login: entity: class: DemoDemoBundleEntityUser property: account
|
登陆代码,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
use ComponentSecurityCoreAuthenticationTokenUsernamePasswordToken; use ComponentSecurityCoreAuthenticationEvents; use ComponentSecurityCoreEventAuthenticationEvent;
$token = new UsernamePasswordToken($user, null, 'demo_login', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$this->get('event_dispatcher')->dispatch( AuthenticationEvents::AUTHENTICATION_SUCCESS, new AuthenticationEvent($token) );
|
登出代码,
1 2
|
$this->get('security.token_storage')->setToken(null); $request->getSession()->invalidate();
|
近期评论