我们正在Symfony 2中从头开始构建业务应用程序,我在用户注册流程中遇到了一些问题:在用户创建帐户后,他们应该使用这些凭据自动登录,而不是立即被迫再次提供他们的证书.
任何人都有这方面的经验,或能够指出我正确的方向?
我已经在stackoverflow上阅读了很多关于此的帖子.但是大多数方法在Symfony 2.3中没用.所以我尝试在测试中手动登录用户以在后端进行一些操作.这是我的security.yml
security:
...
role_hierarchy:
ROLE_SILVER: [ROLE_BRONZE]
ROLE_GOLD: [ROLE_BRONZE, ROLE_SILVER]
ROLE_PLATINUM: [ROLE_BRONZE, ROLE_SILVER, ROLE_GOLD]
ROLE_ADMIN: [ROLE_BRONZE, ROLE_SILVER, ROLE_GOLD, ROLE_PLATINUM, ROLE_ALLOWED_TO_SWITCH]
providers:
database:
entity: { class: Fox\PersonBundle\Entity\Person, property: username }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/person/login$
security: false
main:
pattern: ^/
provider: database
form_login:
check_path: /person/login-check
login_path: /person/login
default_target_path: /person/view
always_use_default_target_path: true
logout:
path: /person/logout
target: /
anonymous: true
access_control:
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/person/registration, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/person, …Run Code Online (Sandbox Code Playgroud) 我有一个控制器,应该通过一个简单的表单添加用户,但我不能让用户手动验证.
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
public function addAction($user)
{
$token =new UsernamePasswordToken(
$user->getUsername(),
$user->getPassword(),
'secured_area',
$user->getRoles()
);
$this->get('security.context')->setToken($token);
// as suggested in some other answers
$request->getSession()->set('_security_secured_area', serialize($token));
// as suggested in http://techblog.zabuchy.net/2012/manually-authenticate-symfony-2-user/
return $this->redirect($this->generateUrl('acme_project_secure_show' )
);
}
}
Run Code Online (Sandbox Code Playgroud)
重定向到安全路由工作,但是该方法$this->getUser()返回null,因为身份验证设置不正确...我可以从用户$user= $this->get('security.context')->getToken();而不是$user= $this->get('security.context')->getToken()->getUser();快捷方式获取用户,$this->getUser() 看看这里的书
.
我正在研究一个新项目symfony 2.我正在同时学习这个框架.对于用户管理,我使用捆绑FOSUserBundle.我的项目运作良好,我可以登录,注册,注销和所有其他命令.
问题是,我想制作智能手机应用程序,它将使用我的Symfony应用程序的API.在应用中,用户必须登录或注册.是否也可以为API使用FOSUserBundle方法?
我研究了另一个非常适合制作API的软件包,它是FOSRestBundle.如果没有解决方案,你认为我将不得不创建我自己的用户方法,如:
/api/login
/api/registrer
Run Code Online (Sandbox Code Playgroud)
然后,在这个方法里面,我重定向到FOSUserBundle方法?我很想知道什么是最好的,最干净的登录方式,并通过智能手机注册FOSUserBundle,所以使用API
谢谢
我刚刚完成了本教程:http: //symfony.com/doc/current/cookbook/security/api_key_authentication.html(包括"在会话中存储身份验证")
它通过api密钥工作并授权用户,并在Session中成功存储身份验证.
但是,我没有任何想法如何通过该身份验证方法以编程方式验证用户身份.
我尝试过类似的东西:
$user = new User(
'admin',
null,
['ROLE_ADMIN']
);
$token = new PreAuthenticatedToken($user, null, "secured_area", $user->getRoles());
$this->get("security.token_storage")->setToken($token);
$request = $this->get("request");
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
Run Code Online (Sandbox Code Playgroud)
但似乎它使用了错误的身份验证提供程序.
可以请有人告诉我我做错了什么?(:
更新:
当通过上述方法完成身份验证时,会话令牌存储在"默认"防火墙下.
security:
providers:
api_key_user_provider:
id: api_key_user_provider
firewalls:
dev:
pattern: ^/(_(profiler|wdt|error)|css|images|js)/
security: false
secured_area:
pattern: ^/admin
simple_preauth:
authenticator: apikey_authenticator
default:
anonymous: ~
Run Code Online (Sandbox Code Playgroud)
为什么不使用"secured_area"防火墙而使用"默认"?如何正确强制使用"secured_area"?