簡體   English   中英

WordPress - 如何阻止未登錄用戶訪問特定文件或目錄?

[英]WordPress - How to block Access to specific files or directories for users that are not logged in?

在我的 WordPress 網站上,觀察到機器人進行了數百次登錄嘗試。
他們嘗試使用系統中確實存在的用戶名登錄。

我想阻止這一切!

我發現,這些機器人從 URL mydomain.de/wp-json/wp/v2/users/ 獲取有關現有用戶的信息

因此,我嘗試使用在其他文章中找到的以下代碼來阻止對這些文件的訪問:

    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;
    });

這樣,所有這些登錄攻擊都停止了。

但不幸的是,這也阻止了我的插件 CONTACT FORM 7 工作! 我將其用作未登錄用戶的聯系表單。該插件嘗試訪問 /wp-json/contact-form-7/v1/contact-forms/9589/feedback,但這被代碼阻止。

我如何避免這種情況? 未登錄的用戶應該能夠使用該網站。 應該只阻止從 /wp-json/wp/v2/users/ 等獲取這些“內部信息”。

提前致謝。

用插件解決

WP Cerber 安全

它能夠阻止除某些名稱空間(如 contact-form-7)之外的所有其余 api 調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM