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

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


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


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()

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


皈依舞
浏览 148回答 1
1回答

三国纷争

保持您当前的文件格式,我只会做类似的事情: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")但强烈建议您保存散列的密码并适当更改验证......
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python