对传统DB定制认证后端 [英] custom authentication backend for legacy db

查看:239
本文介绍了对传统DB定制认证后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我使用遗留数据库的工作我应该用另一种模式,而不是默认的Django contrib.auth.models.User 进行身份验证。

Since I'm working with a legacy database I should use another model instead of the default Django contrib.auth.models.User for authentication.

我的模型有一个id字段( ID = models.DecimalField(...))用于输入用户名和字段密码(密码= models.CharField(...))的管理员定义了从数据库接口。

My model has an id field (id = models.DecimalField(...)) that is used for username and a field for password(password = models.CharField(...)) that the admin defines from the database interface.

我写我自己的认证后端:

I wrote my own authentication backend:

from auth.models import Owners
from django.contrib.auth.models import User

class AuthBackend(object):
    def authenticate(self, username, password):
        try:
           owner = Owners.objects.get(id=username, password=password)
           try:
               user = User.objects.get(id=owner.id)
               print user
           except User.DoesNotExist:
               user = User(id=owner.id)
               user.is_staff = False
               user.is_superuser = False
               user.set_unusable_password()
               user.save()
           return user

       except Owners.DoesNotExist:
           return None

   def get_user(self, id):
       try:
           return User.objects.get(id=id)
       except User.DoesNotExist:
           return None

我想问某事也许微不足道,但因为我现在创建用户和我利用有Django的认证模块的它自己的表等,是否也应该将它们添加到传统的DB我使用?(我现在的工作在它与synchdb一个实例我得到了它增加了身份验证表,但我应该做同样的约当我使用DB本身?什么?)
这是处理我的应用程序的现有身份验证系统,以正确的方式?

I wanted to ask sth maybe trivial, but since I now create Users and I make use of django's authentication module that has it's own tables etc, should I also add them to the legacy db I use?(now I'm working on an instance of it and with synchdb I got the auth tables added in it but what about when I use the db itself?Am I supposed to do the same?) Is this the right way of handling my app's existing authentication system?

推荐答案

这是authdb条目通常远远长于密码,并需要相应地更多的空间用于存储。

An authdb entry is usually much longer than a password, and needs correspondingly more space for storage.

$<algorithm>$<salt>$<hash>

请看Linux的的crypt(3)手册页了解详情。

这篇关于对传统DB定制认证后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆