让Android的WebView不存储cookie或密码密码、Android、WebView、cookie

2023-09-12 10:58:30 作者:望不穿秋水

我用的Android的WebView Twitter的OAuth的:微博要求用户登录和授权应用程序,我检索访问令牌,坚持在我的应用程序

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.

我有没有必要(也不会)存储用户密码,但web视图一直围绕Twitter的饼干,它也要求用户如果他想的是要记住的密码。由于这样的结果是,即使后,通过他的Twitter帐户页面去授权的应用程序,和我的应用程序销毁的访问令牌,下一次的WebView打开时,它可能仍然登录,即使没有,它有密码箱已经装满。

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.

我怎么能强迫的WebView不要求记忆的密码,并不会保留会话cookie? 如果这是不可能的,我可以删除其所有的存储状态(也许除了图像缓存)?

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)?

推荐答案

您可以使用此prevent cookies的存储和干净的饼干已经存储:

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)
 
精彩推荐