您好我会以编程方式验证用户,
$user = new User();
$user->setStatus(9);
$user->setUsername('Gast_'.++$id);
$user->setPassword('0');
$user->setEmail('Gast_'.$id.'@');
$user->setAvatar('avatar.jpg');
//create role
$ur = $this->em()->getRepository('AppBundle:UserRole')->findOneBy(array('role'=>'ROLE_USER'));//new UserRole();
$user->addUserRole($ur);
//login as Guest automatically
$token = new UsernamePasswordToken($user, null, 'guest', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
用户类 - >
class User extends OAuthUser implements AdvancedUserInterface, \Serializable
security.yml
# app/config/security.yml
安全性: 编码器: AppBundle \ Entity \ User:明文
role_hierarchy:
ROLE_ADMIN: [ROLE_USER, ROLE_EDITOR]
providers:
chain_provider:
chain:
providers: [user_db, hwi_oauth]
user_db:
entity: { class: "AppBundle:User" }
hwi_oauth:
id: ib_user.oauth_user_provider
firewalls:
socials:
pattern: ^/
anonymous: true
http_basic: ~
# provider: user_db
switch_user: true
form_login:
provider: user_db
login_path: /lgn
check_path: /lgn
logout:
path: /logout
target: /
remember_me:
secret: '%secret%'
lifetime: 604800 # 1 week in seconds
path: /.*
oauth:
resource_owners:
facebook: "/login/check-facebook"
login_path: /lgn
use_forward: false
failure_path: /reg
default_target_path: /
oauth_user_provider:
service: ib_user.oauth_user_provider
#hwi_oauth.user.provider.entity
guest:
pattern: ^/guest
provider: user_db
anonymous: ~
http_basic: ~
form_login:
login_path: /guest/lgn
check_path: /guest/lgn
use_referer: true
logout:
path: /logout
target: /
remember_me:
secret: '%secret%'
lifetime: 604800 # 1 week in seconds
path: /.*
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
access_control:
- { path: ^/admin/user.*, roles: ROLE_SUPER }
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: /.*, roles: IS_AUTHENTICATED_ANONYMOUSLY }
Wenn以编程方式进行身份验证的代码完成没有错误,但是如果我转到另一个页面,我会收到以下错误 - >
Neither the property "id" nor one of the methods "id()", "getid()"/"isid()" or "__call()" exist and have public access in class "HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUser".
为什么UsernamePasswordToken($user, null, 'guest', $user->getRoles())
不使用访客防火墙?
有任何想法吗?或者您需要更多信息吗?