Django的Python Social Auth引发Authforbidden异常
作者:互联网
我正在将Django 1.10与python 2.7和social-auth-app-django(1.2.0)一起使用.它是Python Social Auth库的一部分.
我希望仅将登录限制为我公司的域ID,因此我从库中使用了此设置.
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS=['mycompany.in']
现在,如果您尝试按预期使用任何其他域登录,则会引发错误.
我的目标是捕获此异常并向用户显示自定义页面.但是对于我的一生,我无法做到.
如果我将Debug设置为False,它将用户重定向到我的
LOGIN_ERROR_URL='/'
页面,但无法将我的自定义消息传递给用户
这是我的setting.py的一部分
调试=假
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',
'social_django.middleware.SocialAuthExceptionMiddleware',
]
#social auth
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY= "9******6.apps.googleusercontent.com"
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET="W*****x"
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS=['mycompany.in']
LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = 'upload_file'
LOGOUT_URL = 'logout'
LOGIN_ERROR_URL='logout
在我看来,我有这段代码可以处理异常
from social_django.middleware import SocialAuthExceptionMiddleware
from social_core.exceptions import AuthForbidden
class SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
def process_exception(self, request, exception):
print "exception occured"
if hasattr(social_exceptions, 'AuthForbidden'):
print "hello two"
return HttpResponse("I'm a exception %s" % exception)
else:
print "other exception"
raise exception
我什至尝试过
def process_template_response(self):
print "response"'
def get_redirect_uri(request, exception):
print "URL"
但无济于事.
我关注了这些链接
python-social-auth AuthCanceled exception
和
这是debug设置为False时的输出:
“AuthForbidden at /app/oauth/complete/google-oauth2/
Your credentials aren’t allowed”
解决方法:
>需要在settings.py中为SOCIAL_AUTH_LOGIN_ERROR_URL设置值
>扩展SocialAuthExceptionMiddleware类&需要覆盖“ get_message”方法
>处理错误URL将消息显示给用户.
例如
中间件
from social_django.middleware import SocialAuthExceptionMiddleware
class CustomSocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
def get_message(self, request, exception):
default_msg = super(CustomSocialAuthExceptionMiddleware).get_message(request, exception) # in case of display default message
return "Custom messages text write here."
settings.py
SOCIAL_AUTH_LOGIN_ERROR_URL = '/error_page/'
MIDDLEWARE = [
'...',
'path.to.custom.middleware.CustomSocialAuthExceptionMiddleware',
]
URL.py
from django.conf.urls import url
from views import ErrorPage
urlpatterns = [
url(r'^error_page/$', ErrorPage.as_view(), name="error-page"),
]
view.py
from django.views.generic.base import TemplateView
class ErrorPage(TemplateView):
template_name = 'error.html'
error.html(模板)
....
<body>
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }} </li>
{% endfor %}
</ul>
{% endif %}
</body>
....
如果您正在使用django消息框架.在不使用django消息框架的情况下,中间件将消息添加到GET参数中,该参数可以显示在错误页面上.
标签:authentication,google-login,social,python,django 来源: https://codeday.me/bug/20191025/1932510.html