本文介绍了没有数据库的 PHP Symfony2 自定义身份验证 (SOAP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个肥皂来验证用户
I have a soap to authenticate users
<?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 中使用,只在内存中使用...
I would like to use in Symfony2 without database, only in memory...
我试图实现一个 自定义 UserProvider,但我没有拥有使其工作所需的所有数据...
I tried to implement a Custom UserProvider, but I do not have all the data needed to make it work...
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 服务检查密码.
As I understand, you want to use form authentication and check password using SOAP service.
如果您使用 Symfony 2.4 或更高版本,则可以使用 SimpleFormAuthenticatorInterface
If you use Symfony 2.4 or above, you can use 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);
}
}
这篇关于没有数据库的 PHP Symfony2 自定义身份验证 (SOAP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!