gpt4 book ai didi

Django allauth不使用https发送链接

转载 作者:行者123 更新时间:2023-12-05 00:24:39 25 4
gpt4 key购买 nike

我希望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/
我想念什么吗?谢谢!

最佳答案

深入研究一下代码,您可以看到allauth使用Django的build in activate_url方法设置了build_absolute_uri模板上下文变量:

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.py中添加 set the environment variable

以下是有关SSL的一般Django安全性的好文章:
  • https://security.stackexchange.com/questions/8964/trying-to-make-a-django-based-site-use-https-only-not-sure-if-its-secure

  • 编辑

    奇怪的是,重置密码模板使用另一种方法来构造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设置

    关于Django allauth不使用https发送链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824598/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com