我正在編寫(xiě)一個(gè)要求用戶名和密碼的登錄屏幕。一旦我讓這個(gè)屏幕正常工作,我計(jì)劃將它添加到我正在處理的更大程序的開(kāi)頭。我目前遇到的問(wèn)題是我無(wú)法驗(yàn)證特定用戶的密碼。現(xiàn)在,程序?qū)z查用戶名是否已存在,如果存在,則將檢查密碼是否存在。問(wèn)題是有人可以輸入另一個(gè)用戶名和自己的密碼,然后登錄到其他人的帳戶。安全性對(duì)我的程序來(lái)說(shuō)不是什么大問(wèn)題(我只是在制作一個(gè)多鄰國(guó)類型的語(yǔ)言應(yīng)用程序),但這是一個(gè)非常明顯的問(wèn)題,我想弄清楚如何解決。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()任何幫助將不勝感激,如果對(duì)代碼有其他評(píng)論,請(qǐng)不要退縮。我想學(xué)習(xí)——不僅僅是得到答案!
1 回答

三國(guó)紛爭(zhēng)
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊
保持您當(dāng)前的文件格式,我只會(huì)做類似的事情:
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")
但強(qiáng)烈建議您保存散列的密碼并適當(dāng)更改驗(yàn)證......
添加回答
舉報(bào)
0/150
提交
取消