简体   繁体   中英

How to clean up the session created in a WebView?

I have a WebView instance which allows the user to authenticate and get access to some parts of the functionality provided by the web app. Once the user has finished their work with the WebView , we need to clean up the session for security purposes.

What's strange, the next time I open a screen with WebView , the session is still alive and I can proceed where I left off, despite my attempts to clean up the session.

Things I tried to do (I actually ran the debugger to make sure these methods are called):

webView.clearCache(true)
webView.clearFormData()
webView.clearHistory()
webView.clearSslPreferences()
webView.clearMatches()

CookieManager.getInstance().removeAllCookies {
    CookieManager.getInstance().flush()
}

The reason I'm calling flush() in the removeAllCookies() 's callback is that removeAllCookies() method is async, so I was thinking that maybe it takes some time to sync the state of in-memory cookies storage with persistent storage, but this does not help.

I don't do anything fancy with CookieManager , it's a default one provided by OS (Android 9.0 installed on Pixel 2 XL). Is there anything I'm missing?

Try this:

@SuppressWarnings("deprecation")
public static void clearCookies(Context context)
{

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        Log.d(C.TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    } else
    {
        Log.d(C.TAG, "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
        CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
        cookieSyncMngr.startSync();
        CookieManager cookieManager=CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
        cookieSyncMngr.sync();
    }
}

//Use above
webView.clearCache(true);
webView.clearFormData();
webView.clearHistory();
webView.clearSslPreferences()
webView.clearMatches();
clearCookies(this);

and it's not still working then try this hack

First retrieve an instance of your preference values

//Object holding preference values before clearing data
Map<String, ?> allEntries = mySharedPreferece.getAll(); 

Clear your apps data

//Call this function to clear Apps data
public void clearApplicationData() {
        File cacheDirectory = getCacheDir();
        File applicationDirectory = new File(cacheDirectory.getParent());
        if (applicationDirectory.exists()) {
            String[] fileNames = applicationDirectory.list();
            for (String fileName : fileNames) {
                if (!fileName.equals("lib")) {
                    deleteFile(new File(applicationDirectory, fileName));
                }
            }
        }
    }

    public static boolean deleteFile(File file) {
        boolean deletedAll = true;
        if (file != null) {
            if (file.isDirectory()) {
                String[] children = file.list();
                for (int i = 0; i < children.length; i++) {
                    deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
                }
            } else {
                deletedAll = file.delete();
            }
        }

        return deletedAll;
    }

Restore Preference

   //Call this function to re-save the preference values
    public void restorePreference() {
     for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
       Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());
       mySharedPreferece.edit().putString(entry.getKey(),entry.getValue().toString());
     } 
       mySharedPreferece.edit().commit();
   }

Then use like this

clearApplicationData();
restorePreference();
//At this point apps data should be cleared leaving just the preference. 

好的,原来该会话已存储在本地存储中,因此您所需要做的就是:

WebStorage.getInstance().deleteAllData()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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