简体   繁体   English

在Android中区分HTTPOnly Cookie与其他Cookie

[英]Differentiating HTTPOnly cookies from other in android

In my current project we are implementing session handling between Android application and server. 在我当前的项目中,我们正在实现Android应用程序与服务器之间的会话处理。 Now by our design Android application should accept only HTTP Cookies and remove all other cookies. 现在,根据我们的设计,Android应用程序应仅接受HTTP Cookie并删除所有其他Cookie。 But looking at all the available option I couldn't find any class or method which help me to identify whether cookie is HTTPOnly or not. 但是,在所有可用选项中,我找不到任何类或方法来帮助我确定cookie是否为HTTPOnly。

I am storing cookies in following way: 我以以下方式存储cookie:

        connections = (HttpURLConnection) serverURL.openConnection();
        // Setting cookies manager
        java.net.CookieManager manager = new java.net.CookieManager();
        manager.setCookiePolicy(new CookiePolicy() {

            @Override
            public boolean shouldAccept(URI uri, HttpCookie cookie) {
                return cookie.getSecure();
            }
        });
        CookieHandler.setDefault(manager);

        connections.setDoInput(true);
        connections.setDoOutput(true);
        connections.setConnectTimeout(TIME_OUT);

        connections.getOutputStream().write(data);

        InputStream inputStream = connections.getInputStream();
        CookieStore cookieJar = manager.getCookieStore();
        if (cookieJar != null) {
            List<HttpCookie> cookies = cookieJar.getCookies();
            for (HttpCookie httpCookie : cookies) {

                Log.i("yash", httpCookie.toString());
            }
        }

But this HttpCookie doesn't have any HTTPOnly method. 但是此HttpCookie没有任何HTTPOnly方法。

By some google browing I find out that RFC 6265 has HTTPOnly attribute and it also obsulate RFC 2965. But Why google has not supported this RFC 6265? 通过一些Google浏览器,我发现RFC 6265具有HTTPOnly属性,并且它也使RFC 2965变得模糊。但是,为什么Google不支持此RFC 6265?

According to the class documentation HttpOnly is supported but for some reason there isn't any accessor nor mutator for this field. 根据类文档 ,支持HttpOnly,但是由于某种原因,此字段没有任何访问器或更改器。

To be able to access and modify the httpOnly field you should use reflection: 为了能够访问和修改httpOnly字段,您应该使用反射:

// Workaround httpOnly (getter)
private boolean getHttpOnly() {
    try {
        Field fieldHttpOnly = cookie.getClass().getDeclaredField("httpOnly");
        fieldHttpOnly.setAccessible(true);

        return (boolean) fieldHttpOnly.get(cookie);
    } catch (Exception e) {
        // NoSuchFieldException || IllegalAccessException ||
        // IllegalArgumentException
        Log.w(TAG, e);
    }
    return false;
}

// Workaround httpOnly (setter)
private void setHttpOnly(boolean httpOnly) {
    try {
        Field fieldHttpOnly = cookie.getClass().getDeclaredField("httpOnly");
        fieldHttpOnly.setAccessible(true);

        fieldHttpOnly.set(cookie, httpOnly);
    } catch (Exception e) {
        // NoSuchFieldException || IllegalAccessException ||
        // IllegalArgumentException
        Log.w(TAG, e);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM