【问题标题】:Cordova: Share webview cookies with native pluginsCordova:与本机插件共享 webview cookie
【发布时间】:2015-06-15 22:05:29
【问题描述】:

我有一个运行 iOS 和 Android 的 cordova 应用程序,它使用 cookie 进行身份验证(用户只需输入电子邮件和密码,应用程序内的浏览器设置 cookie,在 Rails 中使用设计后端)。到目前为止,这有效。

现在我有一个适用于 android 和 iOS 的本机插件,它以后台模式 (iOS) / 作为服务 (Android) 上传一些数据。问题是,插件尝试访问的 URL 也受到保护。因此,插件需要以某种方式将 cookie 与请求一起发送。

我找到了很多文章,其中大部分都有点旧(比如这个http://blog.winfieldpeterson.com/2013/01/17/cookies-in-hybrid-android-apps),每一个解决方案都感觉有点矫枉过正。

我使用的是 Cordova 4.3.0,也许有一个标准的解决方案,但我还没有找到(并且在所有其他文章都写完后得到了更新)。

这是我的 Android 和 iOS 代码的一部分,它将数据发送到服务器:

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
HttpClient httpclient = new DefaultHttpClient();

SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(httpclient.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, httpclient.getParams());

// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

HttpPost httppost = new HttpPost(locationCache.getUrl());
httppost.setHeader("Content-type", "application/json");

JSONObject body;
body = new JSONObject();
body.put("someKey", "someData");

HttpResponse response = httpclient.execute(httppost);

还有 iOS 版本:

NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:geodata options:0 error:&err];
NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:url]];
postRequest.HTTPMethod = @"POST";
[postRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
postRequest.HTTPBody = [jsonString dataUsingEncoding: NSUTF8StringEncoding];
[NSURLConnection sendAsynchronousRequest:postRequest queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           if(error) {
                             NSLog(@"Httperror: %@, %d", error.localizedDescription, error.code);
                           } else {
                              NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                              NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                              NSLog(@"REQUEST SENT TO SERVER! HTTP response code: %d; response body: %@", responseCode, responseString);
                           }
                       }];

希望有人能提供帮助。

【问题讨论】:

    标签: android ios cordova authentication cookies


    【解决方案1】:

    在 iOS 上,您可以像这样获取 url 的 cookie:

    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"__YOUR_URL__"]];
    

    或所有像这样的cookie:

    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    

    之后,您可以像这样设置请求标头:

    NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
    [postRequest setAllHTTPHeaderFields:headers]; 
    

    在 android 上你有 webview CookieManager

    CookieManager cookieManager=CookieManager.getInstance();
    String cookie=cookieManager.getCookie(url);
    

    【讨论】:

    • 感谢您的回答。我用 android 试了一下(iOS 明天跟着),但 cookie 变量是 null。我看了一下cookieManager.acceptCookie(),即truecookieManager.hasCookies()false。我必须指定要使用的 webview 吗?我假设整个 Cordova 应用程序只有一个 webview,对吧?
    • android cookieManager有两个,你是导入java.webkit还是java.net?应该是第一个,但是两个都试试
    • 我正在使用import android.webkit.CookieManager;。当我尝试导入import java.webkit.CookieManager; 时,该包不存在。使用import java.net.CookieManager; 时会引发运行时错误cannot find symbol method getInstance()。但他们都没有工作......还有其他想法吗?
    • 您使用的cookies有过期日期吗?我已经看到 inAppBrowser 插件在您启动时会删除没有过期日期的 cookie
    • 太棒了,就是这样。非常感谢!
    猜你喜欢
    • 2015-04-22
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    相关资源
    最近更新 更多