限制对WordPress REST API的访问

Mar*_*arc 5 php wordpress wordpress-rest-api

有没有一种方法可以限制对WP REST API进行的URL调用的访问?我正在使用WP REST API创建可通过URL访问的AJAX供稿。它们的格式如下:http://example.com/wp-json/posts?type=post&filter[posts_per_page]=10

问题是任何人都可以添加/wp-json/posts?type=post&filter[posts_per_page]=10到我的URL的末尾并检索此信息的提要。当用户未登录WordPress时,我想关闭此功能:

if ( !is_user_logged_in()) {
    // Turn off REST API feed
}
Run Code Online (Sandbox Code Playgroud)

或者,我想添加某种身份验证,需要添加这些身份验证以屏蔽api。

我在网上找到了类似的东西,但是我并没有运气。我将其添加到自定义插件。不幸的是,如果未登录,我仍然可以访问供稿。

add_action( 'init', function() {
    global $wp_post_types;
    $wp_post_types['post']->show_in_rest = is_user_logged_in();
}, 20 );
Run Code Online (Sandbox Code Playgroud)

我担心在激活API和在前端发出HTTP请求之间无法建立连接。我在考虑这个错误吗?有没有人遇到这个问题?

谢谢!

Fle*_*bel 8

Wordpress 的问题和好处是它允许太多的灵活性,特别是当平台提供一个干净的方法时:所有请求都需要身份验证

您可以通过向rest_authentication_errors过滤器添加is_user_logged_in 检查来要求对所有REST API 请求进行身份验证。

注意:传入的回调参数可以是 null、WP_Error 或布尔值。参数的类型表示认证的状态:

  • null:尚未执行任何身份验证检查,并且钩子回调可能会应用自定义身份验证逻辑。
  • boolean:表示执行了先前的身份验证方法检查。boolean true 表示请求已成功
    验证,boolean false 表示验证失败。
  • WP_Error:遇到某种错误。
add_filter( 'rest_authentication_errors', function( $result ) {
    // If a previous authentication check was applied,
    // pass that result along without modification.
    if ( true === $result || is_wp_error( $result ) ) {
        return $result;
    }

    // No authentication has been performed yet.
    // Return an error if user is not logged in.
    if ( ! is_user_logged_in() ) {
        return new WP_Error(
            'rest_not_logged_in',
            __( 'You are not currently logged in.' ),
            array( 'status' => 401 )
        );
    }

    // Our custom authentication check should have no effect
    // on logged-in requests
    return $result;
});
Run Code Online (Sandbox Code Playgroud)

公平地说,这隐藏在常见问题中。

编辑:排除 jwt-auth

global $wp;

// No authentication has been performed yet.
// Return an error if user is not logged in and not trying to login.
if ( ! is_user_logged_in() && $wp->request !== 'wp-json/jwt-auth/v1/token' ) {
    return new WP_Error(
        'rest_not_logged_in',
        __( 'You are not currently logged in.' ),
        array( 'status' => 401 )
    );
}
Run Code Online (Sandbox Code Playgroud)


Col*_*una 5

这将为未登录的用户删除 WordPress 和 Woocommerce 的所有 REST API 端点:

function myplugin_removes_api_endpoints_for_not_logged_in() {

    if ( ! is_user_logged_in() ) {

        // Removes WordpPress endpoints:
        remove_action( 'rest_api_init', 'create_initial_rest_routes', 99 );

        // Removes Woocommerce endpoints
        if ( function_exists('WC') )
            remove_action( 'rest_api_init', array( WC()->api, 'register_rest_routes' ), 10 );
    }

} add_action('init', 'myplugin_removes_api_endpoints_for_not_logged_in');
Run Code Online (Sandbox Code Playgroud)


dav*_*mor 1

这将阻止任何未登录的人使用整个 REST API:

function no_valid_user_no_rest($user) {
    if (!$user) {
        add_filter('rest_enabled', '__return_false');
        add_filter('rest_jsonp_enabled', '__return_false');
    }
    return $user;
}
add_filter('determine_current_user', 'no_valid_user_no_rest', 50);
Run Code Online (Sandbox Code Playgroud)

标准警告是,这只是关闭 REST,仅此而已。确保它有足够的优先级位于其他determine_current_user过滤器之后。没有在多站点上进行测试。

如果您想按 URL 或其他因素进行过滤,您也可以向条件添加其他测试。