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

2023-12-27

我正在将 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 https://github.com/Bouke/django-two-factor-auth/issues/219,这是由于管理员用户没有设置 2FA 造成的。

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

EDIT:

我删除了网站的 2FA 登录要求,然后添加了电话设备。没有运气。


我最近遇到了这种情况,并根据那里的评论创建了这个解决方案:

https://github.com/Bouke/django-two-factor-auth/issues/219#issuecomment-494382380 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)

Then in urls.py:

from django.contrib import admin
admin.site.__class__ = AdminSiteOTPRequiredMixinRedirSetup
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

django-two-factor-auth 无法访问管理站点 的相关文章

随机推荐