• <tfoot id='Romnt'></tfoot>
    <legend id='Romnt'><style id='Romnt'><dir id='Romnt'><q id='Romnt'></q></dir></style></legend>

    1. <i id='Romnt'><tr id='Romnt'><dt id='Romnt'><q id='Romnt'><span id='Romnt'><b id='Romnt'><form id='Romnt'><ins id='Romnt'></ins><ul id='Romnt'></ul><sub id='Romnt'></sub></form><legend id='Romnt'></legend><bdo id='Romnt'><pre id='Romnt'><center id='Romnt'></center></pre></bdo></b><th id='Romnt'></th></span></q></dt></tr></i><div id='Romnt'><tfoot id='Romnt'></tfoot><dl id='Romnt'><fieldset id='Romnt'></fieldset></dl></div>
    2. <small id='Romnt'></small><noframes id='Romnt'>

          <bdo id='Romnt'></bdo><ul id='Romnt'></ul>

        PHP 中的 JWT(JSON Web 令牌),不使用 3rd-party 库.怎么签?

        JWT (JSON Web Token) in PHP without using 3rd-party library. How to sign?(PHP 中的 JWT(JSON Web 令牌),不使用 3rd-party 库.怎么签?)

            • <bdo id='GXP85'></bdo><ul id='GXP85'></ul>

                <small id='GXP85'></small><noframes id='GXP85'>

                <i id='GXP85'><tr id='GXP85'><dt id='GXP85'><q id='GXP85'><span id='GXP85'><b id='GXP85'><form id='GXP85'><ins id='GXP85'></ins><ul id='GXP85'></ul><sub id='GXP85'></sub></form><legend id='GXP85'></legend><bdo id='GXP85'><pre id='GXP85'><center id='GXP85'></center></pre></bdo></b><th id='GXP85'></th></span></q></dt></tr></i><div id='GXP85'><tfoot id='GXP85'></tfoot><dl id='GXP85'><fieldset id='GXP85'></fieldset></dl></div>
              • <legend id='GXP85'><style id='GXP85'><dir id='GXP85'><q id='GXP85'></q></dir></style></legend>

                    <tbody id='GXP85'></tbody>
                  <tfoot id='GXP85'></tfoot>

                  本文介绍了PHP 中的 JWT(JSON Web 令牌),不使用 3rd-party 库.怎么签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有一些库可以在 PHP 中实现 JSON Web Tokens (JWT),例如 php-jwt.我正在编写自己的非常小而简单的课程,但无法弄清楚为什么我的签名无法通过验证here,即使我已经试图坚持标准.我已经尝试了几个小时,但我被卡住了.请帮忙!

                  There are a few libraries for implementing JSON Web Tokens (JWT) in PHP, such as php-jwt. I am writing my own, very small and simple class but cannot figure out why my signature fails validation here even though I've tried to stick to the standard. I've been trying for hours and I'm stuck. Please help!

                  我的代码很简单

                  //build the headers
                  $headers = ['alg'=>'HS256','typ'=>'JWT'];
                  $headers_encoded = base64url_encode(json_encode($headers));
                  
                  //build the payload
                  $payload = ['sub'=>'1234567890','name'=>'John Doe', 'admin'=>true];
                  $payload_encoded = base64url_encode(json_encode($payload));
                  
                  //build the signature
                  $key = 'secret';
                  $signature = hash_hmac('SHA256',"$headers_encoded.$payload_encoded",$key);
                  
                  //build and return the token
                  $token = "$headers_encoded.$payload_encoded.$signature";
                  echo $token;
                  

                  base64url_encode 函数:

                  function base64url_encode($data) {
                      return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
                  }
                  

                  我的标头和有效负载完全匹配 验证站点的 默认 JWT,但我的签名不匹配,所以我的令牌是标记为无效.这个标准看起来很简单,所以我的签名有什么问题?

                  My headers and payload perfectly match the validation site's default JWT, but my signature doesn't match so my token is flagged as invalid. This standard seems really straightforward so what's wrong with my signature?

                  推荐答案

                  我解决了!我没有意识到签名本身需要进行 base64 编码.此外,我需要将 hash_hmac 函数的最后一个可选参数设置为 $raw_output=true(参见 文档.简而言之,我需要更改原始代码:

                  I solved it! I did not realize that the signature itself needs to be base64 encoded. In addition, I needed to set the last optional parameter of the hash_hmac function to $raw_output=true (see the docs. In short I needed to change my code from the original:

                  //build the signature
                  $key = 'secret';
                  $signature = hash_hmac('sha256',"$headers_encoded.$payload_encoded",$key);
                  
                  //build and return the token
                  $token = "$headers_encoded.$payload_encoded.$signature";
                  

                  致更正:

                  //build the signature
                  $key = 'secret';
                  $signature = hash_hmac('sha256',"$headers_encoded.$payload_encoded",$key,true);
                  $signature_encoded = base64url_encode($signature);
                  
                  //build and return the token
                  $token = "$headers_encoded.$payload_encoded.$signature_encoded";
                  echo $token;
                  

                  这篇关于PHP 中的 JWT(JSON Web 令牌),不使用 3rd-party 库.怎么签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  1. <i id='kvaTV'><tr id='kvaTV'><dt id='kvaTV'><q id='kvaTV'><span id='kvaTV'><b id='kvaTV'><form id='kvaTV'><ins id='kvaTV'></ins><ul id='kvaTV'></ul><sub id='kvaTV'></sub></form><legend id='kvaTV'></legend><bdo id='kvaTV'><pre id='kvaTV'><center id='kvaTV'></center></pre></bdo></b><th id='kvaTV'></th></span></q></dt></tr></i><div id='kvaTV'><tfoot id='kvaTV'></tfoot><dl id='kvaTV'><fieldset id='kvaTV'></fieldset></dl></div>

                    <small id='kvaTV'></small><noframes id='kvaTV'>

                          <tbody id='kvaTV'></tbody>
                        <legend id='kvaTV'><style id='kvaTV'><dir id='kvaTV'><q id='kvaTV'></q></dir></style></legend>
                        • <bdo id='kvaTV'></bdo><ul id='kvaTV'></ul>
                            <tfoot id='kvaTV'></tfoot>