用于django app的heroku上的htaccess

mea*_*a36 11 django .htaccess heroku

标题几乎总结了我的问题.我想密码保护我生活在heroku上的django应用程序中的一些文件.
如果我不能使用htaccess,是否有人建议我还能使用什么?谢谢.

And*_*uín 16

正如@mipadi所说,你不能在Heroku上使用htaccess,但你可以为此创建一个中间件:

from django.conf import settings
from django.http import HttpResponse
from django.utils.translation import ugettext as _


def basic_challenge(realm=None):
    if realm is None:
        realm = getattr(settings, 'WWW_AUTHENTICATION_REALM', _('Restricted Access'))
    # TODO: Make a nice template for a 401 message?
    response =  HttpResponse(_('Authorization Required'), mimetype="text/plain")
    response['WWW-Authenticate'] = 'Basic realm="%s"' % (realm)
    response.status_code = 401
    return response

def basic_authenticate(authentication):
    # Taken from paste.auth
    (authmeth, auth) = authentication.split(' ',1)
    if 'basic' != authmeth.lower():
        return None
    auth = auth.strip().decode('base64')
    username, password = auth.split(':',1)
    AUTHENTICATION_USERNAME = getattr(settings, 'BASIC_WWW_AUTHENTICATION_USERNAME')
    AUTHENTICATION_PASSWORD = getattr(settings, 'BASIC_WWW_AUTHENTICATION_PASSWORD')
    return username == AUTHENTICATION_USERNAME and password == AUTHENTICATION_PASSWORD

class BasicAuthenticationMiddleware(object):
    def process_request(self, request):
        if not getattr(settings, 'BASIC_WWW_AUTHENTICATION', False):
            return
        if 'HTTP_AUTHORIZATION' not in request.META:
            return basic_challenge()
        authenticated = basic_authenticate(request.META['HTTP_AUTHORIZATION'])
        if authenticated:
            return
        return basic_challenge()
Run Code Online (Sandbox Code Playgroud)

然后你需要定义settings.py:

BASIC_WWW_AUTHENTICATION_USERNAME = "your user"
BASIC_WWW_AUTHENTICATION_PASSWORD = "your pass"
BASIC_WWW_AUTHENTICATION = True
Run Code Online (Sandbox Code Playgroud)

  • 我实际上是通过使用request.META ['PATH_INFO']来检查url并返回,如果我不希望该url密码保护 (2认同)
  • 如果你在使用这个例子时收到错误`TypeError:object()没有参数`,你可能正在使用Django 1.10或更高版本,它需要添加`django.utils.deprecation.MiddlewareMixin`或旧式中间件升级.请参阅:https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-middleware. (2认同)

hak*_*kre 8

我能够使用.htaccess雪松堆栈在heroku上使用文件.

  1. Procfile 需要为Web节点指定脚本:

    web:    sh www/conf/web-boot.sh
    
    Run Code Online (Sandbox Code Playgroud)
  2. conf/web-boot.sh滑流中包括Apache配置文件,例如:

  3. conf/httpd/default.conf则可以让覆盖,当你从Apache的知道这一点.

然后,您可以只使用.htaccess文件.整个过程在我的博客文章中详细记录在Heroku上,其中一部分是关于apache配置的.2.包括你自己的httpd配置的步骤基本上是:

sed -i 's/Listen 80/Listen '$PORT'/' /app/apache/conf/httpd.conf
sed -i 's/^DocumentRoot/# DocumentRoot/' /app/apache/conf/httpd.conf
sed -i 's/^ServerLimit 1/ServerLimit 8/' /app/apache/conf/httpd.conf
sed -i 's/^MaxClients 1/MaxClients 8/' /app/apache/conf/httpd.conf

for var in `env|cut -f1 -d=`; do
  echo "PassEnv $var" >> /app/apache/conf/httpd.conf;
done
echo "Include /app/www/conf/httpd/*.conf" >> /app/apache/conf/httpd.conf
touch /app/apache/logs/error_log
touch /app/apache/logs/access_log
tail -F /app/apache/logs/error_log &
tail -F /app/apache/logs/access_log &
export LD_LIBRARY_PATH=/app/php/ext
export PHP_INI_SCAN_DIR=/app/www
echo "Launching apache"
exec /app/apache/bin/httpd -DNO_DETACH
Run Code Online (Sandbox Code Playgroud)

我希望这是有帮助的.我用它来.htaccess和更改webroot.