0

我在 Linode 上使用以下 SSLMiddleware 已经有一段时间了,我的 SSL 在这方面工作得很好,现在我已经将服务器更改为 Webfaction,突然之间,我的 HTTPS 页面无法正常工作,因为它被重定向到https页面正确,但是我所有的css文件,css文件中的图像(没有绝对url),javascript都变成了不安全的来源(指的是http://而不是https://),我现在真的很困惑因为我不知道它是否与 SSLMiddleware 或其他东西有关,所以除了数据库参数值之外,我没有更改 settings.py 中的任何内容。请帮忙。提前致谢。

__license__ = "Python"
__copyright__ = "Copyright (C) 2007, Stephen Zabel"
__author__ = "Stephen Zabel - sjzabel@gmail.com"
__contributors__ = "Jay Parlar - parlar@gmail.com"

from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect, get_host

SSL = 'SSL'

class SSLRedirect:

    def process_view(self, request, view_func, view_args, view_kwargs):
        if SSL in view_kwargs:
            secure = view_kwargs[SSL]
            del view_kwargs[SSL]
        else:
            secure = False

        if settings.ENABLE_SSL:
                if not secure == self._is_secure(request):
                    return self._redirect(request, secure)
        else:
            return

    def _is_secure(self, request):
        if request.is_secure():
            return True

        #Handle the Webfaction case until this gets resolved in the request.is_secure()
        if 'HTTP_X_FORWARDED_SSL' in request.META:
            return request.META['HTTP_X_FORWARDED_SSL'] == 'on'

        return False

    def _redirect(self, request, secure):
        protocol = secure and "https" or "http"
        newurl = "%s://%s%s" % (protocol,get_host(request),request.get_full_path())
        if settings.DEBUG and request.method == 'POST':
            raise RuntimeError, \
        """Django can't perform a SSL redirect while maintaining POST data.
           Please structure your views so that redirects only occur during GETs."""

        return HttpResponsePermanentRedirect(newurl)
4

1 回答 1

0

我最近在 WebFaction 上实现了 SSL,没有任何自定义中间件修补,这是一个非常简单的过程。

看看这里:http: //community.webfaction.com/questions/512/how-do-i-set-up-a-https-ssl-django-site

如果这没有帮助,请向他们开一张票。他们通常非常擅长快速解决问题。

于 2011-12-13T19:34:15.727 回答