如何清理在WebView中创建的会话?

时间:2019-07-29 14:56:01

标签: android session webview android-cookiemanager

我有一个WebView实例,该实例允许用户进行身份验证并访问Web应用程序提供的功能的某些部分。用户完成WebView的工作后,出于安全目的,我们需要清理会话。

奇怪的是,下次我用WebView打开屏幕时,该会话仍然有效,尽管我尝试清理该会话,但仍可以继续进行。

我尝试做的事情(实际上我运行调试器以确保调用了这些方法):

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

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

我在flush()的回调中调用removeAllCookies()的原因是removeAllCookies()方法是异步的,所以我想可能要花一些时间来同步状态具有持久存储的内存cookie存储,但这无济于事。

我对CookieManager没什么好想的,这是操作系统(在Pixel 2 XL上安装的Android 9.0)提供的默认设置。我有什么想念的吗?

2 个答案:

答案 0 :(得分:0)

尝试一下:

@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);

它仍然无法正常工作,然后尝试使用此hack

首先获取您的首选项值的实例

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

清除您的应用数据

//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;
    }

还原首选项

   //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();
   }

然后像这样使用

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

答案 1 :(得分:0)

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

WebStorage.getInstance().deleteAllData()
相关问题