angular6 - 如何在 angular-oauth2-odic 中将标头内容类型设置为 application/json?
问题描述
我将 Content-Type 标头设置为application/json
fromfetchTokenUsingPasswordFlow
方法,但它是application/x-www-form-urlencoded
. 有没有办法将标题内容类型设置为application/json
?
根据源代码,Content-Type
标头已被硬编码为application/x-www-form-urlencoded
.
我正在为后端使用 Spring Boot Rest 服务,但它不允许application/x-www-form-urlencoded
作为Content-Type
. 请在下面找到示例 Angular 6 代码供您参考:
import { Component, OnInit, Input } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Usermodel } from '../models/usermodel';
import { OAuthService } from 'angular-oauth2-oidc';
import { HttpHeaders } from '@angular/common/http';
@component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
@input() message: any;
@input() apiUrl: any;
@input() params: any;
currentUser: Usermodel;
model: any = {};
loading = false;
returnUrl: string;
headers: HttpHeaders;
constructor(private router: Router,
private route: ActivatedRoute,
private oauthService: OAuthService,
) {
oauthService.tokenEndpoint = "http://localhost:7890/api/login";
oauthService.requireHttps = false;
this.oauthService.setStorage(localStorage);
this.headers = new HttpHeaders().set('Content-Type', 'application/json');
console.log('oauthtoken', this.oauthService.getAccessToken());
}
ngOnInit() {
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
public login() {
this.loading = true;
this.apiUrl = 'login'
console.log("Headers::->" + this.headers)
this.oauthService.fetchTokenUsingPasswordFlow(this.model.userName, this.model.password, this.headers).then((resp) => {
console.log('resp', resp);
});
}
}
解决方案
不久前我在 angular-oauth2-oidc repo 上看到了类似的问题,我将在这里重复我的回复作为答案,以便人们轻松找到。
库硬编码application/x-www-form-urlencoded
,我认为可能是正确的:RFC 6749 似乎规定了这一点:
4.3.2. 访问令牌请求
客户端通过使用“application/x-www-form-urlencoded”格式添加以下参数向令牌端点发出请求...
我有点惊讶您的 Spring Boot 包不支持更改资源所有者密码流的令牌请求端点的可能内容类型,您可以尝试仔细检查吗?
或者,您可以使用适当的 spring-boot 包提出问题?
在这一点上,我看到的唯一最后一个选项(除了不使用库,这对于该流程来说是很有可能的)是分叉库并自己更改内部结构以进行自定义构建。
推荐阅读
- javascript - 将数据从 Node 服务器重定向并发送回 React
- ruby-on-rails - Microsoft SQL Server 的 Rails 5.2.3 架构加载失败
- flatbuffers - 如何让 FlatBufferToString 为联合类型生成有效的 JSON?
- python - Altair:使用 binding_select 时选择所有值
- verilog - 当 32 无法正常工作时,verilog 与门
- mysql-workbench - MySQL-Workbench 连接失败
- java - 带有密码检查器的布尔逻辑循环有问题
- azure - 使用 Xamarin Forms 的 Azure Active Directory B2C 导航失败
- nginx - 如何使用 lua 在 python 中创建列表和附加值
- python - 为 seq2seq 模型拆分训练和验证集