簡體   English   中英

使用 Altorouter 和 .htpasswd 基本身份驗證

[英]Using Altorouter along with .htpasswd basic authentication

我的索引.php

require_once 'altorouter.php';
$router = new Router();

$router->map('GET', '/', function() {
    require 'home.php';
}, 'home');

$router->map('GET|POST', '/dashboard/[*:serial]?', function($serial) {
    require 'dashboard.php';
}, 'dashboard');

$match = $router->match();

if (is_array($match)) {

    header("HTTP/1.1 200 OK");
    call_user_func_array( $match['target'], $match['params'] );
        
}

else {

    header('Content-type: text/javascript');
    header("HTTP/1.1 400 Bad Request");
    echo json_encode(array('message' => 'Please check validity of methods and URL slugs.', 'debug' => ($_SERVER['REQUEST_URI'])), JSON_UNESCAPED_UNICODE, JSON_UNESCAPED_SLASHES);

}

Apache 站點可用 ssl conf 文件

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        <Directory /var/www/html/>
            #Options Indexes FollowSymLinks
            #AllowOverride All
            #Require all granted
            AuthType Basic
            AuthName "Restricted Content"
            AuthUserFile /etc/apache2/.htpasswd
            Require valid-user
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
        </IfModule>

        ServerName example.com
        Include /etc/letsencrypt/options-ssl-apache.conf
        ServerAlias www.example.com
        SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    </VirtualHost>
</IfModule>

.htaccess

RewriteEngine on

# Autorouter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

# Enable last slash in URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)/$ /$1 [R=301,L]

啟用基本身份驗證后,可以訪問 home.php(登錄后),但不能訪問 dashboard.php。

        <Directory /var/www/html/>
            AuthType Basic
            AuthName "Restricted Content"
            AuthUserFile /etc/apache2/.htpasswd
            Require valid-user
        </Directory>

https://www.example.com/dashboard/abcd123/返回 404

Not Found

The requested URL was not found on this server.

禁用基本身份驗證后,一切正常。

        <Directory /var/www/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

https://www.example.com/dashboard/abcd123/返回 200 和頁面。

有沒有辦法將 Altorouter 與 Basic Auth 結合起來? 為什么'/'路由不受認證影響,而其他路由受認證影響?

顯然, AllowOverride All需要啟用。 現在路由工作了。

暫無
暫無

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

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