简体   繁体   English

如何使用symfony2安全性实现自定义编码

[英]How to implement custom encoding with symfony2 security

So the problem that I've run into is the following: Currently, the symfony2 project I have has a user entity with its own methods for encrypting its password in the database: 所以我遇到的问题如下:目前,我所拥有的symfony2项目有一个用户实体,它有自己的方法来加密数据库中的密码:

private function blowfishCrypt($password,$cost)
{
    $chars='./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    $salt=sprintf('$2a$%02d$',$cost);
    //Create a 22 character salt -edit- 2013.01.15 - replaced rand with mt_rand
    mt_srand();
    for($i=0;$i<22;$i++) $salt.=$chars[mt_rand(0,63)];
    return crypt($password,$salt);
}

public function encryptPassword($string)
{
    $this->setEncryptedPassword($this->blowfishCrypt($string, 10));
}

The login method basically just checks the entered password like so: 登录方法基本上只检查输入的密码,如下所示:

if (crypt($userPost['password'], $user->getEncryptedPassword()) != $user->getEncryptedPassword())

Then it sets session variables, authToken and userId . 然后它设置会话变量, authTokenuserId

But because of that throughout the application calls are having to be made to ensure that the userId and authToken are set in the session - so that any action we want authenticated users only to have access to we have to do a check like: 但由于这一点,整个应用程序必须进行调用以确保在会话中设置userId和authToken - 因此我们希望经过身份验证的用户只能访问的任何操作我们必须执行以下检查:

if (!authToken) { return 401 } //not exactly, but you get the idea.

My temporary solution to clean up all those checks was to create an interface that all my controllers can implement that will run some code before the controller action, and can return a redirect if the user is not logged in. What I would like to do is rewrite all of this so that I can utilize symfony2's security layer. 我清理所有这些检查的临时解决方案是创建一个接口,我的所有控制器都可以实现在控制器操作之前运行一些代码,并且如果用户没有登录则可以返回重定向。我想做的是重写所有这些,以便我可以利用symfony2的安全层。 How can I accomplish this? 我怎么能做到这一点?

EDIT: I have a separate portal running from the same code base that uses an entirely different table (like an administration portal) for users. 编辑:我有一个单独的门户网站,从相同的代码库运行,为用户使用完全不同的表(如管理门户)。 Assuming the above is possible, is this possible with symfony2's security features? 假设以上是可能的,symfony2的安全功能是否可以实现?

EDIT 2: Trying to get this all integrated, so the first step I thought would be to implement UserInterface in my User entity. 编辑2:试图将这一切全部集成,所以我认为第一步是在我的用户实体中实现UserInterface。 I've done that, and here's my security.yml: 我已经做到了,这是我的security.yml:

security:
  firewalls:
    secured_area:
      pattern: ^/
      anonymous: ~
      form_login:
        login_path: login
        check_path: login_check
  access_control:
    - { path: ^/., roles: IS_AUTHENTICATED_FULLY }
    - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/login.*, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/login_check.*, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/signup.*, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/_wdt, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/_profiler, role: IS_AUTHENTICATED_ANONYMOUSLY }
  providers:
    main:
      entity:
        class: BundleNamespace\Entity\User
        property: email
  encoders:
    Symfony\Component\Security\Core\User\User: plaintext

I get an infinite loop. 我得到一个无限循环。 Basically I need users to be able to access /, /login, /signup, etc, without logging in. Almost every other page will require an authenticated user. 基本上我需要用户能够访问/,/登录,/注册等,而无需登录。几乎每个其他页面都需要经过身份验证的用户。 I haven't gotten to the custom password encoder yet, I figure that will be my last step. 我还没有得到自定义密码编码器,我认为这将是我的最后一步。 I want to get past this redirect loop problem. 我想通过这个重定向循环问题。 Any ideas? 有任何想法吗?

You have to use a custom encoder, not a self-made thing put on top of the user entitie... 你必须使用一个自定义编码器,而不是一个自制的东西放在用户权利之上......

Give a look at : https://stackoverflow.com/a/8175657/2858398 请查看: https//stackoverflow.com/a/8175657/2858398

Then you can use native symfony connection method with your custom password encoder. 然后,您可以使用本机symfony连接方法与自定义密码编码器。

Don't want to go into too much detail because this turned out to be a pretty grueling process of conversion. 不想过多细节,因为这是一个非常艰苦的转换过程。 My only advice would be - do this sort of thing from scratch and save yourself a headache. 我唯一的建议是 - 从头开始​​做这种事情,让自己头疼。

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

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