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

标签 django security two-factor-authentication

我正在为 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/

相关文章:

javascript - 将 Duo Web 2 因素身份验证与 ColdFusion 集成

python - 具有授权用户的基于 Django View 的缓存

python - Django - UnboundLocalError

python - 如何使用 Django Rest Framework 清除图像?

asp.net - 我应该采取什么措施来保护我的多层 ASP.NET 应用程序?

security - 根据 Reporting Services 中的参数控制报表权限

java - 使用双因素身份验证的 JSCH SFTP 登录

python - Django 二进制或 BLOB 模型字段

javascript - 使用 CryptoJS 在 JavaScript 中进行 AES 加密/解密

two-factor-authentication - 如何从链接打开谷歌身份验证器?