问题描述
我目前正在处理 Symfony2 的安全组件.
I'm currently dealing with Symfony2's Security component.
我尝试针对网络服务对用户进行身份验证.要对用户进行身份验证,我必须向网络服务提供用户名和密码.
I try to authenticate users against a webservice. To authenticate a user, I have to provide to the webservice a username and a password.
我知道我必须创建一个实现 UserProvider 的类.但是 loadUserByUsername 函数不符合我的网络服务需求:为了验证用户身份,它要求输入用户名和密码,而 UserProvider 的函数只需要用户名.
I know that I've got to create a class that implements UserProvider. But the loadUserByUsername function doesn't fit my webservice needs : in order to authenticate a user, it ask for both username and password whereas the UserProvider's function only requires username.
这里有一个与我面临的问题类似的问题:Symfony2 authentication without UserProvider一个>
Here is a similar question to the problem I face : Symfony2 authentication without UserProvider
我已经在这个问题上苦苦挣扎了几天......
I've been struggling on this problem for a couple of days...
推荐答案
我是这样解决这个问题的:
I fixed this problem in that way:
services.yml:
services.yml:
services:
user_provider:
class: "%my_class%"
arguments: ["@service_container"]
WebServiceUserProvider.php
WebServiceUserProvider.php
/**
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->apiClient = $container->get('api_client');
$this->request = $container->get('request');
}
并在 loadUserByUsername 方法中使用 $password = $this->request->get('password');
and use $password = $this->request->get('password');
in your loadUserByUsername method
这篇关于Symfony2 通过网络服务对用户进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!