【问题标题】:Manually Encode a Password in php symfony在 php symfony 中手动编码密码
【发布时间】:2020-08-02 21:01:12
【问题描述】:

我想手动编码密码(登录密码不是 api 哈希) 在 Symfony 中

这里是安全配置:

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        login:
            pattern:  ^/auth-api/login
            stateless: true
            anonymous: true
            json_login:
                check_path:               /auth-api/login_check
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure

        api:
            pattern:   ^/auth-api
            stateless: true
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator
        main:
            pattern: ^/
            user_checker: security.user_checker
            form_login:
                default_target_path: /accounts
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager

            logout:       true
            anonymous:    true

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        # - { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }
        - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api,       roles: IS_AUTHENTICATED_ANONYMOUSLY }

我在 APiController.php 中尝试此代码

namespace App\Controller;

use App\Repository\AccountsRepository;
use InstagramAPI\Instagram;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
.....
$encoder = $this->get('security.encoder_factory')->getEncoder($userClass);
$encodedPassword = $encoder->encodePassword("test");
   echo $encoded;

收到此错误

Service "security.encoder_factory" not found:即使它存在于应用程序的容器中,"App\Controller\APiController" 内的容器是一个较小的服务定位器,只知道 "doctrine"、"form.factory" 、“http_kernel”、“parameter_bag”、“request_stack”、“router”、“security.authorization_checker”、“security.csrf.token_manager”、“security.token_storage”、“serializer”、“session”、“template”和“树枝”服务。尝试使用依赖注入。

我也试试这个代码:

....
use FOS\UserBundle\Model\User;
...
   $user = new App\Entity\User();
    $plainPassword = 'ryanpass';
    $encoded = $encoder->encodePassword($user, $plainPassword);

    $user->setPassword($encoded);

收到此错误:

不能将 FOS\UserBundle\Model\User 用作用户,因为该名称已在使用中

【问题讨论】:

  • 给定的代码不完整。此外,为什么不为此使用自动装配?

标签: php symfony


【解决方案1】:

如前所述,使用依赖注入https://symfony.com/doc/current/service_container/autowiring.html

对于控制器,您可以通过__constructor 或作为操作的argument 绑定服务

使用控制台 cmd 查找其类型提示 bin/console debug:autowiring encoder_factory

【讨论】:

    猜你喜欢
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2020-03-02
    • 1970-01-01
    相关资源
    最近更新 更多