PHP Symfony2无数据库自定义身份验证(SOAP)


PHP Symfony2 custom authentication without database (SOAP)

我有一个soap来验证用户

<?php
$client = new SoapClient('http://mysite/code/soap.wsdl');
$user   = $client->login('username','password');
if( isset($user['IdAccount']) && ($user['IdAccount'] > 0) )
   echo 'Logon OK';
else
   echo 'Logon Fail!';

我想在没有数据库的Symfony2中使用,只在内存中使用。。。

我试图实现一个自定义用户提供程序,但我没有使其工作所需的所有数据。。。

class WebserviceUserProvider implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $client = new SoapClient('http://mysite/code/soap.wsdl');
        $user   = $client->login($username, PASSWORD???? );
        if ( isset($user['IdAccount']) && ($user['IdAccount'] > 0) ) 
            return new WebserviceUser($username, PASSWORD????, SALT???, ROLES????);
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)
    );
}

我不能换肥皂

抱歉我英语不好!:(

据我所知,您希望使用SOAP服务使用表单身份验证和检查密码。

如果您使用Symfony 2.4或更高版本,您可以使用SimpleFormAuthenticatorInterface

示例实现:

// ... namespace and uses
class Authenticator implements SimpleFormAuthenticatorInterface
{
    private $soapClient;
    public function __construct('SoapClient $soapClient)
    {
        $this->soapClient = $soapClient;
    }
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
    {
        $user = $this->soapClient->login($token->getUsername(), $token->getCredentials());
        if (isset($user['IdAccount']) && ($user['IdAccount'] > 0)) {
            $user = $userProvider->loadUserByUsername($token->getUsername());
            return new UsernamePasswordToken(
                $user,
                null,
                $providerKey,
                $user->getRoles()
            );
        }
        throw new AuthenticationException('Invalid username or password');
    }
    public function supportsToken(TokenInterface $token, $providerKey)
    {
        return $token instanceof UsernamePasswordToken
            && $token->getProviderKey() === $providerKey;
    }
    public function createToken(Request $request, $username, $password, $providerKey)
    {
        return new UsernamePasswordToken($username, $password, $providerKey);
    }
}