【问题标题】:LexikJWTAuthenticationBundle Decoding token in controllerLexikJWTAuthenticationBundle 控制器中的解码令牌
【发布时间】:2018-06-11 17:23:31
【问题描述】:

如果有办法将令牌解码为用户 ID,我正在尝试在文档中找到它。此捆绑包是否提供此类选项/服务? 谢谢

【问题讨论】:

  • 到目前为止我得到了这个,但不确定如何转换实际令牌。实现接口 Symfony\Component\Security\Core\Authentication\Token\TokenInterface $jwtManager = $this->container->get('lexik_jwt_authentication.jwt_manager'); $x = $jwtManager->decode($token);

标签: symfony


【解决方案1】:

所以可行的解决方案是创建新的防火墙规则

    auto_login:
        pattern: ^/auto_login
        anonymous: true
        stateless: true
        lexik_jwt:
            query_parameter:
                enabled: true
                name:    bearer

现在我可以将令牌作为获取查询传递

/auto_login/redirect_to_some_url?bearer=eyJhbGciOiJSUzI1...

在路由“/auto_login”的控制器中,如果令牌很好,我会为用户创建会话,并且我是自动登录,看起来和工作都很好。

/**
 * @Route("/auto_login", name="auto_login")
 * @Route("/auto_login/{redirect}")
 */
public function autoLoginAction(Request $request, $redirect = null)
{
    $current_user_id = $this->getUser()->getId();
    if ($redirect && $current_user_id) {

        $user = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->find($current_user_id);

        $sf_token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
        $this->get('security.token_storage')->setToken($sf_token);
        $this->get('session')->set('_security_main', serialize($sf_token));

        $event = new InteractiveLoginEvent($request, $sf_token);
        $this->get('event_dispatcher')->dispatch('security.interactive_login', $event);

        return $this->redirectToRoute($redirect);
    } else {
        return $this->redirectToRoute('login');
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 2019-06-27
    相关资源
    最近更新 更多