我正在尝试加载网页(例如 www.google.com),但网页 View 通过下一条消息发送到控制台:
A cookie associated with a resource at http://google.com/ was set with
SameSite=None
but withoutSecure
. A future release of Chrome will only deliver cookies markedSameSite=None
if they are also markedSecure
. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5633521622188032.", source: https://www.google.com/
我不知道如何解决这个问题。我正在执行下一个代码来加载 url,我该如何解决这个问题?
private void initWebView(String url){
webViewTvWeakReference = new WeakReference<>(new WebView(getActivity()));
webViewTvWeakReference.get().setId(TV_WEB_VIEW_ID);
webViewTvWeakReference.get().setLayerType(View.LAYER_TYPE_HARDWARE, new Paint());
webViewTvWeakReference.get().setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
webViewTvWeakReference.get().setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.lightBlueBackgroundColor));
webViewTvWeakReference.get().requestFocusFromTouch();
setUpWebSettings(webViewTvWeakReference.get());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().removeAllCookies(null);
}
Map<String, String> headers = new HashMap<>();
headers.put("Set-Cookie", "HttpOnly;Secure;SameSite=Strict");
if (BuildConfig.DEBUG) {
webViewTvWeakReference.get().setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
}
if(!url.isEmpty()){
webViewTvWeakReference.get().loadUrl(url, headers);
}else{
webViewTvWeakReference.get().loadUrl("about:blank", headers);
}
webViewTvWeakReference.get().pauseTimers(); // WebView timers consumes a lot off CPU
webViewTvWeakReference.get().resumeTimers(); // Pause and Resume timers fix this behaivour
}
我已将下一行添加到标题中,但仍然无法正常工作。
headers.put("Set-Cookie", "HttpOnly;Secure;SameSite=Strict");
谢谢
最佳答案
此警告专门针对来自 google.com
的 cookie,因此您无需负责设置其属性。 Google 的团队需要将相关属性应用于他们的 cookie 才能解决此警告。
如果您确实有任何需要在跨站点/第三方上下文中发送的 cookie,则需要使用以下方式设置它们:
Set-Cookie: foo=bar; SameSite=None; Secure
但是,在这个阶段,这个警告应该纯粹是信息性的。 cookie 仍随请求一起发送,您应该不会在 WebView 中看到网站行为的任何问题。
这些新限制将成为 Chrome 80 中的默认限制,但它们暂时不适用于 WebView。
您可以在 https://web.dev/samesite-cookies-explained 了解有关这些即将发生的变化的更多信息.
此外,您可以通过将其指向 https://samesite-sandbox.glitch.me 来检查您的特定浏览器或 WebView 是否正在实现新行为。 .如果该矩阵全是绿色,则您的浏览器正在实现新限制。如果板上有任何红色或橙色,则表示您的浏览器尚未应用新的默认设置。
关于Android WebView 没有加载 url SameSite=None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58751376/