首页 > 解决方案 > 为什么不发送授权承载?

问题描述

要发送和接收授权不记名,我确实阅读了 正确的方法来使用 CURL 设置不记名令牌 和这个 如何正确使用不记名令牌? 这是我的代码:

$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 页面,但$_SERVER['Authorization']在我的 phpinfo 中看不到,我在任何地方都看不到我的令牌,我的代码有问题或者我应该检查什么?

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

标签: phpcurlbearer-token

解决方案


服务器不会自动设置不记名令牌

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

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

通常与其他软件结合使用,例如用于 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 令牌...有一个原因 Bearer 令牌称为 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);
        
    }

请注意:

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

所有信息均严格用于教育目的。

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

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


推荐阅读