为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

皈依舞 2021-06-23 18:19:10
我正在编写一个要求用户名和密码的登录屏幕。一旦我让这个屏幕正常工作,我计划将它添加到我正在处理的更大程序的开头。我目前遇到的问题是我无法验证特定用户的密码。现在,程序将检查用户名是否已存在,如果存在,则将检查密码是否存在。问题是有人可以输入另一个用户名和自己的密码,然后登录到其他人的帐户。安全性对我的程序来说不是什么大问题(我只是在制作一个多邻国类型的语言应用程序),但这是一个非常明显的问题,我想弄清楚如何解决。from tkinter import *import jsonclass 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()任何帮助将不胜感激,如果对代码有其他评论,请不要退缩。我想学习——不仅仅是得到答案!
查看完整描述

1 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

保持您当前的文件格式,我只会做类似的事情:


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

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


查看完整回答
反对 回复 2021-06-29
  • 1 回答
  • 0 关注
  • 147 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号