首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django-带有VueJS的租户如何使用auth令牌登录(端点)

Django-带有VueJS的租户如何使用auth令牌登录(端点)
EN

Stack Overflow用户
提问于 2021-05-07 23:24:20
回答 2查看 268关注 0票数 0

我正在使用Django Rest Framework /django进行一个小型项目--我想知道登录获得令牌的终点是什么,似乎django-租户中间件阻止了我的请求或类似于我的代码:

我安装的应用程序:

代码语言:javascript
复制
'django_tenants',
'rest_framework',
'rest_framework.authtoken',

'allauth',
'allauth.account',
'allauth.socialaccount',

'rest_auth',
'rest_auth.registration',

我的网址:

代码语言:javascript
复制
path('api-auth/', include('rest_framework.urls')),
path('api/rest-auth/', include('rest_auth.urls')),

当我向:http://localhost:5555/api-auth/login/发送post请求时,我收到一条错误消息: 404 Not -没有找到主机名"localhost“的租户

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-08 16:36:17

经过两天的研究,并基于这个答案:django中的URLCONF -租户模式

通过修复main.py中间件,我自己找到了解决方案

确切地说,除了:

代码语言:javascript
复制
def process_request(self, request):
        # Connection needs first to be at the public schema, as this is where
        # the tenant metadata is stored.
        connection.set_schema_to_public()
        hostname = self.hostname_from_request(request)

        domain_model = get_tenant_domain_model()
        try:
            tenant = self.get_tenant(domain_model, hostname)
        except domain_model.DoesNotExist:
            request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
            request.public_tenant = True
            return
            #raise self.TENANT_NOT_FOUND_EXCEPTION('No tenant for hostname "%s"' % hostname)

所以现在我使用:PUBLIC_SCHEMA_URLCONF = 'myproject.urls_public'

我的urls_public.py

代码语言:javascript
复制
from django.conf.urls import include, url
from django.urls import path
#from tenant_tutorial.views import HomeView
from django.contrib import admin

urlpatterns = [
    path('api-auth/', include('rest_framework.urls')),
    path('api/rest-auth/', include('rest_auth.urls')),
]
票数 1
EN

Stack Overflow用户

发布于 2021-05-08 02:41:18

Django租户试图从URL中提取租户。当租户注册时,应该给他们一个子域名。因此,在您的示例中,向localhost发送api请求将失败,因为它不会对任何租户进行评估。文档

租户通过其主机名(即tenant.domain.com)进行标识。此信息存储在公共架构上的表中。每当发出请求时,主机名都用于匹配数据库中的租户。如果匹配,则更新搜索路径以使用此租户的模式。因此,从现在开始,所有查询都将在租户的模式中进行。例如,假设您在http://customer.example.com有一个租户客户。在customer.example.com收到的任何请求都将自动使用客户的模式,并在请求时使租户可用。如果找不到租户,则会引发404错误。这也意味着您的主域应该有一个租户,通常使用公共模式。有关更多信息,请阅读安装部分。

必须首先在公共模式中创建包含详细信息的租户,然后将其映射到localhost。然后为正在注册的租户添加模式,这些模式将映射到tenant1.localhosttenant2.localhost等。

代码语言:javascript
复制
from customers.models import Client, Domain


# create your public tenant
tenant = Client(schema_name='public',
                name='Schemas Inc.',
                paid_until='2022-12-05',
                on_trial=False)
tenant.save()

# Add one or more domains for the tenant
domain = Domain()
domain.domain = 'my-domain.com' # don't add your port or www here! on a local server you'll want to use localhost here
domain.tenant = tenant
domain.is_primary = True
domain.save()

# create your real tenant

tenant = Client(schema_name='tenant1',
                name='My First Tenant',
                paid_until='2021-12-05',
                on_trial=True)
tenant.save()

# Add one or more domains for the tenant
domain = Domain()
domain.domain = 'tenant1.my-domain.com' # tenant.localhost while in development
domain.tenant = tenant
domain.is_primary = True
domain.save()

所以您应该向tenant.localhost:5555/api-auth/login/发送请求

此外,您还需要将tenant.localhost添加到主机文件中,以解析回送地址。或者使用像dnsmasq这样的工具将域名解析为回送地址。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67442687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档