【问题标题】:Direct access to database for authentification in django在 django 中直接访问数据库进行身份验证
【发布时间】:2015-05-04 21:44:25
【问题描述】:

我有一个小 django 应用程序,它允许显示一些与用户相关的信息。此信息存储在 sqlite 中,我对此表示满意。 用户的登录名和密码存储在另一个 oracle 数据库中,我的问题是:如何使用存储在 oracle db 中的 creditinals 对用户进行身份验证?我想,直接访问 db (select) 就可以了,因为不能更改 oracle db 中的数据。还有另一个问题来了:如何存储 django 超级用户数据? 提前致谢。

【问题讨论】:

    标签: python django database django-models


    【解决方案1】:

    关于如何使用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数据库了解不多。希望这会有所帮助...

    【讨论】:

    • 感谢您的回答。这似乎不是简单的方法=\
    【解决方案2】:

    默认情况下,Django 支持多个数据库。 官方documentation 提供了大量示例,也符合您的条件。

    您可以使用Database routers 指示 Django 使用哪个数据库进行身份验证。

    【讨论】:

    • 感谢您的回答。在问这个问题之前,我已经看过了。不幸的是,oracle db 方法中的数据似乎并不简单且不安全(我在这里可能是错的)。我正在寻找一种非常简单的方法来获取用户名和密码,例如 if ("select from users where username = %username) 等等。
    猜你喜欢
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多