使用授权标题打开InAppBrowser [英] Open InAppBrowser with Authorization Header

查看:143
本文介绍了使用授权标题打开InAppBrowser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Ionic2应用程序,我需要下载发票,但仅适用于经过身份验证的用户.有什么办法吗?

For my Ionic2 app i have a requirement of downloading invoices but only for authenticated users. Is there any way to do it?

我已经安装了InAppBrowser cordova插件,但似乎无法将其用于发送带有请求的授权标头.

I have installed the InAppBrowser cordova plugin but it seems like it cannot be used for sending Authorization Header with request.

推荐答案

对我有用: 查找(YourProject)\ plugins \ cordova-plugin-inappbrowser \ src \ android \ InAppBrowser.java文件并进行编辑.

This work for me: Look for the (YourProject)\plugins\cordova-plugin-inappbrowser\src\android\InAppBrowser.java file and edit it.

在文件末尾的onReceivedHttpAuthRequest中,应放置如下内容.

In the onReceivedHttpAuthRequest at the end of the file you should put something like this.

    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {

        // Check if there is some plugin which can resolve this auth challenge
        String UserName = getUser();
        String Password = getPass();

        if (UserName != null && Password!= null) {
            handler.proceed(UserName, Password);
            return;
        }

您可以用用户名和密码替换getUser()getPass(),以尝试是否有效.创建方法以从输入中检索数据.

You can replace the getUser() and getPass() with your username and password to try if is working, if it does. Create methods to retrieve the data from the inputs.

在我的情况下,我使用这个:

In my case I use this:

public void setUserPass(String username, String password) {
    this.USUARIO = username;
    this.CONTRASENA = password;
}    

public String getUser() {
    return this.USUARIO;
}

public String getPass() {
    return this.CONTRASENA;
}

包含onReceivedHttpAuthRequest方法的InAppBrowserClient方法之外.然后几乎在文档的顶部,我用execute方法(setUserPass(args.getString(3),args.getString(4));)

Outside the method InAppBrowserClient that contains the onReceivedHttpAuthRequest method. Then almost in the top of the document I wrote this in the execute method (setUserPass(args.getString(3), args.getString(4));)

public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
    if (action.equals("open")) {
        this.callbackContext = callbackContext;
        final String url = args.getString(0);
        setUserPass(args.getString(3), args.getString(4));
        String t = args.optString(1);
        if (t == null || t.equals("") || t.equals(NULL)) {
            t = SELF;
        }
        final String target = t;

然后,您必须在最后编辑(YourProject)\ plugins \ cordova-plugin-inappbrowser \ www \ inappbrowser.js文件.

Then you have to edit the (YourProject)\plugins\cordova-plugin-inappbrowser\www\inappbrowser.js file at the end.

module.exports = function(strUrl, strWindowName, strWindowFeatures, callbacks) {
    // Don't catch calls that write to existing frames (e.g. named iframes).
    if (window.frames && window.frames[strWindowName]) {
        var origOpenFunc = modulemapper.getOriginalSymbol(window, 'open');
        return origOpenFunc.apply(window, arguments);
    }

    strUrl = urlutil.makeAbsolute(strUrl);
    var iab = new InAppBrowser();

    callbacks = callbacks || {};
    for (var callbackName in callbacks) {
        iab.addEventListener(callbackName, callbacks[callbackName]);
    }

    var cb = function(eventname) {
       iab._eventHandler(eventname);
    };

    strWindowFeatures = strWindowFeatures || "";

    exec(cb, cb, "InAppBrowser", "open", [strUrl, strWindowName, strWindowFeatures[0], strWindowFeatures[1], strWindowFeatures[2]]);
    return iab;
};

最后,在index.js或控制您应用程序的所有js中,都使用相同的调用,但是您使用功能部件作为数组添加了登录数据,如下所示:

Finally, in the index.js or any js that control your app use the same call but you add your login data using the feature part as an array and like this:

window.open(URL,'_blank',['toolbar = no,presentationstyle = fullscreen,location = no,hardwareback = yes,hidden = no',用户名和密码]);

window.open(url, '_blank', ['toolbar=no,presentationstyle=fullscreen,location=no,hardwareback=yes,hidden=no', username, password]);

要激活所有这些功能,您应该在控制台中构建项目.

To activate all this you should build the project in the console.

希望这对某人有所帮助.

Hope this help somebody.

这篇关于使用授权标题打开InAppBrowser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆