Should I safely store the token in memory or in sqlite? or suggest

I started learning Android a few days ago, and so far I have finished implementing the login function, the main activity that extends the abstract basic activity.

The Nav Bar element, when clicked, opens xml from fragments.

I have a question about the token that I get after a successful login. This token is used with each request to receive data after a successful login. Should I save the token in the sqlite database safely or should I make a public property in the main activity? The main activity will always remain in memory, as this will open fragments.

+4
source share
7 answers

3 : 1) , - :

public static void saveToken(Context ctx, String fileName, Object token) {
        if (token == null) {
            ctx.deleteFile(fileName);
        } else {
            ObjectOutputStream out = null;
            try {
                FileOutputStream fout = ctx.openFileOutput(fileName, 0);
                out = new ObjectOutputStream(fout);
                out.writeObject(token);
                fout.getFD().sync();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null)
                        out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

, token java.io.Serializable interface.

API 24:

saveToken(appContext, someFileName, someTokenObject);

API 24 :

saveToken(appContext.createDeviceProtectedStorageContext(), someFileName, someTokenObject);

2) SQLCipher .

3) , https://developer.android.com/training/articles/keystore.html

+4

SharedPreferences , Context.MODE_PRIVATE , . SharedPreferences -

.

SharedPreferences prefs = context.getPreferences(Context.MODE_PRIVATE);
prefs.edit().putString("token", token).apply();
token = prefs.getString("token");

SQLite:

SQLite - , .

:

, . .

+1

sqlite ? , .

Android " ". :

,

, , .

" " , , . Java public private . .

, public , , char[], String. .

, , sqlite . KeyStore, . . , , Scytale.

0

1) singleton ( BaseApplication)

public class BaseApplication extends Application {
    // token
    private String token = null;
    public String getToken() {return this.token;}
    public void setToken(String token) {this.token = token;}
}

/. , .

. REST api, , , .

2) SharedPreferences - , .

. @Ryan.

0

SharedPreferences token.it, .

0

Sqlite Realm. , . , , . MainActivity , , .

Shared Preference . , . Android Realm

-1

Shared Preference, .

. , .

, , , https://github.com/ophio/secure-preferences , java , , proguard, playstore.

, .

sqlite, root, db , . , , sqlite. .

, .

-1

Source: https://habr.com/ru/post/1672799/


All Articles