简体   繁体   English

Symfony2中的安全性:定制用户提供程序如何管理验证密码?

[英]Security in Symfony2: How a custom user provider manages to validate passwords?

I'm following the Symfony cookbook on creating a custom user provider but i have no idea how to validate the user password. 我正在遵循Symfony食谱来创建自定义用户提供程序,但是我不知道如何验证用户密码。 Is this a task of the User Provider? 这是用户提供者的任务吗? Or this should happen elseware? 还是应该发生其他软件? How do i make sure the password he submitted via form equals the one in the database? 我如何确保他通过表单提交的密码等于数据库中的密码?

The user provider's not the one that handles the authentication. 用户提供者不是处理身份验证的提供者。 That's the task of the authentication provider. 这是身份验证提供程序的任务。

Take a look at the neighbouring article How to create a custom Authentication Provider to get some ideas. 看一下相邻的文章如何创建自定义身份验证提供程序以获取一些想法。

You could also take a look at Symfony\\Component\\Security\\Core\\Authentication\\Provider\\DaoAuthenticationProvider. 您还可以查看Symfony \\ Component \\ Security \\ Core \\ Authentication \\ Provider \\ DaoAuthenticationProvider。 See how its constructor accepts an instance of the userProvider and then uses it in retrieveUser() to get the user's data from the DB? 看看其构造函数如何接受userProvider的实例,然后在retrieveUser()中使用它以从数据库获取用户的数据? Also, in UserAuthenticationProvider abstact class, which the DaoAuthenticationProvider, this data is passed to checkAuthentication() method (implemented by DaoAuthenticationProvider). 此外,在DaoAuthenticationProvider的UserAuthenticationProvider abstact类中,此数据传递到checkAuthentication()方法(由DaoAuthenticationProvider实现)。 There it uses the encoder associated with the User class to first encode the password of the UserInterface instance retrieved from the UsernamePasswordToken, then compare it to the already encoded version retrieved from the database. 在那里,它使用与User类关联的编码器来对从UsernamePasswordToken检索到的UserInterface实例的密码进行编码,然后将其与从数据库检索到的已编码版本进行比较。

EDIT: regarding your comment: I've done something similar to this not so long ago (only I used a standard form login with a VERY nonstandard authentication manager (: ), but unfortunately I don't quite remember the implementation. What I remember is that I had to read a lot of Symfony2 source code... You could try starting with the implementation of your custom AuthenticationProvider. You could try to base it on the UserAuthenticationProvider abstract class and use DaoAuthenticationProvider as a sort of a reference... that is, you could implement retrieveUser() in such a way that it uses your user provider to retrieve the relevant data, then also implement the checkAuthentication() method in such a way that it makes use of the retrieved user's data. Then, if your user's credentials are encoded in some way, i think you'll need to specify an appropriate encoder in the secruty.encoders config for your custom user class. Oh, and don't forget about the authentication provider factory and cu 编辑:关于您的评论:不久前我已经做过类似的事情(只有我使用带有非常非标准身份验证管理器(:)的标准表单登录名,但是不幸的是,我不太记得实现。是我必须阅读很多Symfony2源代码...您可以尝试从自定义AuthenticationProvider的实现开始。您可以尝试将其基于UserAuthenticationProvider抽象类,并使用DaoAuthenticationProvider作为一种引用。也就是说,您可以通过使用用户提供者来检索相关数据的方式来实现retrieveUser(),然后可以利用所检索到的用户数据的方式来实现checkAuthentication()方法。您的用户凭据以某种方式编码,我认为您需要在secruty.encoders配置中为您的自定义用户类指定适当的编码器。哦,别忘了身份验证提供程序的factory和cu stom security tokens. stom安全令牌。

I'm really sorry I some information that I provided is incorrect. 非常抱歉,我提供的某些信息不正确。 It's just that I don't have my implementation in front of me right now (and, to be absolutely frank, I'm too lazy to go diggin though Symfony 2 code again right now). 只是我现在还没有实现自己的实现(而且,坦率地说,我很懒,现在就再次通过Symfony 2代码进行挖掘)。

ANOTHER EDIT : concerning the configuration part. 另一个编辑 :关于配置部分。 Yet again, I'm not sure if I remember correctly, but I THINK that you definitely need to specify your custom auth provider's factory in the config. 再一次,我不确定我是否记得正确,但是我认为您肯定需要在配置中指定自定义身份验证提供程序的工厂。 It will then give you the ability to include the key (that you specified in the factory's getKey() method) in your firewall's config, and this will enable your custom auth provider for this firewall. 然后,它将使您能够将密钥(在工厂的getKey()方法中指定的密钥)包括在防火墙的配置中,这将启用此防火墙的自定义身份验证提供程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM