是的,您需要創建自己的用戶提供程序並將其作爲服務添加到Symfony應用程序中。創建必須實現UserProviderInterface
爲了讓您的類開始這裏的用戶提供程序類是位於命名空間/包/安全/ Provider.php我的自定義類的一個實例:
namespace CB\WebsiteBundle\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use CB\WebsiteBundle\Entity\User;
class Provider implements UserProviderInterface {
protected $user;
public function __contsruct (UserInterface $user) {
$this->user = $user;
}
/**
* Loads the user for the given username.
*
* This method must throw UsernameNotFoundException if the user is not
* found.
*
* @throws UsernameNotFoundException if the user is not found
* @param string $username The username
*
* @return UserInterface
*/
function loadUserByUsername($username) {
$user = User::find(array('username'=>$username));
if(empty($user)){
throw new UsernameNotFoundException('Could not find user. Sorry!');
}
$this->user = $user;
return $user;
}
/**
* Refreshes the user for the account interface.
*
* It is up to the implementation if it decides to reload the user data
* from the database, or if it simply merges the passed User into the
* identity map of an entity manager.
*
* @throws UnsupportedUserException if the account is not supported
* @param UserInterface $user
*
* @return UserInterface
*/
function refreshUser(UserInterface $user) {
return $user;
}
/**
* Whether this provider supports the given user class
*
* @param string $class
*
* @return Boolean
*/
function supportsClass($class) {
return $class === 'MC\WebsiteBundle\Entity\User';
}
}
的一些關鍵這個類的注意事項是它使用了一個自定義的用戶實體,你將自己定義這個實體,這將有助於連接到你的Java中間件。在我的情況下,我連接到REST api來獲取我的用戶信息,我的用戶類使用User :: find($ criteria)靜態函數通過用戶名查找用戶。
一旦你有你自己的用戶類,它與你的中間件交互,你有你的新的供應商,你需要添加提供程序作爲服務組合中的配置:YourBundle /資源/ config.xml中:
<parameters>
<parameter key="cb_security_user.class">CB\WebsiteBundle\Entity\User</parameter>
<parameter key="cb_security_provider.class">CB\WebsiteBundle\Security\Provider</parameter>
</parameters>
<services>
<service id="cb_security_user" class="%cb_security_user.class%" />
<service id="cb_security_provider" class="%cb_security_provider.class%">
<argument type="service" id="cb_security_user" />
</service>
</services>
你可以找到我的博客張貼在這裏更多的細節:Custom User Providers in Symfony2
希望這有助於!
好奇你爲什麼選擇一個爲期6天的,稀疏記錄的重大項目框架? –