【问题标题】:JWT And Routes Symfony3JWT 和路由 Symfony3
【发布时间】:2016-11-08 09:31:30
【问题描述】:

我的 security.yml 文件中有两个防火墙:

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        login:
            pattern:  ^/api/v1/auth$
            stateless: true
            anonymous: true
            provider: fos_userbundle
            form_login:
                check_path:               /api/v1/auth
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false

        api:
            pattern:   ^/api/v1
            stateless: true
            provider: fos_userbundle
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

以及AuthController中的两条路由:

/**
 * @param ParamFetcherInterface $paramFetcher
 *
 * @Rest\Post("/auth")
 * @Rest\RequestParam(name="email", strict=true)
 * @Rest\RequestParam(name="password", strict=true)
 *
 * @return array
 */
public function postTokenAuthAction (ParamFetcherInterface $paramFetcher)
{
    if($user = $this->getUser()) {
        return $user->getRoles();
    }

    $email = $paramFetcher->get('email');
    $password = $paramFetcher->get('password');

    /** @var User|null $user */
    $user = $this->getDoctrine()->getRepository('AppBundle:User')->findOneByEmail($email);

    if(!$user || !$this->get('security.password_encoder')->isPasswordValid($user, $password)) {
        throw new HttpException(403, $this->get('translator')->trans('auth.error'));
    }

    $token = $this->get('lexik_jwt_authentication.encoder')->encode([
        'email' => $user->getEmail()
    ]);

    return ['access_token' => $token];

}

/**
 * @param ParamFetcherInterface $paramFetcher
 *
 * @Rest\Post("/auth/check")
 *
 * @return array
 */
public function postCheckLoginAction (ParamFetcherInterface $paramFetcher)
{
    /** @var User $user */
    $user = $this->getUser();

            if (!$user) {
        throw $this->createAccessDeniedException();
    }

    return $user->getRoles();

}

我在 /api/v1/auth 中发送了 POST 请求,并带有 POST email=&password= 参数以获取 access_token。但我收到 401 错误“凭据错误”。

好的。接下来,我将login 防火墙中的参数pattern 更改为^/api/v1/authform_login.check_path 更改为/api/v1/auth/check,它工作正常。 现在我可以通过电子邮件和密码登录并获取 access_token。

但路由 /api/v1/auth/check 现在返回错误凭据。它试图通过 emailpassword 在这条路线上授权我,但我希望它尝试通过 Authorization 标头授权。

为什么会出错?

最后,我想将emailpassword 发送到/api/v1/auth 以获取access token,然后将access_token 发送到/api/v1/auth/check 并获取用户角色。

【问题讨论】:

  • email=&password=
  • 我知道。这只是问题的例子。我在 POST 正文中发送此参数。
  • 您使用的是哪个版本的 jwt?
  • 稳定版 2.0.2。实际上,我认为这是 symfony security.yml 的问题,而不是 JWT Bundle

标签: php symfony lexikjwtauthbundle


【解决方案1】:

您是否尝试使用 _username 和 _password(使用低破折号)代替您使用的那些?默认情况下,这是存储此值的字段。

我希望这对你有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 2017-12-27
    • 2016-07-22
    • 2021-06-18
    • 1970-01-01
    相关资源
    最近更新 更多