如何在没有拦截器的情况下将令牌作为授权标头发送

时间:2019-03-20 14:06:34

标签: angular

userdetail方法返回令牌和我在本地组件中所需的其他信息,并且正确获取了它。但是,令牌需要在someMethod中作为标头发送,因此我在auth服务中调用方法时传递了access_token。现在,在someMethod的auth服务中,调用api时,我需要像使用postman

一样,将它作为带有承载的授权标头发送

本地组件

userDetail: any = null;
    access_token;
      constructor(private auth: AuthService, private activatedRoute: ActivatedRoute) { }
      ngOnInit() {
       this.activatedRoute.data.subscribe((data: any)=> {
       this.elcUserDetail = data.elcUserDetailResolver.body.data;
       this.access_token = this.elcUserDetail.access_token;
       console.log(this.access_token);
       })

    if(this.userDetail != null){
    this.auth.someMethod(this.access_token).subscribe((res)={
    })
   }
 }

身份验证服务

headers: any;

    userdetail(): Observable<any> {
      return this.http.get(this.userdetailURL, {observe: 'response'});
    }

    someMethod(access_token) {
    const headers =  new Headers({ Authorization: `Bearer this.headers`});
   }
    return this.http.post(this.someURL, null,{headers: this.headers } );
    }

用户详细信息解析

export class UserDetailResolveService implements Resolve<any> {
  constructor(private auth: AuthService, private http: HttpClient) { }
  resolve(route: ActivatedRouteSnapshot) {
    return this.auth.userdetail();
  }

0 个答案:

没有答案