2

这将是一个很长的问题。

为了使 Windows 身份验证与 Angular 一起使用,我为 http 调用提供了包装器,如下所示

import { Injectable } from '@angular/core';
import { Http, Response, RequestOptions, Headers, RequestOptionsArgs } from '@angular/http';
import { Config } from '../_helpers/config';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class HTTPUtility {
    public baseApiUrl: string;
    constructor(private http: Http, private config: Config) {
        this.baseApiUrl = this.config.getByKey('baseApiUrl') || '';
    }

    public getApiUrl(url) {
        return this.baseApiUrl + url;
    }

    public get(url: string, options?: RequestOptions) {
        if (!options) {
            options = this.getDefaultHeaders();
        }
        return this.http.get(this.getApiUrl(url), options)
            .catch(this.handleError);
    }

    private getDefaultHeaders() {
        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
        headers.append('Accept', 'application/json');
        return new RequestOptions({ headers, withCredentials: true });
    }

    public handleError(response: Response) {
        return Observable.throw(JSON.parse(response.json().Message) || 'Server error');
    }
}

如果您观察到new RequestOptions({ headers, withCredentials: true });允许浏览器向服务器发送凭据以进行 Windows 身份验证。它对一切都很好。

现在谈到这个问题,我sampleComponent正在使用其中ServerDataSource,如下所示:

import { Component, OnInit, NgZone } from '@angular/core';
import { Http, RequestOptions } from '@angular/http';
import { Ng2SmartTableModule, ServerDataSource } from 'ng2-smart-table';

@Component({
selector: 'criteria',
templateUrl: './criteria.component.html',
styles: [require('./criteria.scss')],
})

export class SampleComponent implements OnInit {

    Source: ServerDataSource;
    settings: any;
    constructor(
    private http: Http) {
    this.Source = new ServerDataSource(http, { endPoint: 'https://xxxxxx.org/yyy/GetCriterias'});
   }

     ngOnInit(): void {
        // this.settings = {}// assigning the settings.
    }
}

如您所见,ServerDataSource 正在接受 Http 实例,我已经检查了那里的文档,但没有找到任何传递给 RequestOptions 的方法。因此,ng2-smart-table 发出的 Web api 调用失败,状态为 401,因为凭据未通过。

为了解决这个问题,我直接将 ng2-smart-table 源文件更改为特定的“server.data-source.js”,这是更改

ServerDataSource.prototype.createRequestOptions = function () {
        var requestOptions = {withCredentials : true}; // this where I have added the withCredntials flag
        requestOptions.params = new URLSearchParams();
        requestOptions = this.addSortRequestOptions(requestOptions);
        requestOptions = this.addFilterRequestOptions(requestOptions);
        return this.addPagerRequestOptions(requestOptions);
    };

通过此更改,到目前为止一切正常。但是我将来可能会遇到问题,如果我们在这种情况下升级软件包,我必须再次进行更改。

因此,如果有人可以帮助我以其他方式解决问题,请告诉我。

链接:https ://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/server/advanced-example-server.component.ts

谢谢。

4

0 回答 0