gpt4 book ai didi

django-two-factor-auth 无法访问管理站点

转载 作者:行者123 更新时间:2023-12-04 01:59:04 25 4
gpt4 key购买 nike

我正在为 webapp 使用 django-two-factor-auth。我无法访问管理页面。

我知道我正在输入正确的凭据。当我输入不正确的凭据时,我会收到一条相应的错误消息。

当我输入正确的凭据时,页面只是使用以下 URL 重新加载:

http://localhost:8080/account/login/?next=/inveskore/

这些是我与 two_factor 相关的设置:
LOGIN_URL = 'two_factor:login'
LOGIN_REDIRECT_URL = '/inveskore'
TWO_FACTOR_SMS_GATEWAY = 'two_factor.gateways.twilio.gateway.Twilio'

这是关联的 URL 路径:
path('admin/', admin.site.urls),

根据 this ,这是因为管理员用户没有设置 2FA。

那么,如果您无法访问该站点,您如何为管理员用户设置 2FA?

编辑:

我取消了该站点的 2FA 登录要求,然后添加了电话设备。没运气。

最佳答案

我最近遇到了这种情况,并根据那里的评论创建了这个解决方案:
https://github.com/Bouke/django-two-factor-auth/issues/219#issuecomment-494382380
我子类化 AdminSiteOTPRequired然后将其指定为要使用的管理类

from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.views import redirect_to_login
from django.http import HttpResponseRedirect
from django.shortcuts import resolve_url
from django.urls import reverse
from django.utils.http import is_safe_url
from two_factor.admin import AdminSiteOTPRequired, AdminSiteOTPRequiredMixin


class AdminSiteOTPRequiredMixinRedirSetup(AdminSiteOTPRequired):
def login(self, request, extra_context=None):
redirect_to = request.POST.get(
REDIRECT_FIELD_NAME, request.GET.get(REDIRECT_FIELD_NAME)
)
# For users not yet verified the AdminSiteOTPRequired.has_permission
# will fail. So use the standard admin has_permission check:
# (is_active and is_staff) and then check for verification.
# Go to index if they pass, otherwise make them setup OTP device.
if request.method == "GET" and super(
AdminSiteOTPRequiredMixin, self
).has_permission(request):
# Already logged-in and verified by OTP
if request.user.is_verified():
# User has permission
index_path = reverse("admin:index", current_app=self.name)
else:
# User has permission but no OTP set:
index_path = reverse("two_factor:setup", current_app=self.name)
return HttpResponseRedirect(index_path)

if not redirect_to or not is_safe_url(
url=redirect_to, allowed_hosts=[request.get_host()]
):
redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

return redirect_to_login(redirect_to)
然后在 urls.py :
from django.contrib import admin
admin.site.__class__ = AdminSiteOTPRequiredMixinRedirSetup

关于django-two-factor-auth 无法访问管理站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48600737/

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