允许不属于白名单iPS的登录用户

时间:2017-02-04 作者:bran

我有一个apache配置,其中我将IP白名单到非WordPress子文件夹,如下所示:

<Directory /var/www/html/link>
Order allow,deny
Require all granted

#Our Network
Allow from ip-address/22
Allow from ip-address/24
Allow from ip-address/24
Allow from ip-address/24
#and even longer list of IPs and other folders
</Directory>
我还想允许不属于此IP块但拥有用户帐户的人使用。有没有办法做到这一点?

3 个回复
最合适的回答,由SO网友:Fayaz 整理而成

简单(&A);快速但不是100%安全的方法:

此方法不是100%安全的,但对于大多数用户(如99%),它会起作用。

这里,基本上你将使用.htaccess 文件inside the restricted directory,说:/var/www/html/link 使用以下代码:

    SetEnvIf Cookie "^.*wordpress_logged_in.*$" WP_LOGGED_IN=maybe

    Order allow,deny
    Require all granted

    # Our Network
    #Allow from ip-address/22
    #Allow from ip-address/24
    #Allow from ip-address/24
    #Allow from ip-address/24
    # and even longer list of IPs

    # Perhaps WordPress is logged in according to cookie: works for trivial cases, but not 100% secure
    Allow from env=WP_LOGGED_IN
因此,基本上,您将在此处放置允许的IP网络,而不是apache配置.htaccess 文件(无<Directory> 块),还将使用检查WordPress登录cookieSetEnvIf 指令。如果IP不在匹配项中(&M);也找不到登录cookie,使用此代码,其余访问者将被拒绝访问。

.htaccess 根web目录的文件是必需的。

安全方法:

如果您需要100%安全的方法,那么最好使用PHP。

首先从apache配置文件中删除所有现有的IP限制(对应目录)。

然后,在中使用以下代码.htaccess 根web目录的文件。

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /

        RewriteRule ^link/access\\.php$ - [L]
        RewriteRule ^link/.*$ /link/access.php [L]

        # Your remaining rewrite rules for WordPress
    </IfModule>
然后在/var/www/html/link/access.php 文件中,请使用以下PHP代码:

<?php

    function network_match( $ip, $networks ) {
        foreach( $networks as $network ) {
            $net_parts = explode( \'/\', $network );

            $net = $net_parts[0];
            if( count( $net_parts ) === 2 ) {
                $netmask = $net_parts[1];
                if( ( ip2long( $net ) & ~ ( ( 1 << ( 32 - $netmask ) ) - 1 ) ) === ( ip2long( $ip ) & ~ ( ( 1 << ( 32 - $netmask ) ) - 1 ) ) ) {
                    return true;
                }
            }
            else if( $ip === $net ) {
                return true;
            }
        }
        return false;
    }

    $networks = array(
        // Put your allowed networks HERE
        "192.168.0.0/24",
        "127.0.0.1"
    );

    $allowed = false;
    if( network_match( $_SERVER[\'REMOTE_ADDR\'], $networks ) ) {
        $allowed = true;
    }
    else {
        // assuming WordPress is installed one directory above
        // if not, then change accordingly
        require_once dirname( dirname( __FILE__ ) ) . "/wp-load.php";
        if( is_user_logged_in() ) {
            $allowed = true;
        }
    }

    if( $allowed ) {
        // HERE you SERVE THE REQUESTED FILE WITH PHP
        echo "You are allowed";
    }
    else {
        die("Access denied!");
    }
正如现在一样,这段代码基本上在内部将所有请求重写为/var/www/html/link/ 目录收件人/var/www/html/link/access.php &文件;它检查IP访问权限;WordPress登录。

您可以修改此PHP代码(在以下行中):// HERE you SERVE THE REQUESTED FILE WITH PHP) 从link 目录

您可以查看此帖子server files from PHP CODE.

SO网友:T.Todua

据我所知。htaccess(或apache-config)具有优先级,您无法轻松覆盖它。有两种解决方案:

1) 只需使用simple-browser.php 文件(根据需要修改受保护目录的路径)

2) 不要使用apache配置,而是使用.htaccess 在该目录中,如下所示:

创造.htaccess 在该文件夹中:

<Directory "./">
Order allow,deny
Require all granted

#Our Network
Allow from ip-address/22
........
#DONT_REMOVE_ME_START
#DONT_REMOVE_ME_END
</Directory>
首先,在您的functions.php 然后,将用户发送到此链接:yoursite.com/?check_permission:

define(\'path_to_target_htaccess\',   ABSPATH.\'path/to/protected/folder/.htaccess\');

add_action(\'init\', \'start_validation\');
function start_validation(){
    if (isset($_GET[\'check_permission\'])){
        if(is_user_logged_in()){

            $new_content=  str_replace(
                \'#DONT_REMOVE_ME_START\',  
                \'#DONT_REMOVE_ME_START\'."\\r\\n".\'Allow from \'.$_SERVER[\'REMOTE_ADDR\'], 
                file_get_contents(path_to_target_htaccess)
            );
            file_put_contents(path_to_target_htaccess,  $new_content);
            header("location:  /path/to/folder"); exit;
        }
    }
}
注意:每周或每天运行一次函数,该函数将删除#DONT_REMOVE_ME_START#DONT_REMOVE_ME_END

SO网友:cjbj

我没有遇到过这种情况,对WP的了解也比Apache多,但这里大致介绍了我将如何处理它。

使用mod_setenvif 要根据包含特定代码的请求url设置环境变量,请说“wp login-!”!637#6’

  • Allow access 如果环境变量正确。这将失败Redirect "foo.html" "wp/specialpage?foo.html".redirect 将代码“wp login-”附加到原始url!637#6’
  • 现在回到步骤1,但这次将正确设置环境变量,您可以访问文件

  • 相关推荐

    如何在我的根域上组合WordPress htaccess+在子文件夹上组合php

    我为一个看似重复的问题道歉,但我所看到的几十个问题中,没有一个是同样的问题。我有以下目录结构:/.htaccess /index.php /subfolder/.htaccess /subfolder/index.php 我希望所有页面请求都由/index.php, 除非请求启动/subfolder 在这种情况下,应由/subfolder/index.php.e、 g。/abc 将重写为/subfolder/abce、 g。/subfolder/def 将重写为/subf