【发布时间】:2012-03-13 01:52:46
【问题描述】:
标题几乎概括了我的问题。我想用密码保护我的 django 应用程序中存在于 heroku 上的一些文件。
如果我不能使用 htaccess,是否有人对我还可以使用什么提出建议?
谢谢。
【问题讨论】:
-
我宁愿不使用任何特定于 django 的东西,因为我有一个测试、生产和演示服务器要处理
-
为什么不使用
SITE_ID和站点框架来区分?
标题几乎概括了我的问题。我想用密码保护我的 django 应用程序中存在于 heroku 上的一些文件。
如果我不能使用 htaccess,是否有人对我还可以使用什么提出建议?
谢谢。
【问题讨论】:
SITE_ID 和站点框架来区分?
正如@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()
那你需要在settings.py中定义:
BASIC_WWW_AUTHENTICATION_USERNAME = "your user"
BASIC_WWW_AUTHENTICATION_PASSWORD = "your pass"
BASIC_WWW_AUTHENTICATION = True
【讨论】:
TypeError: object() takes no parameters,您可能使用的是Django 1.10 或更高版本,需要添加django.utils.deprecation.MiddlewareMixin 或升级旧式中间件。请参阅:docs.djangoproject.com/en/1.10/topics/http/middleware/…。
'str' object has no attribute 'decode'̀. They'll need to import the base64` 模块并将auth.strip().decode('base64') 更改为base64.b64decode(b).decode('utf-8')。
我可以在 heroku 上使用 .htaccess 文件和雪松堆栈。
Procfile需要为web节点指定一个脚本:
web: sh www/conf/web-boot.sh
conf/web-boot.sh 滑流包含 apache 配置文件,例如:
conf/httpd/default.conf 然后可以允许覆盖,正如您从 apache 中知道的那样。然后您可以只使用.htaccess 文件。整个过程在我的博文PHP on Heroku again中有详细记录,其中一部分是关于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
我希望这会有所帮助。我将它用于 .htaccess 和更改 webroot。
【讨论】:
您不能使用 .htaccess,因为 Heroku 应用程序不使用 Apache。不过,您可以使用Django authentication。
或者您可以使用 Apache 从另一台正在的服务器上提供文件。
【讨论】: