简体   繁体   English

Lexic/JWTAuthenticationBundle - 无法通过查询参数提交令牌

[英]Lexic/JWTAuthenticationBundle - cannot submit token via query parameter

I am trying to upgrade the Lexic/JWTAuthenticationBundle from version 1.7 to 2.14.我正在尝试将 Lexic/JWTAuthenticationBundle 从版本 1.7 升级到 2.14。 (This is in support of our upgrade of the Symfony bundle from version 3 to 4, and eventually 5). (这是为了支持我们将 Symfony 包从版本 3 升级到版本 4,最终升级到版本 5)。

Some of our code makes API queries with the token as a query parameter (meaning in the URL).我们的一些代码使用令牌作为查询参数(在 URL 中表示)进行 API 查询。 This stopped working after we upgraded.这在我们升级后停止工作。 Calls that put the token into curl request headers still work.将令牌放入 curl 请求标头的调用仍然有效。

Here is an example of a call that works on the old setup, but not the new ($token is supplied by a previous call to our API to login):这是一个适用于旧设置但不适用于新设置的调用示例($token 由先前调用我们的 API 提供以进行登录):

$curl_opts = array(
  \CURLOPT_URL            => $our_url . "?bearer=" . $token;
  \CURLOPT_SSL_VERIFYPEER => false,
  \CURLOPT_SSL_VERIFYHOST => false,
  \CURLOPT_RETURNTRANSFER => 1,
  \CURLOPT_USERAGENT      => 'cURL Request',
  \CURLOPT_CONNECTTIMEOUT => 5,
  \CURLOPT_TIMEOUT        => 10, 
  \CURLOPT_HEADER         => false, 
  \CURLOPT_CUSTOMREQUEST  => GET,
  \CURLOPT_POSTFIELDS     => '',
  \CURLOPT_HTTPHEADER     => array(
                                'Content-Type: application/x-www-form-urlencoded',
                                'Content-Length: ' . 0
                             ),
);
$curl = curl_init();
curl_setopt_array($curl, $curl_opts);
$data = curl_exec($curl);
curl_close($curl);

This returns a 500 error, and this error message is logged:这将返回 500 错误,并记录此错误消息:

request.CRITICAL: Uncaught PHP Exception Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTDecodeFailureException: "Invalid JWT Token" at /oursymfony/vendor/lexik/jwt-authentication-bundle/Encoder/LcobucciJWTEncoder.php line 53 {"exception":"[object] (Lexik\\Bundle\\JWTAuthenticationBundle\\Exception\\JWTDecodeFailureException(code: 0): Invalid JWT Token at /oursymfony/vendor/lexik/jwt-authentication-bundle/Encoder/LcobucciJWTEncoder.php:53, Lcobucci\\JWT\\Token\\InvalidTokenStructure(code: 0): The JWT string must have two dots at /var/www/nest/core/vendor/lcobucci/jwt/src/Token/InvalidTokenStructure.php:13)"} []

If I make the same call, but add the token in the curl headers instead of the URL, the call works.如果我进行相同的调用,但在 curl 标头中添加令牌而不是 URL,则调用有效。

I believe we have correctly configured this to work.我相信我们已经正确配置它来工作。 Here is our configuration:这是我们的配置:

lexik_jwt_authentication:
    private_key_path:    ""
    public_key_path:     ""
    # ...
    token_extractors:
        authorization_header:       # check token in Authorization Header
            enabled: true
            prefix:  Bearer         # case-sensitive...can also be an arbitrary string ('SCStoken')
        cookie:                     # check token in a cookie
            enabled: true
            name:    BEARER
        query_parameter:            # check token in query string parameter
            enabled: true
            name:    bearer

I looked a bit at the code where the error is thrown, and noticed that on the broken call, the token is empty, but on the working call it's there.我看了看引发错误的代码,注意到在中断的调用中,令牌是空的,但在工作调用中它就在那里。

It seems to me that the token needs to be sent in the request header在我看来,令牌需要在请求中发送 header

  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/x-www-form-urlencoded',
    'Authorization: Bearer you-token'
  ),

This will probably not be helpful to other people, but I believe the source of the issue was a class written by our team that interacted with the other security components including the lexik_jwt_authentication plugin.这可能对其他人没有帮助,但我相信问题的根源是我们团队编写的 class,它与其他安全组件(包括 lexik_jwt_authentication 插件)交互。

I don't understand this, but prior to the upgrade, our code evaluated tokens passed in the header, and lexik evaluated tokens passed in the header or the query parameter.我不明白这一点,但是在升级之前,我们的代码评估了在 header 中传递的令牌,而 lexik 评估了在 header 或查询参数中传递的令牌。 After the upgrade, the lexik code didn't evaluate tokens.升级后,lexik 代码没有评估标记。 (The code was still there, it just wasn't reached. Possibly because of how our code interacted with it). (代码仍然在那里,只是没有到达。可能是因为我们的代码如何与它交互)。 However, I was able to modify our code to handle both types of evaluation.但是,我能够修改我们的代码来处理这两种类型的评估。

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

相关问题 尝试从名称空间“ Lexik \\ Bundle \\ JWTAuthenticationBundle \\ DependencyInjection \\ Security \\ Factory”加载类“ JWTUserFactory” - Attempted to load class “JWTUserFactory” from namespace “Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\Security\Factory” Symfony4/Doctrine - 无法通过 SSL 连接(DBAL 配置) - Symfony4/Doctrine - Cannot connect via SSL (DBAL Config) 无法刷新令牌,因为用户已更改 - Syfmony 4 - LDAP - Cannot refresh token because user has changed - Syfmony 4 - LDAP 由于用户更改了Syfmony 4-EquatableInterface问题,因此无法刷新令牌 - Cannot refresh token because user has changed Syfmony 4 - EquatableInterface problem 无法解析 Symfony 配置文件中布尔节点中的参数 - Cannot resolve parameter in boolean node in Symfony config files 提交后如何仅获取更改的实体? - How to get the changed entities only, after a submit? symfony 4多个表格循环树枝提交 - symfony 4 multiple forms loop twig submit 无法使用 LexikBundle 以编程方式签署 JWT 令牌 - Unable to sign JWT Token programmatically with LexikBundle Nelmio API 文档区域和不记名令牌与 symfony 4 - Nelmio API doc area and bearer token with symfony 4 令牌存储不包含身份验证令牌,并且denyAccessUnlessGranted() - Token storage contains no authentication token and denyAccessUnlessGranted()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM