使用授权标头打开InAppBrowser

时间:2016-03-16 16:40:08

标签: angular http-basic-authentication ionic2 angular2-http

对于我的Ionic2应用程序,我需要下载发票,但仅限经过身份验证的用户。有没有办法做到这一点?

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

2 个答案:

答案 0 :(得分:0)

这项工作对我来说: 查找(YourProject)\ plugins \ cordova-plugin-inappbrowser \ src \ android \ InAppBrowser.java文件并进行编辑。

在文件末尾的onReceivedHttpAuthRequest中,您应该输入这样的内容。

    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()替换为您的用户名和密码,以尝试是否有效,如果有的话。创建从输入中检索数据的方法。

在我的情况下,我使用它:

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));)

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文件。

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中使用相同的调用,但你使用功能部分作为数组添加你的登录数据,如下所示:

window.open(网址,' _blank',[' toolbar = no,presentationstyle =全屏,location = no,hardwareback = yes,hidden = no',用户名,密码] );

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

希望这有助于某人。

答案 1 :(得分:-1)

标题可以与请求兄弟一起发送。你是否尝试过这样:

import {Http, Headers} from 'angular2/http';


    var headers = new Headers();
    headers.append('Authorization', 'Bearer ' + this.authToken));
    this.http.get('http://localhost:3333/getinfo', {headers: headers}).subscribe(data => {
    console.log(data);

请查看 - http://tphangout.com/?p=162 (它提供了有关授权请求的简单说明)。希望这会对你有所帮助。