我已经为@ angular / Http创建了一个包装器服务,以添加Bearer身份验证的逻辑和Http Requests的计数器。
我想使用这个自定义服务来取代微风的服务。 如何做到这一点?
这是我的包装器的最简单版本
@import {Injectable} from '@angular/core'
import {Http, Headers} from '@angular/Http'
import {AuthService} from './auth.service'
@Injectable()
export class AuthHttpService
{
constructor(private http: Http, private authService: AuthService){}
get = (url: string){
//some logic for bearer authenticated calls
let params = {
// request extra info
};
return this.http.get(url, params);
}
}

我的微风经理面料服务。您可以看到身份验证承载逻辑。我希望持有者附加代码并更改整个Breeze的Http服务。
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable, Scheduler } from 'rxjs/Rx';
import { LocalStorage, SessionStorage } from 'ng2-webstorage';
import { config, NamingConvention, EntityManager, EntityQuery, DataService, MetadataStore } from 'breeze-client';
import { AuthenticationService } from '../authentication/authentication.service';
import { BreezeProvider } from './breeze.provider.service';
import { Constants } from '../constants.class';
@Injectable()
export class EntityManagerProvider {
constructor(private provider: BreezeProvider, authenticationService: AuthenticationService) {
NamingConvention.camelCase.setAsDefault();
let ajaxAdapter = <any>config.getAdapterInstance('ajax', 'angular');
let headers = ajaxAdapter.defaultSettings['headers'] || {};
headers['Authorization'] = 'Bearer ' + authenticationService._userData.accessToken;
ajaxAdapter.defaultSettings['headers'] = headers;
}
}
&#13;
答案 0 :(得分:2)
我一直在寻找任何在breeze Doc网站上找到这个snipplet的例子:
angular.module('app').run(['$http', function($http) {
var ajax = breeze.config.initializeAdapterInstance('ajax', 'angular');
ajax.setHttp($http); // use the $http instance that Angular injected into your app.
}]);
这个例子适用于angularjs,但帮助我理解替换 对
let ajaxAdapter = <any>config.getAdapterInstance('ajax', 'angular');
更改为
let ajaxAdapter = <any>config.getAdapterInstance('ajax', 'angular');
ajaxAdapter.http = authService;
并删除硬编码的承载线
//let headers = ajaxAdapter.defaultSettings['headers'] || {};
//headers['Authorization'] = 'Bearer ' + authenticationService._userData.accessToken;
//ajaxAdapter.defaultSettings['headers'] = headers;
<强>更新强>
<小时/> 由于angular / http方法使用默认设置,因此我对Breeze的更新默认设置执行了注释行的反转。因此,Bearer身份验证保留在默认设置和自定义http提供程序的其他逻辑人员。
整个逻辑如下:
// Provider replacement
let ajaxAdapter = <any>config.getAdapterInstance('ajax', 'angular');
ajaxAdapter.http = authService;
//Default authentication settings
let headers = ajaxAdapter.defaultSettings['headers'] || {};
headers['Authorization'] = 'Bearer ' + authenticationService._userData.accessToken;
ajaxAdapter.defaultSettings['headers'] = headers;
答案 1 :(得分:0)
与Angular(2/4)相关,我通过观察Leonardo在他的回答以及Ward Bell和团队here:的讨论中如何接近它来找到我需要的大部分工作。
基本上,用您自己的授权版本替换ajax适配器,该版本包含持有者令牌。
Per Ward:
import { AjaxAngular2Adapter } from 'breeze-bridge-angular2'; ...
// assume you injected auth0Http instance when you get here ... config.registerAdapter('ajax', () => new AjaxAngular2Adapter(auth0Http)); config.initializeAdapterInstance('ajax', AjaxAngular2Adapter.adapterName, true); ...
在我的具体情况下,这意味着更改我的实体管理器提供程序并注入我的authService:
if (this.authService.isAuthenticated()) {
config.registerAdapter('ajax', () => new AjaxAngular2Adapter(<any>this.authService.authHttp));
config.initializeAdapterInstance('ajax', AjaxAngular2Adapter.adapterName, true);
}
// Unrelated, but including for context....
let dsconfig: DataServiceOptions = {
serviceName: EntityManagerProvider.serviceName
};
let dataService = new DataService(dsconfig);
let masterManager = EntityManagerProvider._masterManager = new EntityManager({
dataService: dataService
});
我还在努力解决当实体管理器提供商未创建时用户未经过身份验证的情况,但稍后会登录。有一些关于不抛弃jwt的讨论链接中的错误,但我还没有完全弄明白。