如何在Angularjs 2.0中使用基本身份验证调用REST服务? [英] How to call REST service with Basic Authentication in Angularjs 2.0?

查看:26
本文介绍了如何在Angularjs 2.0中使用基本身份验证调用REST服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angularjs 1.3中具有Java REST服务和使用者.服务基于JSON,并且需要BASIC身份验证(用户名密码). 在AgJs 1.3中,我使用Base64工厂代码(可在google上找到)从用户名和密码创建基本身份验证令牌,并能够成功使用服务.

I have java REST services and consumer in Angularjs 1.3. Services are JSON based and expect BASIC authentication (username-password). In AgJs 1.3, I used Base64 factory code (found on google) for creating basic authentication token from username and password and able to consume services SUCCESSFULLY.

我阅读了AgJs 2.0文档中的HTTP类,但是没有找到有关基本身份验证的任何信息.

I read HTTP class in AgJs 2.0 documentation but I didn't find any information about basic authentication.

有人可以提供示例代码来使用BASIC身份验证在Angularjs 2.0中使用REST服务吗?

Could anybody please provide sample code to consume REST services in Angularjs 2.0 using BASIC authentication?

提前谢谢!

最诚挚的问候!

我的AgJs 1.3代码:

My AgJs 1.3 code:

var url =" http://x.x.x.x:xxxx/data/" +数字;

var url = "http://x.x.x.x:xxxx/data/" + number;

$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode('username' + ':' + 'password');
$http.defaults.headers.common['Content-Type'] = 'application/json';

$http({ method: 'GET', url: url }).
        success(function (data, status, headers, config) {
           alert(data);
        }).
        error(function (data, status, headers, config) {
            alert("error: " + data);
        });

推荐答案

我刚刚从angular2到具有Basic auth的服务器上发布了一个帖子,这可行:

I have just make a post from angular2 to a server that has Basic auth and this works:

    sendAuthentification(credentials: string): Observable<any> {
          var headers = new Headers();
          headers.append('Authorization', 'Basic ' +  btoa('user:pass'));

          return this.http.get("http://ip/yourpost", headers});
   }

希望这个回答您的问题.

Hope this answer your question.

这篇关于如何在Angularjs 2.0中使用基本身份验证调用REST服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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