簡體   English   中英

SOCIAL_AUTH_LOGIN_ERROR_URL不起作用Django 2.0.6

[英]SOCIAL_AUTH_LOGIN_ERROR_URL not working Django 2.0.6

我正在為我的組織使用網絡應用程序(讓我們說“ students.uni”和“ academics.uni”)。我的組織使用Google服務,因此它們是Google域,我想將訪問權限僅限於使用'social_django'我正在使用Django 2.0.6和Python 3.6.5

我正在使用它來限制對應用程序的訪問權限,效果很好:

SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['student.uni', 'academics.uni']

當我登錄或使用允許的帳戶注冊時,效果很好,但是,當不允許使用該帳戶的域時,我想重定向或將用戶發送到另一個頁面,讓他知道發生了什么。

我一直在嘗試:

SOCIAL_AUTH_LOGIN_ERROR_URL = 'principal'

但是我無法使它正常工作,因為如果設置DEBUG = False我會不斷收到AuthForbidden異常或500錯誤

關於不允許該域時如何進行重定向或發送消息的任何建議? 非常感謝

一些代碼:

Settings.py(不是全部,但是相關的代碼)

    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'apps.user',
    'apps.inv',
    'apps.sol',
    'social_django',
]



AUTHENTICATION_BACKENDS = (
 'social_core.backends.open_id.OpenIdAuth',  # for Google authentication
 'social_core.backends.google.GoogleOpenId',  # for Google authentication
 'social_core.backends.google.GoogleOAuth2',  # for Google authentication

 'django.contrib.auth.backends.ModelBackend',
)

LOGIN_URL =  'login'
LOGIN_REDIRECT_URL = 'principal'
#DEBUG = False
SOCIAL_AUTH_LOGIN_ERROR_URL = 'principal'

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY ='****.apps.googleusercontent.com'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = '******'
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['student.uni', 'academics.uni']

嘗試使用另一個域帳戶登錄時得到的頁面:

AuthForbidden at /auth/complete/google-oauth2/
Your credentials aren't allowed
Request Method: GET
Request URL:    http://127.0.0.1:8000/auth/complete/google-oauth2/?state=8*********&code=4/***********-***********************************&authuser=0&session_state=*****************************..e40c&prompt=none
Django Version: 2.0.6
Exception Type: AuthForbidden
Exception Value:    
Your credentials aren't allowed
Exception Location: C:\ambientes\prueba\lib\site-packages\social_core\pipeline\social_auth.py in auth_allowed, line 14
Python Executable:  C:\ambientes\prueba\Scripts\python.exe
Python Version: 3.6.5
Python Path:    
['C:\\proyectosDjango\\Modular',
 'C:\\ambientes\\prueba\\Scripts\\python36.zip',
 'C:\\Users\\espar\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
 'C:\\Users\\espar\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
 'C:\\Users\\espar\\AppData\\Local\\Programs\\Python\\Python36-32',
 'C:\\ambientes\\prueba',
 'C:\\ambientes\\prueba\\lib\\site-packages']
Server time:    Dom, 1 Jul 2018 15:45:32 -0500

經過(很多?)研究,我找到了一種方法。 繼續:我只希望來自域列表(由Google提供)的電子郵件能夠在我的網站上注冊:example1 @ domain1.com,example2 @ another.domain.com,您可以在此處此處找到使用google登錄的基本步驟完成這些步驟后,無論其域是什么,您都可以使用任何google,facebook,twitter帳戶登錄。 下一步是在settings.py中定義下一個變量

SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['domain1.edu.com', 'domain2.edu.com']

這將過濾您希望能夠注冊的域。

您也可以設置下一個,盡管這些與我現在無關,我認為擁有它們會更好

SOCIAL_AUTH_LOGIN_ERROR_URL = '/user/error/'
SOCIAL_AUTH_RAISE_EXCEPTIONS = False

錯誤網址定義了登錄時發生錯誤時應用程序應重定向到的路徑

我的解決方案從這里開始:當系統檢測到您的域允許時,一切都會正常進行,但是當它檢測到您的域不在白名單中時,它會拋出AuthForbidden異常(是的,即使我定義了它不應該引發上面的變量除外)。 由於異常不是錯誤,因此不會轉到SOCIAL_AUTH_LOGIN_ERROR_URL,您將獲得異常屏幕。

解決方案是創建自定義管道方法。 您可以在這里找到管道的介紹

1-在與settings.py相同的文件夾中創建pipeline.py文件。2-定義方法auth_allowed,這樣它不會引發異常,而是重定向到其他地方(或執行您想做的任何事情)。 我這樣做是這樣的:

from django.shortcuts import redirect

def auth_allowed(backend, details, response, *args, **kwargs):
    if not backend.auth_allowed(response, details):
        return redirect('user:error')#<-here goes your url as defined on your urls.py

3-在settings.py中定義管道,用您的自定義方法替換auth_allowed

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'modular.pipeline.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.user.create_user',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details',
)

(模塊化是我的proyect的名字,寫上您的proyect的名字)(許多帖子將向您展示僅由“ social.pipeline ...”調用的方法,而我卻使用了“ social_core.pipeline ...”,因為社交給了我一個錯誤)

這應該工作。 希望它可以幫助某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM