【问题标题】:How to hide password in Database Connection?如何在数据库连接中隐藏密码?
【发布时间】:2021-10-12 01:30:03
【问题描述】:

我正在编写一个从 Python 读取 Oracle 数据库的程序。

我遇到的最大问题是我想隐藏密码。如有必要,我什至希望通过文本字段输入密码。

这是我知道的用 Python 连接到 Oracle 数据库的代码行。

db_conn = cx_Oracle.connect(user=r'myUserName', password='myPassword', dsn=dsn_tns)

【问题讨论】:

  • 你想把密码隐藏在哪里?在日志中?是什么阻止您输入密码?
  • 您可以将凭证存储在 Oracle 钱包中。见这里 - github.com/oracle/python-cx_Oracle/issues/260
  • 我很困惑。您的主题行提到了数据库链接,所有受访者都提到了 Oracle 数据库链接,但您的邮件正文没有提到 Oracle 数据库链接,而是谈到了一些 Python 结构。
  • “数据库链接”是两个数据库之间连接的特殊术语。您正在尝试从 Python 连接到 Oracle 数据库; Python 不是数据库,所以这只是称为“数据库连接”。查看类似问题:stackoverflow.com/questions/51186723/…
  • @EdStevens - OldProgrammer 的回答是关于 OP 的实际问题 - 答案仍然是“使用钱包”

标签: python oracle cx-oracle


【解决方案1】:

更好的解决方案,DB LINK的定义中根本不提供密码,那么就没有什么可隐藏的了。

如何做到这一点?使用钱包。

@connor-mcdonald 在这里展示了如何做到这一点。 https://connor-mcdonald.com/2015/09/21/connection-shortcuts-with-a-wallet/

如果您阅读钱包文档,他们通常会谈论客户。在这种情况下,您在服务器上设置钱包,而数据库“是”客户端,至少在通过 DB_LINK 打开与远程数据库的连接时,它将从钱包中获取所需的密码,如这是 sqlnet.ora 配置。

另外(由@OldProgrammer 提供)这是来自cx_oracle site on using Oracle Wallet 的相关文章。

【讨论】:

    【解决方案2】:

    您可以创建一个函数来读取登录信息:

    #
    # This function gets the login information out of a credentials file
    #
    def get_login_info(file):
        # Create an array of the lines of the file
        line = open(file, "r").readlines()
        # Create a new list to be used to append the cleaned / trimmed lines
        new_line = []
        # Loop through the file
        for a in line:
            # Get rid of any new lines ("enter")
            a = a.replace("\n", "")
            # Add the cleaned data to the new_line list
            new_line.append(a)
        # Set the username to be the first object
        username = new_line[0]
        # Set the password to be the second object
        password = new_line[1]
    
        # Return them
        return username, password
    

    然后,使用它并将其传递给您的连接字符串:

    db_conn = cx_Oracle.connect(user=username, password=password, dsn=dsn_tns)
    

    所以一个示例文件可能是
    :

    JerryM
    Mypassword2019
    

    它会将JerryM 存储为username,将Mypassword2019 存储为password

    【讨论】:

      【解决方案3】:

      创建一个简单的python模块cred.py在模块中,保存用户名和密码

      user='myUserName'
      pass='myPassword'
      

      现在,当你运行你的 python 时:

      import cred

      db_conn = cx_Oracle.connect(user=cred.user, password=cred.pass, dsn=dsn_tns)
      

      这样您的密码就会保存在另一个文件中。确保将 cred.py 放置在原始 python 文件所在的同一路径中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-14
        • 1970-01-01
        • 2013-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-23
        • 2022-12-15
        相关资源
        最近更新 更多