如何在angular-oauth2-odic中将标头内容类型设置为application / json?

时间:2018-09-06 14:21:24

标签: angular6 content-type angular-oauth2-oidc

我正在通过application/json方法将Content-Type标头设置为fetchTokenUsingPasswordFlow,但它将作为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);
    });
  }
}

1 个答案:

答案 0 :(得分:0)

我前段时间看到了similar issue on the angular-oauth2-oidc repo,在这里我将重复我的答复作为答案,以便人们容易找到。

库硬编码application/x-www-form-urlencoded,我认为应该是正确的:RFC 6749 seems to prescribe this

  

4.3.2。访问令牌请求

     

客户端通过使用“ application / x-www-form-urlencoded”格式添加以下参数来向令牌端点发出请求...

让您感到惊讶的是,您的spring boot软件包不支持更改Resource Owner Password流的令牌请求端点的可能的内容类型,您可以尝试仔细检查吗?

或者,您可以使用适当的spring-boot软件包提出问题吗?

此时,我看到的唯一的最后一个其他选择(除了不使用库,对于这种流程是完全可能的)是分叉库并为您的自定义构建自己更改内部。