Typescript Angular6-SpringBoot基本身份验证
我已经安装了NVM1.1.7和Node.js版本8.11.1 现在我已经安装了Typescript Angular6-SpringBoot基本身份验证,typescript,angular6,Typescript,Angular6,我已经安装了NVM1.1.7和Node.js版本8.11.1 现在我已经安装了angular@cli6.1.3版,我用ng new创建了一个角度项目 我正在尝试使用SpringBoot作为服务器进行基本身份验证。我遵循了大量指南,但无法在HttpHeader中设置身份验证参数 如果我写: var headers_object = new HttpHeaders(); headers_object.set('Content-Type', 'application/json'); headers_o
angular@cli
6.1.3版,我用ng new
创建了一个角度项目
我正在尝试使用SpringBoot
作为服务器进行基本身份验证。我遵循了大量指南,但无法在HttpHeader
中设置身份验证参数
如果我写:
var headers_object = new HttpHeaders();
headers_object.set('Content-Type', 'application/json');
headers_object.set('Authorization', 'Basic ' + btoa('guest:guest1234'));
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
console.log(httpOptions.headers.get('Content-Type'));
然后:
console.log(headers_object.get('Content-Type'));
它打印null
。。如果我写:
var headers_object = new HttpHeaders();
headers_object.set('Content-Type', 'application/json');
headers_object.set('Authorization', 'Basic ' + btoa('guest:guest1234'));
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
console.log(httpOptions.headers.get('Content-Type'));
它打印应用程序/json
。如果我检查HttpHeaders
code,我可以清楚地看到set
方法
在我的package.json
中,我看到:
"@angular/animations": "^6.1.0",
"@angular/common": "^6.1.0",
"@angular/compiler": "^6.1.0",
"@angular/core": "^6.1.0",
"@angular/forms": "^6.1.0",
"@angular/http": "^6.1.0",
"@angular/platform-browser": "^6.1.0",
"@angular/platform-browser-dynamic": "^6.1.0",
"@angular/router": "^6.1.0",
"core-js": "^2.5.4",
"rxjs": "6.0.0",
"zone.js": "~0.8.26"
怎么了
--------------------------更新-----------------------
我按如下方式发送(我想..)Header参数:
let httpHeaders = new HttpHeaders()
.set('Authorization', 'Basic ' + btoa('guest:guest1234'))
.set('Content-Type', 'application/json');
console.log(httpHeaders.get('Content-Type')); // print correct
const httpOptions = {
headers: httpHeaders
};
console.log(httpOptions.headers.get('Content-Type')); // print correct
return this.http.get('http://localhost:8080/hello', httpOptions); // return 401!!
我真的为这件蠢事发疯了。。。PS:Cors被Chrome扩展禁用。。和邮递员一起到达服务器…解决了。。问题是服务器端。。我忘了:
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
内部Spring安全配置class。不管怎样,有了邮递员,即使没有上面的代码,我仍然可以到达服务器。。我仍然对此表示怀疑:
var headers_object = new HttpHeaders();
headers_object.set('Content-Type', 'application/json');
headers_object.set('Authorization', 'Basic ' + btoa('guest:guest1234'));
console.log(headers_object.get('Content-Type')); // printing null
我是Angular的新手,我将在Typescript 2.9
文档中搜索它
任何帮助(或文档提示)都将不胜感激