Django 1.11 Allauth google令牌验证和令牌验证

Django 1.11 Allauth google令牌验证和令牌验证,django,django-rest-framework,django-allauth,Django,Django Rest Framework,Django Allauth,我用allauth设置了Django rest framework(DRF),允许google身份验证作为外部身份验证,我不使用任何内置身份验证,如注册用户等。 在我的DRF中,我确实有受保护的资源,我打算使用为相应用户接收的auth_令牌来保护它们。我可以看到在点击GoogleOAuthURL后我可以登录,我可以看到Django管理后端,如下表所示 “SocialAccounts”我可以在那里看到用户,在“Social Application Token”表中,我可以看到用户和相应的authe

我用allauth设置了Django rest framework(DRF),允许google身份验证作为外部身份验证,我不使用任何内置身份验证,如注册用户等。 在我的DRF中,我确实有受保护的资源,我打算使用为相应用户接收的auth_令牌来保护它们。我可以看到在点击GoogleOAuthURL后我可以登录,我可以看到Django管理后端,如下表所示 “SocialAccounts”我可以在那里看到用户,在“Social Application Token”表中,我可以看到用户和相应的authe_令牌,以及刷新令牌和到期时间

我的用例如下

  • 在我的Django视图中,我希望使用该用户的auth_令牌来
    创建会话
  • 授权令牌到期时,它应该是自动的 刷新,以便客户端不应每次重新对其进行身份验证 自我
  • 在JWT令牌传递过程中是否可以使用Auth_令牌 具有DRF中受保护资源的标头,以便它不会被删除
    访问不需要的用户
如果有人可以指导我任何文档或用例场景将是巨大的帮助

设置.py url.py 新更新 在使用以下内容更改setting.py文件后,我可以创建会话并使用google oauth2对自己进行身份验证

设置.py

    """
    Django settings for DjangoE2ISAapi project.

    Generated by 'django-admin startproject' using Django 1.11.15.

    For more information on this file, see
    https://docs.djangoproject.com/en/1.11/topics/settings/

    For the full list of settings and their values, see
    https://docs.djangoproject.com/en/1.11/ref/settings/
    """

    import os

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = 'feufm)u(pvsvb%&_%%*)p_bpa+sv8zt$#_-do5q3(vou-j*d#p'

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    ALLOWED_HOSTS = []

    # Application definition

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.sites',
        #Django Project Apps
        'persons',
        'earningtype',
        'expensetype',
        'investmtype',
        'Earnings',
        'Expenses',
        'Shares',
        'Investment',
        'accounts',
        #Django Third party
        'rest_framework',
        'allauth',
        'allauth.account',
        'allauth.socialaccount',
        'allauth.socialaccount.providers.google',
        #'social_django',


    ]

    SOCIAL_AUTH_PIPELINE = (
        'social_auth.backends.pipeline.social.social_auth_user',
        'social_auth.backends.pipeline.social.associate_user',
        'social_auth.backends.pipeline.social.load_extra_data',
        'social_auth.backends.pipeline.user.update_user_details'
    )

    ACCOUNT_USERNAME_REQUIRED = False
    ACCOUNT_EMAIL_VERIFICATION = "none"
    SOCIALACCOUNT_QUERY_EMAIL = True
    LOGIN_REDIRECT_URL = "/api/persons"

    AUTHENTICATION_BACKENDS = (
        'social_core.backends.google.GoogleOAuth2',
        'django.contrib.auth.backends.ModelBackend',
        'social_core.backends.google.GoogleOpenId',
        'social_core.backends.google.GoogleOAuth',
        'allauth.account.auth_backends.AuthenticationBackend',
    )

    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'django.middleware.common.CommonMiddleware',
                ]


    ROOT_URLCONF = 'DjangoE2ISAapi.urls'
    SITE_ID = 1

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')]
            ,
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]

    WSGI_APPLICATION = 'DjangoE2ISAapi.wsgi.application'


    # Database
    # https://docs.djangoproject.com/en/1.11/ref/settings/#databases
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'OPTIONS': {
                'read_default_file': 'C:\\my.cnf',
            },
        }
    }


    # Password validation
    # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

    AUTH_PASSWORD_VALIDATORS = [
        {
            'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
        },
    ]


    # Internationalization
    # https://docs.djangoproject.com/en/1.11/topics/i18n/

    LANGUAGE_CODE = 'en-us'

    TIME_ZONE = 'UTC'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = True


    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.11/howto/static-files/

    STATIC_URL = '/static/'

    #File upload setting are here
    MEDIA_ROOT = os.path.join(BASE_DIR, 'upd_files')
    MEDIA_PATH = '/media'
    REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
                'rest_framework.authentication.SessionAuthentication',
                'allauth.account.auth_backends.AuthenticationBackend',
                # 'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
                #'rest_framework.authentication.BasicAuthentication',
    ),

    'DEFAULT_PERMISSION_CLASSES':(
    'rest_framework.permissions.IsAuthenticated',
    )
}
#

    from DjangoE2ISAapi.restconf.main import *
现在,我将如何在HTTP请求(GET、POST、PUT)中使用google给定的auth_令牌作为受保护api资源的授权头

我怀疑django all auth为google oauth2创建的会话是否可以用于Java脚本前端的移动应用程序。 任何使用过此场景的人都请对其进行评论

对于Django allauth使用google auth token作为用户授权头的请求,仍在进行中,没有给出相同的日期。

你解决了吗???在前端模拟Django All auth很复杂
"""DjangoE2ISAapi URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.conf.urls import url, include
from rest_framework_jwt.views import obtain_jwt_token,refresh_jwt_token,verify_jwt_token
from .restconf import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    #url(r'^',include('persons.api.urls'),name='home'),
    url(r'^api/persons/',include('persons.api.urls'),name='HOME'),
    url(r'^api/eartypes/',include('earningtype.api.urls')),
    url(r'^api/exptypes/',include('expensetype.api.urls')),
    url(r'^api/invtypes/',include('investmtype.api.urls')),
    url(r'^api/auth/jwt/',obtain_jwt_token),
    url(r'^api/auth/jwt/refresh',refresh_jwt_token),
    url(r'^api/auth/jwt/verify',verify_jwt_token),
    url(r'^new',views.index),
    url(r'^oauth/', include('social_django.urls', namespace='social')),
    url(r'^accounts/', include('allauth.urls')),
]
    """
    Django settings for DjangoE2ISAapi project.

    Generated by 'django-admin startproject' using Django 1.11.15.

    For more information on this file, see
    https://docs.djangoproject.com/en/1.11/topics/settings/

    For the full list of settings and their values, see
    https://docs.djangoproject.com/en/1.11/ref/settings/
    """

    import os

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = 'feufm)u(pvsvb%&_%%*)p_bpa+sv8zt$#_-do5q3(vou-j*d#p'

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    ALLOWED_HOSTS = []

    # Application definition

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.sites',
        #Django Project Apps
        'persons',
        'earningtype',
        'expensetype',
        'investmtype',
        'Earnings',
        'Expenses',
        'Shares',
        'Investment',
        'accounts',
        #Django Third party
        'rest_framework',
        'allauth',
        'allauth.account',
        'allauth.socialaccount',
        'allauth.socialaccount.providers.google',
        #'social_django',


    ]

    SOCIAL_AUTH_PIPELINE = (
        'social_auth.backends.pipeline.social.social_auth_user',
        'social_auth.backends.pipeline.social.associate_user',
        'social_auth.backends.pipeline.social.load_extra_data',
        'social_auth.backends.pipeline.user.update_user_details'
    )

    ACCOUNT_USERNAME_REQUIRED = False
    ACCOUNT_EMAIL_VERIFICATION = "none"
    SOCIALACCOUNT_QUERY_EMAIL = True
    LOGIN_REDIRECT_URL = "/api/persons"

    AUTHENTICATION_BACKENDS = (
        'social_core.backends.google.GoogleOAuth2',
        'django.contrib.auth.backends.ModelBackend',
        'social_core.backends.google.GoogleOpenId',
        'social_core.backends.google.GoogleOAuth',
        'allauth.account.auth_backends.AuthenticationBackend',
    )

    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'django.middleware.common.CommonMiddleware',
                ]


    ROOT_URLCONF = 'DjangoE2ISAapi.urls'
    SITE_ID = 1

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')]
            ,
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]

    WSGI_APPLICATION = 'DjangoE2ISAapi.wsgi.application'


    # Database
    # https://docs.djangoproject.com/en/1.11/ref/settings/#databases
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'OPTIONS': {
                'read_default_file': 'C:\\my.cnf',
            },
        }
    }


    # Password validation
    # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

    AUTH_PASSWORD_VALIDATORS = [
        {
            'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
        },
    ]


    # Internationalization
    # https://docs.djangoproject.com/en/1.11/topics/i18n/

    LANGUAGE_CODE = 'en-us'

    TIME_ZONE = 'UTC'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = True


    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.11/howto/static-files/

    STATIC_URL = '/static/'

    #File upload setting are here
    MEDIA_ROOT = os.path.join(BASE_DIR, 'upd_files')
    MEDIA_PATH = '/media'
    REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
                'rest_framework.authentication.SessionAuthentication',
                'allauth.account.auth_backends.AuthenticationBackend',
                # 'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
                #'rest_framework.authentication.BasicAuthentication',
    ),

    'DEFAULT_PERMISSION_CLASSES':(
    'rest_framework.permissions.IsAuthenticated',
    )
}
#

    from DjangoE2ISAapi.restconf.main import *