在桌面應用程序中保存用戶名和密碼 (Saving username & password in desktop app)


問題描述

在桌面應用程序中保存用戶名和密碼 (Saving username & password in desktop app)

I am making one frame which has two text field one for username & other for password,and one button to login.

Now I want to store my login information for a specified period of time , so that next time   when user open my frame he'll be able to retrieve his login information. I know this is possible through cookies, but I need to know how to apply this concept  in my frame.

My application is a windows based application, no link to browser.  

How to achieve this feature?


參考解法

方法 1:

You can client information to the system in any number of ways...

You could

Use Properties, which allow you to specify key/name pairs of information, which can loaded and saved to a file.

You could

Use the Preferences API which is simluar to the Properties API, except you get the added benifit of internal persistence (you don't need to read/write the files), groupings (you can have a key specified multiple times, so long as it's in different groups)

As a side note.  You shouldn't be storing passwords in any manner that they might be retrieved.  Instead you should store using something like a MD5 hash and compare hashs.  See Convert MD5 into String in java for more details...

方法 2:

You might like to use temporary file to write the user detail.  File tempFile = File.createTempFile("temp‑file‑name", ".tmp"); The next time the user comes, you can read the data from that temp file.  Additionally you can make the temp file hidden for more data security. 

It seems the temp file name is appended by a random number so it will be better to create the temp file your self.

This code snippet might help you. 

try {
        String tempDir = System.getProperty("java.io.tmpdir");
        File tempFile = new File(new File(tempDir), "userDetail.tmp");
        if(tempFile.exists()){//This is subsequent login 
            //Write your code to read file and 
            //your application logic
        }else{ //This is first login 
            tempFile.createNewFile();
            //Write your logic to write data to the file 
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

You can refactor the code to suit your design.

(by user2303984MadProgrammerpundit)

參考文件

  1. Saving username & password in desktop app (CC BY‑SA 3.0/4.0)

#Security #java #passwords #cookies #swing






相關問題

只允許 oracle db 登錄到特定的應用程序? (Allowing oracle db login only to specific application?)

在桌面應用程序中保存用戶名和密碼 (Saving username & password in desktop app)

如何使用算法 RSA/ECB/PKCS1Padding 通過 JavaScript 解密加密字符串 (How to decrypt through JavaScript of encrypted string using algorithm RSA/ECB/PKCS1Padding)

wcf:將用戶名添加到消息頭是否安全? (wcf: adding username to the message header is this secure?)

沒有 .htaccess 的安全目錄密碼保護 (Secure directory password protection without .htaccess)

無法在 Oracle 表上創建簡單視圖 (Unable to create a simple view on Oracle table)

當請求來自調度程序時,無法寫入 App_Data (Cannot write in App_Data when request is from scheduler)

安全的 PHP 文件上傳 (Secure PHP file uploading)

Grails Spring 安全配置通過 xml (Grails Spring Security Configuration thru xml)

醫療應用的安全要求 (Security Requirements for Medical Applications)

如何保護 Silverlight 應用程序 (How to Secure Silverlight Application)

在使用 azure 流量管理器和 azure 應用程序網關與 WAF 時實現國家級阻止 (Achieve country level blocking while using azure traffic manager and azure application gateway with WAF)







留言討論