为了简化开发流程,使用了django 的用户管理机制。项目中需要实现sso单点登录,保障系统能够整合到原有旧平台中。
(1)在app中添加authbackend.py 。 yourapp/auth_backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
class PasswordlessAuthBackend(ModelBackend):
"""Log in to Django without providing a password.
"""
def authenticate( self , username = None ):
try :
return User.objects.get(username = username)
except User.DoesNotExist:
return None
def get_user( self , user_id):
try :
return User.objects.get(pk = user_id)
except User.DoesNotExist:
return None
|
(2)设置setting.py
1
2
3
4
|
AUTHENTICATION_BACKENDS = (
# ... your other backends
‘yourapp.authbackend.PasswordlessAuthBackend‘ ,
) |
(3)在自己的view中可以直接使用
1
2
|
user = authenticate(username = user.username)
login(request, user) |