Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript WordPress JWT-错误身份验证(401)_Javascript_Php_Jquery_Wordpress_Jwt - Fatal编程技术网

Javascript WordPress JWT-错误身份验证(401)

Javascript WordPress JWT-错误身份验证(401),javascript,php,jquery,wordpress,jwt,Javascript,Php,Jquery,Wordpress,Jwt,我不久前才发现WordPress,在使用带有自定义端点的JWT时遇到了一些问题 事实上,在正确配置API和JWT之后,我在AJAX请求期间遇到了身份验证问题 我觉得奇怪的是,我用邮递员一点问题都没有。所以我认为这是WordPress的配置问题 下面是我的代码的概述,我已经将其缩短,并尽可能清楚地说明了这一点 JWT的配置: (.htaccess) Ajax(在我的插件文件夹中) 端点(在我的孩子主题中) nonce add_action( 'rest_api_init', function ()

我不久前才发现WordPress,在使用带有自定义端点的JWT时遇到了一些问题

事实上,在正确配置API和JWT之后,我在AJAX请求期间遇到了身份验证问题

我觉得奇怪的是,我用邮递员一点问题都没有。所以我认为这是WordPress的配置问题

下面是我的代码的概述,我已经将其缩短,并尽可能清楚地说明了这一点

JWT的配置:

(.htaccess)

Ajax(在我的插件文件夹中)

端点(在我的孩子主题中)

nonce

add_action( 'rest_api_init', function () {
            register_rest_route( 'test/v1', '/test_users', array(
                'methods' => 'GET',
                'callback' => array($this, 'get_users'),
                'permission_callback' => function () {
                    return Routes::test_authorized(5);
                }
            ) );
        });

public static function test_authorized($group)
{
    global $wpdb;
    $current_user = wp_get_current_user();

    if (empty($current_user))
        return false;

    $user = ...

       ...

    if (empty($user) || $user == null || $user->test_group > $group)
        return false;

    return true;
}
add_action('rest_api_init', function () {
            add_filter('jwt_auth_token_before_dispatch', function ($data, $user) {
            // Tells wordpress the user is authenticated
            wp_set_current_user($user->ID);
            $data['nonce'] = wp_create_nonce('wp_rest');
            return $data;
            }, 10, 2);
        });
谢谢大家!

编辑:

错误消息:

{“readyState”:4,“responseText”:“{”代码“:”禁止休息“,”消息“:”抱歉, 你不允许这样做 即“,”数据“:{“状态”:401}”,“状态”:401,“状态文本”:“未经授权”}”

我刚刚做了一个测试:

public static function test_authorized($group)
{
        [...]

        echo '----------' . $current_user->user_nicename . '--------';

        [...]
}
答案如下:

{“readyState”:4,“responseText:”------------------------------------{“代码”:“禁止休息”,“消息”:“抱歉, 你不允许这样做 即“,”数据“:{“状态”:401}”,“状态”:401,“状态文本”:“未经授权”}”


因此,我的印象是,用户在添加过滤器时设置得不好。但是为什么它对邮递员有效?

请确保您在htaccess文件中添加了以下代码

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

请确保在htaccess文件中添加了以下代码

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

你能告诉我们问题出在哪里吗?是否给出了任何错误消息?
{“readyState”:4,“responseText”:“{”code:“rest\u probled”,“message:“对不起,您不允许这样做。”,“data:{”status:“401}”,“status”:401,“statusText:“Unauthorized”}
我刚刚做了一个测试:
公共静态功能测试授权($group){global$wpdb;$current\u user=wp\u get\u current\u user答案是:
{“readyState”:4,“responseText:”…{“code”:“rest\u probled”,“message:“对不起,你不允许这么做。”,“data:{“status”:401}”,“status”:401,“statusText:“Unauthorized”}
因此,我觉得用户在添加过滤器时设置得不好。但为什么它对postman起作用?@AlinoëRuntz单击edit将错误消息和任何其他信息放入您的问题中。您介意告诉我们问题出在哪里吗?是否给出了任何错误消息?
{“readyState”:4,“responseText”:“{”code”:rest_禁止”、“消息”:“抱歉,您不允许这样做。”、“数据”:{“状态”:401}”、“状态”:401,“状态文本”:“未经授权”}
我刚刚做了一个测试:
公共静态功能测试授权($group){global$wpdb;$current_user=wp_get_current_user()这里是答案:
{“readyState”:4,“responseText:”…{“code”:“rest\u forbidden”,“message:“抱歉,您不允许这样做。”,“data:{“status:401}”,“status:401”,“statusText:“Unauthorized”}
因此,我的印象是,用户在添加过滤器时设置得不好。但为什么它对postman有效?@AlinoëRuntz单击“编辑”,将错误消息和任何其他信息放入您的问题中。
public static function test_authorized($group)
{
        [...]

        echo '----------' . $current_user->user_nicename . '--------';

        [...]
}
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1