【问题标题】:Django allauth not sending links with httpsDjango allauth 不发送带有 https 的链接
【发布时间】:2014-11-07 14:17:38
【问题描述】:

我希望 Django Allauth 使用https 发送确认电子邮件或重置密码之类的链接:

类似这样的:

https://example.com/ca/accounts/confirm-email/picuwfpjpptjswi50x5zb4gtsqptmwkscea445kadnbsfwcyij3fdnblery4onuq/

根据the official documentation 仅更改settings.py 中的以下设置应该可以工作:

ACCOUNT_DEFAULT_HTTP_PROTOCOL = "https"

但我不断获得http 而不是https 的链接,如下所示:

http://example.com/ca/accounts/confirm-email/picuwfpjpptjswi50x5zb4gtsqptmwkscea445kadnbsfwcyij3fdnblery4onuq/

我错过了什么吗?谢谢!

【问题讨论】:

    标签: django https django-allauth


    【解决方案1】:

    稍微深入代码,可以看到allauth 使用Django 内置的build_absolute_uri 方法设置了activate_url 模板上下文变量:

    https://github.com/pennersr/django-allauth/blob/master/allauth/account/models.py#L119

    ...
    activate_url = reverse("account_confirm_email", args=[self.key])
    activate_url = request.build_absolute_uri(activate_url)
    ctx = {
    "activate_url": activate_url,
    ...
    }
    

    查看build_absolute_uri 的代码可以看到它需要一个环境变量:

    https://github.com/django/django/blob/master/django/http/request.py#L153

    def _get_scheme(self):
        return 'https' if os.environ.get("HTTPS") == "on" else 'http'
    

    要在此函数生成的 URL 中返回 https://,您需要设置一个 HTTPS 环境变量。

    这取决于你如何设置你的项目,但你可以在你的settings.pymanage.pyset the environment variable

    以下是关于 SSL 的一般 Django 安全性的好帖子:

    编辑

    奇怪的是,重置密码模板使用了不同的方法来构建 URL:

    https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py#L428

    url = '%s://%s%s' % (app_settings.DEFAULT_HTTP_PROTOCOL,
        current_site.domain,
        path)
    context = {"site": current_site,
        "user": user,
        "password_reset_url": url}
    

    改用DEFAULT_HTTP_PROTOCOL 设置

    【讨论】:

    • 使用您的解决方案,现在密码重置电子邮件使用 https,但确认电子邮件地址仍然使用 http。知道为什么吗?
    • 查看更新。对于密码重置模板,似乎使用了另一种构建 URL 的方式。您确定变量设置正确吗?
    • 确认电子邮件地址消息仍然无效。我首先尝试在 settings.py 中设置变量(import os os.environ['HTTPS'] = "on"),然后在终端中手动设置(HTTPS="on"; export HTTPS),但我仍然得到确认带有 http 的电子邮件地址消息。
    • 感谢您的帮助,我设法解决了这个问题!我还报告了这个错误,它将在下一个 django-allauth 版本中解决:github.com/pennersr/django-allauth/issues/717 谢谢! :)
    【解决方案2】:

    除了设置“HTTPS”环境变量和 SECURE_PROXY_SSL_HEADER SECURE_SSL_REDIRECT,当在adapter.py render_mail() 中使用.txt 正文时,在渲染模板和使用EmailMultiAlternatives() 发送邮件时似乎也可能出现问题 [1]:https://github.com/pennersr/django-allauth/blob/master/allauth/account/adapter.py

    for ext in ["html", "txt"]:
            try:
                template_name = "{0}_message.{1}".format(template_prefix, ext)
                bodies[ext] = render_to_string(
                    template_name,
                    context,
                    self.request,
                ).strip()
            except TemplateDoesNotExist:
                if ext == "txt" and not bodies:
                    # We need at least one body
                    raise
        if "txt" in bodies:
            msg = EmailMultiAlternatives(subject, bodies["txt"], from_email, to)
            if "html" in bodies:
                msg.attach_alternative(bodies["html"], "text/html")
        else:
            msg = EmailMessage(subject, bodies["html"], from_email, to)
            msg.content_subtype = "html"  # Main content is now text/html
        return msg
    

    例如 print(bodies[ext]) 给出:

      "To confirm this is correct, go to " https://127.0.0.1:8000/accounts/confirm-email/MjI:1kS0Mj:M5YfUf9-1Vg_TlgjVrK6vAtaLDE/ "
    

    但在电子邮件上仍然是 http://

     http://url7514.sitename/ls/click?upn=HJL2SSWV...
    

    对于大多数设备,这也有效,因为仍应重定向到 https://,但在某些设备上没有,因此必须将默认模板/帐户/电子邮件/email_confirmation_message.txt 更改为 html 扩展名, 结果之后:

    To confirm this is correct, go to https://sitename/accounts/confirm-email/M...

    【讨论】:

      【解决方案3】:

      如果您在代理后面使用 django,则必须将 https 标头发送到 django。 在 apache2 中对我有用的是把它放在 apache2 上

      <VirtualHost *:443>
        RequestHeader set X-Forwarded-Proto 'https' env=HTTPS
      

      添加headers mod后:

      a2enmod headers
      

      这在 django setting.py 上:

      USE_X_FORWARDED_HOST = True
      SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
      

      我的所有 build_absolute_uri 都以 https 开头,模板也是如此,包括密码恢复和身份验证邮件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-08
        • 1970-01-01
        • 1970-01-01
        • 2011-12-01
        • 2016-04-14
        • 1970-01-01
        • 1970-01-01
        • 2020-10-25
        相关资源
        最近更新 更多