繁体   English   中英

Android - 保持用户登录

[英]Android - Keep user logged in

我正在尝试使用 PHP 和 MySQLi for Android 进行登录。 我不明白的是如何让用户保持登录状态? 我看到了一个简单的教程,其中有人使用 SQLite 来保护信息,但我不知道这是否真的安全。 我应该如何保存用户信息以保持用户登录?

谢谢你。

使用android中的SharedPreferences

当您的登录使用时存储数据

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
//on the login store the login
editor.putLong("key_name", "long value"); 
editor.commit();

检索密钥的数据

pref.getString("key_name", "");
                            ^
   default value if the user is not loggedin

注销时清除数据

editor.remove("name");
editor.commit();

请参阅此链接了解更多信息

我不确定 PHP 应用程序中的登录过程。 如果它使用身份验证令牌管理登录,则保存身份验证令牌。 如果它不使用任何基于令牌的身份验证,则意味着您可能正在通过会话管理登录。 在那里您可能必须同时保存用户 ID 和密码。

现在,为了安全地保存此类数据,我更喜欢 Android Jetpack 中的加密共享首选项

String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
     
SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
           "secret_shared_prefs",
           masterKeyAlias,
           context,
           EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
           EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
       );
// use the shared preferences and editor as you normally would
SharedPreferences.Editor editor = sharedPreferences.edit();

您还可以使用不可预测的密钥来确保您的数据更安全。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM