使Android WebView不存储cookie或密码

发布于 2024-10-25 12:24:42 字数 459 浏览 1 评论 0原文

我使用 Android WebView for Twitter OAuth:Twitter 要求用户登录并授权应用程序,我检索访问令牌并将其保留在我的应用程序中。

我不需要(也不)存储用户密码,但 WebView 保留 Twitter 的 cookie,并且还会询问用户是否希望它记住密码。因此,即使在通过他的 Twitter 帐户页面取消对应用程序的授权,并且我的应用程序销毁了访问令牌之后,下次打开 WebView 时,它可能仍然处于登录状态,即使没有,它也已登录密码框已填满。

如何强制 WebView 不要求记住密码,也不保留会话 cookie? 如果这是不可能的,我可以删除其所有存储的状态(图像缓存除外)吗?

I use an Android WebView for Twitter OAuth: Twitter asks the user to log in and authorize the application, I retrieve the access token and persist it in my application.

I have no need (and do not) store the user password, but the WebView keeps Twitter's cookies around, and it also asks the user if he wants it to remember the password. As a result of this, even after the de-authorizes the application via his Twitter account page, and my application destroys the access tokens, the next time the WebView is opened, it is probably still logged in, and even if not, it has the password box already filled.

How can I force WebView to not ask to remember passwords, and to not persist session cookies?
If that is not possible, can I delete all its stored state (except maybe the image cache)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

东北女汉子 2024-11-01 12:24:42

您可以使用它来阻止存储 cookie 并清除已存储的 cookie:

CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(callback);
cookieManager.setAcceptCookie(false);

WebView webview = new WebView(this);
WebSettings ws = webview.getSettings();
ws.setSaveFormData(false);
ws.setSavePassword(false); // Not needed for API level 18 or greater (deprecated)

You can use this to prevent cookies from being stored and clean cookies already stored:

CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(callback);
cookieManager.setAcceptCookie(false);

WebView webview = new WebView(this);
WebSettings ws = webview.getSettings();
ws.setSaveFormData(false);
ws.setSavePassword(false); // Not needed for API level 18 or greater (deprecated)
寄居者 2024-11-01 12:24:42

对于不保存密码:

WebView webview = new WebView(this);
WebSettings mWebSettings = webview.getSettings();
mWebSettings.setSavePassword(false);
mWebSettings.setSaveFormData(false);

对于 cookie:

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(false);

我不太确定 cookie 的实现。

For not saving passwords:

WebView webview = new WebView(this);
WebSettings mWebSettings = webview.getSettings();
mWebSettings.setSavePassword(false);
mWebSettings.setSaveFormData(false);

For cookies:

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(false);

I am not very sure for the cookies implementation.

岛歌少女 2024-11-01 12:24:42

这是我在这方面看到的最好的答案

    webView.clearCache(true);
    webView.clearHistory();
    WebSettings webSettings = webView.getSettings();
    webSettings.setSaveFormData(false);
    webSettings.setSavePassword(false); // Not needed for API level 18 or greater (deprecated)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    } else {
        CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(this);
        cookieSyncMngr.startSync();
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
        cookieSyncMngr.sync();
    }

This is the best answer I have seen in this context

    webView.clearCache(true);
    webView.clearHistory();
    WebSettings webSettings = webView.getSettings();
    webSettings.setSaveFormData(false);
    webSettings.setSavePassword(false); // Not needed for API level 18 or greater (deprecated)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    } else {
        CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(this);
        cookieSyncMngr.startSync();
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
        cookieSyncMngr.sync();
    }
灼痛 2024-11-01 12:24:42

在一行中,试试这个。我认为这应该在启动 webview 后调用。

android.webkit.CookieManager.getInstance().removeAllCookie();

In one line, Try this. I think this should be called after starting the webview.

android.webkit.CookieManager.getInstance().removeAllCookie();
逆蝶 2024-11-01 12:24:42

不要清除cookie,因为它会影响其他会话,如facebook等。存储在cookie内,所以尝试遵循此方法

在oauth事务之前,例如在webview创建之前

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(false);

在oauth事务之后让通过设置接受cookie

cookieManager.setAcceptCookie(true);

它将工作我已经测试过它..

Don't clear cookies beacause it will effect other sessions like facebook etc.. stored inside the cookie so try to follow this method

Before oauth transaction such as before the webview creation

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(false);

After oauth transaction let accept cookie by setting

cookieManager.setAcceptCookie(true);

it will work i have tested it..

海拔太高太耀眼 2024-11-01 12:24:42

我使用了以下解决方案:

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(false);

以下方法对我不起作用:

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();

可能的原因可能是我们没有同步 cookie,如下所示:

CookieSyncManager.createInstance(getContext()).sync();

但这可能需要时间。

强制 WebView 不要求记住密码也不起作用。

而且它也不利于可用性。

I have used following solution:

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(false);

Following method does not worked for me:

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();

A possible reason may be that we have not synced the cookies as following:

CookieSyncManager.createInstance(getContext()).sync();

But it may be taking time.

Forcing WebView to not ask to remember passwords will also not work.

And it is also not good for usability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文