【问题标题】:why authorization bearer is not sent?为什么不发送授权承载?
【发布时间】:2019-10-27 17:06:20
【问题描述】:

为了发送和接收授权持有人,我确实阅读了这个 Correct way to set Bearer token with CURL 还有这个 How to properly use Bearer tokens? 这是我的代码:

$url = "http://www.example.com/phpinfo.php";
$data = array('long_url' => 'http://www.google.com');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$header = array('Authorization: Bearer ffaaf96dd9');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
curl_close($ch);
print($response);

如您所见,我将其发送到我的 phpinfo 页面,但在我的 phpinfo 中没有看到 $_SERVER['Authorization'],我在任何地方都看不到我的令牌,我的代码有问题或者我应该怎么做检查?

编辑:example.com url 实际上是设置我的站点 phpinfo 页面。

【问题讨论】:

  • 您将example.com 更改为正确的网址?
  • 当然是的,我只是在这里编辑是为了保护隐私。这实际上是设置到我的站点 phpinfo 页面。
  • 我在自己的设置(在 docker 上使用 PHP)和 HTTP 标头信息中尝试了您的代码,我得到 - <tr><td class="e">Authorization </td><td class="v">Bearer ffaaf96dd9 </td></tr>
  • 您是如何访问该页面的?是你的phpinfo吗?是$_SERVER['Authorization']吗?还是怎么做?

标签: php curl bearer-token


【解决方案1】:

Bearer Tokens 不是由服务器自动设置的

开发人员需要创建自定义函数,对承载令牌进行编码和解码。

不记名令牌是一种对敏感数据数组进行编码以便在服务器之间安全传输的方法。

通常与其他软件结合使用,例如用于 oAuth 跨服务器 API 功能。

oAuth 是一个开源框架,允许在服务器之间创建安全通信,而不会持续存在使用密码的风险。

承载客户端允许对信息数组进行编码以用于用户身份验证和/或敏感数据传输。

根据您使用它的需要,您可能会在网上找到大量示例、插件和扩展,并且如果它附带一些 3d 派对软件,您通常会获得详尽的文档。强>

以下是基于 Wordpress CMS 的网站使用不记名令牌的示例。

1.在 Wordpress oAuth、Rest_API 和 jwt-authentication-for-wp-rest-api 上安装插件组合,然后使用您自己的插件扩展它们。

您将需要创建自定义令牌生成函数、接收 URL 点等。 然后您将能够安全地发送/接收信息,例如在 Chrome / Safari 浏览器扩展程序和您的 Wordpress 网站之间。

2。在 WordPress 网站上接收 URL 点的示例:

               add_action( 'rest_api_init', function () {
                    //apply_filters( 'determine_current_user', true );
                    register_rest_route( 'humanai/v1', 'data', array(
                 
                        'methods'  => 'POST',
                        'callback' => function($request){ 
                                global $wpdb;
                                $data = $request->get_params();
                                $query = array( 'meta_key' => 'hai-token', 'meta_value' => $data[0]['token'] );
                                $user_id = $wpdb->query('SELECT * FROM '.$wpdb->prefix.'usermeta WHERE meta_key = \'hai-token\' AND meta_value=\''. $data[0]['token'].'\'');

/* 请注意processing_function,您将使用它来处理请求并在需要时返回任何数据。 */

                                return processing_function($user_id, $request);
                        }
                 
                    ) );
                ),12);

3. processing_function

         function processing_function($user_id, $request){
              $res = update_user_meta($user_id,'new_creadit_card_number',$request['new_creadit_card_number']);
         }
  1. 当然你需要一个函数来控制不记名令牌... 不记名令牌称为 Bearer 是有原因的...因为它承载了信息,请看下面我的示例:

    function jwt_token($attr=null){
    
        $secret_key = defined('JWT_AUTH_SECRET_KEY') ? JWT_AUTH_SECRET_KEY : false;
    
        /** First thing, check the secret key if not exist return a error*/
        if (!$secret_key) {
            return new WP_Error(
                'jwt_auth_bad_config',
                __('JWT is not configured properly, please contact the admin', 'wp-api-jwt-auth'),
                array(
                    'status' => 403,
                )
            );
        }
        /** Try to authenticate the user with the passed credentials*/
        $user = wp_get_current_user();
    
        /** If the authentication fails return a error*/
        if (is_wp_error($user)) {
            $error_code = $user->get_error_code();
            return new WP_Error(
                '[jwt_auth] '.$error_code,
                $user->get_error_message($error_code),
                array(
                    'status' => 403,
                )
            );
        }
    
        /** Valid credentials, the user exists create the according Token */
        $issuedAt = time();
        $notBefore = apply_filters('jwt_auth_not_before', $issuedAt, $issuedAt);
        $expire = apply_filters('jwt_auth_expire', $issuedAt + (DAY_IN_SECONDS * 30), $issuedAt);
    
        $token = array(
            'iss' => get_bloginfo('url'),
            'iat' => $issuedAt,
            'nbf' => $notBefore,
            'exp' => $expire,
            'data' => array(
                'user' => array(
                    'id' => $user->data->ID,
                ),
            ),
        );
    
        require dirname(dirname(dirname(__FILE__))) . '/jwt-authentication-for-wp-rest-api/includes/vendor/autoload.php';
    
            /** Let the user modify the token data before the sign. */
            $token = JWT::encode(apply_filters('jwt_auth_token_before_sign', $token, $user), $secret_key);
    

/* 以下注意 令牌已签名,现在使用用户数据创建对象到客户端。 */

            $data = array(
                'token' => $token,
                'user_email' => $user->data->user_email,
                'user_nicename' => $user->data->user_nicename,
                'user_display_name' => $user->data->display_name,
                'user_new_credit_card' => 'XXXX XXXX XXXX XXXX'  
            );

            /** Let the user modify the data before send it back */
            return apply_filters('jwt_auth_token_before_dispatch', $data, $user);
        
    }

请注意:

这不是完整的功能、软件,也不是原始问题的完整解决方案。

所有信息仅用于教育目的。

我强烈建议使用其他加密方法来保护敏感信息。

当构建一个完整的功能/软件并面临新问题时,为什么不在下面的评论中将它们链接到一个新问题中呢? - 我会尽力在新的答案中提供帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    • 2018-07-04
    • 2016-02-23
    • 2018-10-13
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多