关于Android中webview跨域Cookie的坑

5,673 阅读1分钟

最近,一直在做VR3D项目,前端H5页面在webView中加载时,获取不到设置Cookie信息,排除了Cookie设置问题,期初怀疑是跨域导致,验证结果在客户端和IOS测试都发现其问题,但发现在android里跨域请求webView压根没自动把cookie带上,什么鬼...

群里有同仁提醒了我,接着就去翻Android源码,结果还真让我找到了问题点。 源码文件CookieManager.java 接下来看这个setAcceptThirdPartyCookies抽象方法(注意看注释):

/**
     * Sets whether the {@link WebView} should allow third party cookies to be set.
     * Allowing third party cookies is a per WebView policy and can be set
     * differently on different WebView instances.
     * <p>
     * Apps that target {@link android.os.Build.VERSION_CODES#KITKAT} or below
     * default to allowing third party cookies. Apps targeting
     * {@link android.os.Build.VERSION_CODES#LOLLIPOP} or later default to disallowing
     * third party cookies.
     *
     * @param webview the {@link WebView} instance to set the cookie policy on
     * @param accept whether the {@link WebView} instance should accept
     *               third party cookies
     */
    public abstract void setAcceptThirdPartyCookies(WebView webview, boolean accept);

重点看这个:

{@link android.os.Build.VERSION_CODES#LOLLIPOP} or later default to disallowing 
 * third party cookies

翻译:LOLLIPOP (代表API版本:21)这个版本以及以上版本默认不支持第三方cookie。

  • 需要共享cookie信息,必须设置setAcceptCookie(true),意为允许存放和接收cookie
  • setAcceptFileSchemeCookies:允许存放和接收 file scheme URLs 中的cookie。谨慎使用

最后附上修改cookie代码:

public void synCookies(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        CookieSyncManager.createInstance(ProHelper.getApplication());
    }
	CookieManager cookieManager = CookieManager.getInstance();
	cookieManager.setAcceptCookie(true);// 允许接受 Cookie
	//>=LOLLIPOP 版本
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
		cookieManager.setAcceptThirdPartyCookies(webView, true);  //跨域cookie读取
	}
 
	String[] cookies = cookie.split(",");
	// WEB_COOKIE_DOMAIN为支持的域名数组,如{".autohome.com",".che168.com"}
	for (int i = 0; i < RegexURLUtil.WEB_COOKIE_DOMAIN.length; i++) {
		for (String cooky : cookies) {
			String[] values = cooky.split("=");
			String url_cookies = values[0] + "=" + values[1] + ";domain=" + RegexURLUtil.WEB_COOKIE_DOMAIN[i] + ";path=/";
			cookieManager.setCookie(RegexURLUtil.WEB_COOKIE_DOMAIN[i].replaceFirst(".", ""), url_cookies);
		}
	}
 
	if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
		CookieSyncManager.getInstance().sync();
	} else {
		cookieManager.flush();
	}
}

最后找到我们APP开发同学验证,果不其然啊。

参考文档

Android社区

developer.android