ng2-smart-table的ServerDataSource不向web api发送凭据以进行Windows身份验证

时间:2017-10-27 06:17:39

标签: javascript angular typescript authentication ng2-smart-table

这将是一个很长的问题。

对于使用angular的Windows身份验证,我有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

感谢。

0 个答案:

没有答案