Django integrates third-party login (social-django)

Third-party development platform URL

Third-party login needs to go to the official open platform to apply and obtain the necessary parameters. The following are the development platform URLs of each official website:
qq: https://connect.qq.com/
weixin: https://open.weixin.qq. com/
weibo: https://open.weibo.com/
You need to create an application to obtain relevant values

Use social_django to achieve third-party login

We use social_django third-party library to implement third-party login

github address: https://github.com/python-social-auth/social-app-django

Which third platforms are supported

insert image description here

Download the third package:

pip install social-auth-app-django

Configured in INSTALL_APP

'social_django',

generate table:

Just do migrate, because the migration file has already been generated, we can see it in the source code

 python manage.py migrate

You can see that there are five more tables in the database

insert image description here

Modify the settings file

Add to AUTHENTICATION_BACKENDS

insert image description here

AUTHENTICATION_BACKEND=(
    #自定义用户认证
    'apps.user.views.CustomBackend',
    
    'django.contrib,auth.backends.ModelBackend',
    'social_core.backends.weibo.WeiboOAuth2',
    'social_core.backends.qq.QQOAuth2',
    'social_core.backends.weixin.WeixinOAuth2',
)

Add TEMPLATES in settings:

  'social_django.context_processors.backends',
  'social_django.context_processors.login_redirect',

insert image description here

configure url

Note: You need to add $ after the login address of your own website, otherwise it will conflict with third-party login

# 第三方登录
path('', include('social_django.urls', namespace='social')),
# jwt认证接口,自己网站登陆访问的地址
url('^login/$', obtain_jwt_token),

The third-party login address can be seen in the source code: whatever third-party backend you choose will change
insert image description here

APP Secret and App key configuration, inside settings

# 第三方登录,里面的值是你的开放平台对应的值
SOCIAL_AUTH_WEIBO_KEY = 'xxxxxxx'
SOCIAL_AUTH_WEIBO_SECRET = 'xxxxxx'

SOCIAL_AUTH_QQ_KEY = 'xxxxxxx'
SOCIAL_AUTH_QQ_SECRET = 'xxxxxxx'

SOCIAL_AUTH_WEIXIN_KEY = 'xxxxxxx'
SOCIAL_AUTH_WEIXIN_SECRET = 'xxxxxxx'


#登录成功后跳转到首页
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/index'

In this way, I successfully logged in, but because it is a front-end and back-end separation project, the status is not maintained.

implement state preservation

Modify the source code:
After logging in successfully, you will jump to the home page, and find that you are still not logged in. We need to modify the source code

Find this file social_core/actions.py to modify

original code

return backend.strategy.redirect(url)

insert image description here
change into

# 修改源码适配drf
    response = backend.strategy.redirect(url)
    #获取用户
    payload = jwt_payload_handler(user)
    # 设置cookie
    response.set_cookie("name",user.name if user.name else user.username, max_age=24*3600)
    response.set_cookie("token", jwt_encode_handler(payload), max_age=24*3600)
    
    return response

insert image description here

This is a successful login

Guess you like

Origin blog.csdn.net/qq_48082548/article/details/126637397