programing

WP REST API GET 요청 인증

codeshow 2023. 10. 24. 21:36
반응형

WP REST API GET 요청 인증

최근 제 사이트에 WP REST API를 설치했는데 꽤 잘 작동하고 있습니다.그러나 문제는 데이터가 인증이 필요하지 않은 공개 URL을 통해 일반인에게 접근할 수 있다는 것입니다.예를 들어 wp-json/wp/v2/posts에 대한 get request to wp-json/wp/v2/posts는 사용자에게 제공되는 모든 게시 세부 정보를 나열합니다.

GET Requests to WordPress REST API 인증 방법이 있습니까?익명의 사용자가 이 데이터를 사용할 수 있게 하고 싶지 않습니다.기본적인 인증도 저에게 효과가 있을 것입니다!

필터를 추가해야 합니다./wp-includes/default-filters.php

이 섹션 찾기:

// REST API filters.
add_action( 'xmlrpc_rsd_apis',            'rest_output_rsd' );
add_action( 'wp_head',                    'rest_output_link_wp_head', 10, 0 );
add_action( 'template_redirect',          'rest_output_link_header', 11, 0 );
add_action( 'auth_cookie_malformed',      'rest_cookie_collect_status' );
add_action( 'auth_cookie_expired',        'rest_cookie_collect_status' );
add_action( 'auth_cookie_bad_username',   'rest_cookie_collect_status' );
add_action( 'auth_cookie_bad_hash',       'rest_cookie_collect_status' );
add_action( 'auth_cookie_valid',          'rest_cookie_collect_status' );
add_filter( 'rest_authentication_errors', 'rest_cookie_check_errors', 100 );

그런 다음 새 필터를 추가합니다.

add_filter( 'rest_authentication_errors', function( $result ) {
        if ( ! empty( $result ) ) {
            return $result;
        }
        if ( ! is_user_logged_in() ) {
            return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) );
        }
        return $result;
    });

이 솔루션은 WordPress 개발자 리소스 사이트에 언급되어 있지만 필터를 추가할 위치를 구체적으로 알려주지는 않습니다.

이 변경 후 인증되지 않은 사용자가 REST API에 연결하려고 할 때 다음 메시지가 표시됩니다.

{"code":"rest_not_logged_in","message":"You are not currently logged in.","data":{"status":401}}

이 플러그인 https://wordpress.org/plugins/disable-json-api/ 을 사용하여 기록된 사용자에 대해서만 WP REST API를 제한할 수 있습니다.

플러그인을 사용하지 않으려면 테마에 이 코드를 추가할 수 있습니다.

if( ! is_user_logged_in() ) {
add_filter( 'rest_authentication_errors', 'ultimatewoo_disable_rest_api' );
function disable_rest_api( $access ) {
    return new WP_Error( 'rest_disabled', __( 'The REST API on this site has been disabled.' ), array( 'status' => rest_authorization_required_code() ) );
}}

ip로 제한이 가능합니다.

add_filter( 'rest_authentication_errors', 'filter_incoming_connections' );

function filter_incoming_connections( $errors ){

  $allowedAddress = array( '127.0.0.1' );
  $requestServer = $_SERVER['REMOTE_ADDR'];

  if( ! in_array( $requestServer, $allowedAddress ) )
    return new WP_Error( 'forbidden_access', 'Access denied', array( 'status' => 403 ) );

  return $errors;
}

$allowedAddress add authorized ip

이 코드로 코드를 변경할 수 있습니다.

add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
    return $result;
}
if ( ! is_user_logged_in() ) {
    return new WP_Error( 'restx_logged_out', 'Sorry, you must be logged in to make a request.', array( 'status' => 401 ) );
}
return $result;
});

언급URL : https://stackoverflow.com/questions/43972001/authenticate-wp-rest-api-get-requests

반응형