dtyw10299 2015-11-12 15:34
浏览 40
已采纳

Symfony2可以在没有UserProviders的情况下工作吗?

The reason of this question was born from a collaborator's request: I want to know if I can remove the provider. Is mandatory? I want to remove it if I can leave its methods empty.

So, in Symfony providers configuration is mandatory. Also i need to configure at least one Provider. Done! My custom providere is this:

final class CustomListener implements UserProviderInterface                                                                
{                                                                                                                          
    public function loadUserByUsername($username)                                                                          
    {                                                                                                                      
        // ...                                                                                                             
    }                                                                                                                      

    public function refreshUser(UserInterface $user)                                                                       
    {                                                                                                                      
        return $user;                                                                                                      
    }                                                                                                                      

    public function supportsClass($class)                                                                                  
    {                                                                                                                      
        // ...                                                                                                             
    }                                                                                                                      
}

I can login. Logout. ... but I am a bit confused. Why an interface want these "useless", at the moment, loadUserByUSername and supportsClass? Useless in the meaning of: I have empty implementation, and it works!!! Work, means that I set session manually with $request->getSession()->set('firefall', $token); and user is authenticated. I can logout users invalidating session.

I am sure that at least, this provider NEEDS that refreshUser return a UserInterface:

    public function refreshUser(UserInterface $user)                                                                       
    {                                                                                                                      
        return $user;                                                                                                      
    }

Because login does not work without this return. But I can leave other methods empty.

I've looked inside tests and in UserProviderInterface method's comments. But nothing. I didnt get if I can:

  • remove provider;
  • I am missing something in the documentation;
  • I am trying to badly use Symfony (it is possible);
  • I am a Teapot;
  • ...
  • -

And, .... Why login does not work when I comment this line even if My firewall does not use this provider?

    public function refreshUser(UserInterface $user)                                                                       
    {                                                                                                                      
        // return $user;                                                                                                      
    }

--

I've created this CustomListener just because I've defined a service as provider. Then I've received this message:

Argument 1 passed to Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider::__construct() must be an instance of Symfony\Component\Security\Core\User\UserProviderInterface,

  • 写回答

1条回答 默认 最新

  • douzhun5971 2015-11-12 15:49
    关注

    The user provider tells Symfony where to load the users from, so you need one, but you don't need to create your own unless you have a complex authentication system.

    If you're loading the users from the database you can use the default db provider http://symfony.com/doc/current/cookbook/security/entity_provider.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法