Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python登录屏幕-如何从json文件验证密码?_Python_Json_Python 3.x_Authentication - Fatal编程技术网

Python登录屏幕-如何从json文件验证密码?

Python登录屏幕-如何从json文件验证密码?,python,json,python-3.x,authentication,Python,Json,Python 3.x,Authentication,我正在写一个登录屏幕,要求输入用户名和密码。一旦这个屏幕正常工作,我计划将它添加到我正在处理的一个更大程序的开头 我目前遇到的问题是,我无法验证特定用户的密码。现在,程序将检查用户名是否已存在,如果已存在,则将检查密码是否存在。问题是,有人可以输入另一个用户名和自己的密码,然后登录到其他人的帐户。安全性对我的程序来说不是什么大问题,我只是在制作一个duolingo类型的语言应用程序,但这是一个非常明显的问题,我想知道如何解决 from tkinter import * import json c

我正在写一个登录屏幕,要求输入用户名和密码。一旦这个屏幕正常工作,我计划将它添加到我正在处理的一个更大程序的开头

我目前遇到的问题是,我无法验证特定用户的密码。现在,程序将检查用户名是否已存在,如果已存在,则将检查密码是否存在。问题是,有人可以输入另一个用户名和自己的密码,然后登录到其他人的帐户。安全性对我的程序来说不是什么大问题,我只是在制作一个duolingo类型的语言应用程序,但这是一个非常明显的问题,我想知道如何解决

from tkinter import *
import json
class LoginFrame(Frame):
    def __init__(self, master):
        super().__init__(master)
        master.title("Login")

        self.label_username = Label(self, text="Username")
        self.label_password = Label(self, text="Password")

        self.entry_username = Entry(self)
        self.entry_username.focus()     #This sets the focus to the username entry box
        self.entry_password = Entry(self, show="*")

        self.label_username.grid(row=0, column=0)
        self.label_password.grid(row=1, column=0)
        self.entry_username.grid(row=0, column=1)
        self.entry_password.grid(row=1, column=1)

        self.login_button = Button(self, text="Login", command=self.Login)
        self.login_button.grid(columnspan=2)

        self.grid()

    def Login(self):
        current_info = open ("C:\\LearningArabic\\LiblibArriby\\Usernames\\usernames.json").read()

        username = self.entry_username.get()
        password = self.entry_password.get()

        if username in current_info:
            print("Test")
            if password in current_info:
                print("Now we're talking")
            else:
                print("Well, you're trying")
        else:       #This section appends the new username and password combo to the json file
            usr = {username: password}
            with open("C:\\LearningArabic\\LiblibArriby\\Usernames\\usernames.json") as a:
                data = json.load(a)

            data.update(usr)

            with open("C:\\LearningArabic\\LiblibArriby\\Usernames\\usernames.json", "w") as a:
                json.dump(data, a)
root = Tk()
lf = LoginFrame(root)
root.mainloop()

任何帮助都将不胜感激,如果对代码有其他评论,请不要犹豫。我想学习,而不仅仅是得到答案

保持您当前的文件格式,我只需执行以下操作:

PATH_USERNAMES = "C:/LearningArabic/LiblibArriby/Usernames/usernames.json"

with open(PATH_USERNAMES) as fd:
  current_info = json.load(fd)

username = self.entry_username.get()
password = self.entry_password.get()

if username in current_info:
  saved_password = current_info.get(username, '')
  if password == saved_password:
    print("Password OK")

但强烈建议您保存散列的密码并适当更改验证…

只需检查;这是一个玩具的例子,对吗?您没有在JSON中存储任何模糊重要的密码?您可能想对密码进行哈希运算,请参阅类似的内容:@roganjosh,是的-此处存储的密码没有任何重要用途。事实上,我几乎完全跳过了密码,只使用了用户名,但我认为这样能让我学到更多。@SamMason,谢谢你的提示!我尝试了一下,但得到了保存的\u password=current\u info.getusername,AttributeError:“str”对象没有属性“get”任何想法?你想使用json.dump从文件中获取对象,而不是读取itjson.dump而不是json.load吗?难道我不想加载文件,这样我就可以检查内容了吗?是的,你说得对……对不起,集中精力在其他事情上!啊,就是这样。谢谢你的快速提示!我肯定会研究散列。