symfony登录自定义webservice

时间:2014-05-20 12:28:46

标签: web-services symfony authentication login

我正在进行自定义Web服务身份验证,需要用户名作为密码组合,例如通过POST方法传递的“username_password”。
这是我得到的: 我按照this文档创建了Web服务用户和用户提供程序类,并根据this指南配置了security.yml。 但是因为我需要将密码传递给UserProvider,所以我通过将请求服务传递给UserProvider类来获得this解决方案中提供的解决方案,并得到了这个:

class WebserviceUserProvider implements UserProviderInterface
{
    private $request;
    private $buzz;
    private $password;

    public function __construct(ContainerInterface $container)
    {
        $this->request = $container->get('request');
        $this->buzz = $container->get('buzz');
    }

    public function loadUserByUsername($username)
    {
        if ($this->request->get('_password')) {
            $this->password = $this->request->get('_password');
        }

        //remote air.salda.lt login service
        $userData = $this->buzz->post(
            'http://remote.service.url', 
            array(), 
            array("UID" => $username.'_'.$this->password)
        );

        if ($userData->getContent() == 'OK') {
            $password = $this->password;
            $salt = ''; 

            //here should be additional logic to fetch user roles from db, specific to application 

            $roles = array('ROLE_USER');

            return new WebserviceUser($username, $password, $salt, $roles);
        }

        throw new UsernameNotFoundException(
            sprintf('Username "%s" does not exist.', $username)
        );
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof WebserviceUser) {
            throw new UnsupportedUserException(
                sprintf('Instances of "%s" are not supported.', get_class($user))
            );
        } 

        $this->password = $user->getPassword();

        return $this->loadUserByUsername($user->getUsername());
    }

    public function supportsClass($class)
    {
        return $class === 'Cc\ClientControlBundle\Security\User\WebserviceUser';
    }
}

请注意“loadUserByUsername”方法

if ($this->request->get('_password')) {
    $this->password = $this->request->get('_password');
}

在“refreshUser”方法中

$ this-> password = $ user-> getPassword();

我这样做是因为,当第一次从请求获取密码时它工作正常,但是当“refreshUser”发生时,请求对象似乎缺少密码,因此通过在类中全局保存密码,我可以在以后访问它。 / p>

这个解决方案有效,但对我来说这似乎是一个黑客,我真的想妥善解决这个问题,但我不知道如何,也许通过使用Custom Authenticator Provider?认为这看起来真的很复杂。一些建议和指导会有所帮助。

0 个答案:

没有答案