我有一个小的django应用程序,它允许显示与用户相关的一些信息。这个信息存储在sqlite中,我很好。 用户的登录名和密码存储在另一个oracle数据库中,我的问题是:如何使用存储在oracle db中的creditinals来授权用户?我想,直接访问db(select)会没问题,因为不能更改oracle db中的数据。还有另外一个问题:如何存储django超级用户数据? 提前谢谢。
答案 0 :(得分:0)
有关如何在此处使用oracle数据库的信息:https://docs.djangoproject.com/en/1.8/ref/databases/#oracle-notes
另外,这里有一个关于如何使用自定义身份验证后端的好例子:
https://djangosnippets.org/snippets/1678/
from django.contrib.auth.models import User
from django.conf import settings
import cx_Oracle
# set in config or here depending on your taste
try:
ORACLE_CONNECT = settings.ORACLE_CONNECT
except:
ORACLE_CONNECT = None
# when using runserver I turn debugging on, you can
# set to false of remove.
try:
DEBUG = settings.DEBUG
except:
DEBUG = False
class OracleAuthBackend:
"""
This class is used to authenticate against an Oracle database and
adds a user if authentication is successful. It uppercases the
username since oracle usernames are not currently case sensitive.
Additionally I append _ORACLE to the username to help make it
easier to identify what auth source was used and to avoid conflicts
with ldap authenticated sessions.
"""
def authenticate(self, username=None, password=None):
if DEBUG:
print "Attempting to log in as %s" % (username)
if ORACLE_CONNECT == None:
constr = '%s/%s' % (username, password)
else:
constr = '%s/%s@%s' % (username, password, ORACLE_CONNECT)
try:
auth_con = cx_Oracle.connect(constr.encode('ascii','ignore'))
auth_con.close()
except Exception, e:
if DEBUG:
print e
return None
oracle_user = username.upper() + '_ORACLE'
try:
if DEBUG:
print 'Looking up: %s' % oracle_user
user = User.objects.get(username=oracle_user)
except:
if DEBUG:
print 'Adding user: %s' % oracle_user
user = User(username=oracle_user)
user.set_unusable_password()
try:
user.save()
except:
if DEBUG:
print "ERROR: adding %s" % oracle_user
return None
return user
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
复制代码...以供参考。没有经过我的测试。我对oracle数据库了解不多。希望这会有所帮助...
答案 1 :(得分:0)
默认情况下,Django支持多个数据库。 官方documentation附带了大量示例,也符合您的标准。
您可以使用Database routers指示Django使用哪个数据库进行身份验证。