Angular 2 auth http标头为空

时间:2017-04-24 23:49:52

标签: rest angular http authorization

我目前有一个非常简单的rest api,它允许通过基本安全性进行用户身份验证。我的角度客户端从登录输入接收电子邮件和密码,并从服务器获取用户。电子邮件和密码在http标头中发送。问题是,当我的securityfilter(JAX-rs java,jersey backend impl)我知道我的auth头是空的时候。

我从角度发送它们是空的吗?

  getSecuredUser(email: string, password: string){
    let headers: Headers = new Headers();
    headers.append("Authorization", "Basic " + email + ":" + password);
    headers.append("'Access-Control-Allow-Origin': '*' ", "application/x-www-form-urlencoded");
    return this.http.get(DEFAULT_PATH + 'users/' + email, headers).subscribe(
      (data: Response) => {
        var result = data.json();
        return result;
      }
    );
  }

1 个答案:

答案 0 :(得分:0)

您的问题是基本身份验证的标题。

getSecuredUser(email: string, password: string){
    let headers: Headers = new Headers();
    headers.append("Authorization", "Basic " + btoa(`${email}:${password}`);
    headers.append("'Access-Control-Allow-Origin': '*' ", "application/x-www-form-urlencoded");
    return this.http.get(DEFAULT_PATH + 'users/' + email, {headers: headers}).subscribe(
      (data: Response) => {
        var result = data.json();
        return result;
      }
    );
  }