簡體   English   中英

htaccess Pass Protect 干擾 Wordpress 功能

[英]htaccess Pass Protect Interfereing With Wordpress functionality

我的主持人要求我實施htaccess 身份驗證,以便為我的 Wordpress 安裝添加額外的安全層。

訪問 Wordpress 管理區域時,我被要求進行第二個(服務器)級別的身份驗證。 這很好,但是我的網站上有一個頁面是通過保護的。 不幸的是,此頁面的所有用戶也被要求提供 htaccess 憑據。

有沒有更好的方法來添加這種蠻力保護? 或者也許是一種從要求第二因素身份驗證中排除特定頁面的方法?

還有,后者是否會削弱和破壞二次登錄的目的?

這是教程中的相關代碼...

ErrorDocument 401 "Unauthorized Access" ErrorDocument 403 "Forbidden" <FilesMatch "wp-login.php"> AuthName "Authorized Only" AuthType Basic AuthUserFile /home/username/.wpadmin require valid-user </FilesMatch>

編輯:這種方法對於擁有訂閱者的 WP 網站似乎也不可行。 他們肯定需要訪問 wp-login.php。

當您可以更改 WordPress 密碼表單時,為什么要更改 htaccess。 這是對我有用的東西。

add_filter( 'the_password_form', 'wpsight_password_form_dmd' );
function wpsight_password_form_dmd($output){
    global $wp;
    $post   = get_post( $post );
    $label  = 'pwbox-' . ( empty( $post->ID ) ? rand() : $post->ID );
    $output = '<form action="' . esc_url( home_url( $wp->request ).'/?postpassnew123=1' ) . '" class="post-password-form" method="post">
    <p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p>
    <p><label for="' . $label . '">' . __( 'Password:' ) . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr_x( 'Enter', 'post password form' ) . '" /></p></form>
    ';
return $output;    
}
if(isset($_GET['postpassnew123'])){
    if ( !isset($_POST['post_password'] ) ) {
        wp_safe_redirect( wp_get_referer() );
        exit();
    }
    require_once ABSPATH . WPINC . '/class-phpass.php';
    $hasher = new PasswordHash( 8, true );

    $expire  = apply_filters( 'post_password_expires', time() + 10 * DAY_IN_SECONDS );
    $referer = wp_get_referer();
    if ( $referer ) {
        $secure = ( 'https' === parse_url( $referer, PHP_URL_SCHEME ) );
    } else {
        $secure = false;
    }
    setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $_POST['post_password'] ) ), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );

    wp_safe_redirect( wp_get_referer() );
    exit();
}

您可以使用與請求的 URI 匹配的環境變量來排除頁面:

ErrorDocument 401 "Unauthorized Access" 
ErrorDocument 403 "Forbidden" 

SetEnvIfNoCase Request_URI ^/path/to/no-protect-page$ ok_uri=true

AuthName "Authorized Only" 
AuthType Basic 
AuthUserFile /home/username/.wpadmin 
Require valid-user
Allow from env=ok_uri
Satisfy Any

所以這個 htaccess 文件所在的目錄將受到密碼保護,除了/path/to/no-protect-page請求。

如果您使用以下指令,應該可以工作:

# passwordprotect wp-login.php
<Files wp-login.php>
<If "%{QUERY_STRING} != 'action=postpass'">
    AuthUserFile /path/to/passwd/file/.htpasswd
    AuthGroupFile /dev/null
    AuthName 'Login Message'
    AuthType Basic
    Require valid-user
</If>
</Files>

它的作用是,每次提供“action”字符串並且是“postpass”時跳過基本身份驗證。

... 只是一個想法:

我需要自己進一步測試,因為我不知道它會如何影響 Wordpress 的內部路由,例如 /action/postpass/。

暫無
暫無

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

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