【问题标题】:Profile Page Unauthorized (GET 401)个人资料页面未经授权 (GET 401)
【发布时间】:2019-08-14 03:25:24
【问题描述】:

我正在尝试创建一个普通的应用程序,并且我正在使用 JWT 令牌进行身份验证。

用户配置文件的获取请求显示未经授权。但是当我通过邮递员发送 JWT 令牌时,它得到了授权。我认为我的前端 Angular 代码有问题。

我尝试控制台日志 this.authTokenStudentthis.loadToken() 这两个令牌正是同样,当我将该令牌复制到邮递员时,它会获得授权。

这是我的 auth.service.ts 文件:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, Subject } from 'rxjs';
import 'rxjs/add/operator/map';

interface data{
  success: boolean;
  msg: string;
  token: string;
  user: Object;
  teacher: any;
}


@Injectable({
  providedIn: 'root'
})
export class AuthService {

  authTokenStudent: any;
  authTokenTeacher: any;
  user: any;
  teacher: any;

  constructor(private http: HttpClient) { }

  authenticateUser(user){
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    return this.http.post<data>('http://localhost:3000/users/authenticate/student', user, {headers: headers})
    .map(res => res);
  }

  getStudentProfile() {
    let headers = new HttpHeaders();
    this.authTokenStudent = this.loadToken();
    headers.append('Authorization', this.authTokenStudent);
    headers.append('Content-Type', 'application/json');
    return this.http.get<data>('http://localhost:3000/users/profile/student', {headers: headers})
    .map(res => res);
  }
  loadToken(){
    const studentToken = localStorage.getItem('student_id_token');
    return studentToken;
  }
}

这是我的 profile.component.ts 文件:

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {

  user: Object;

  constructor(
    private authService: AuthService,
    private router: Router,
  ) { }

  ngOnInit() {
    this.authService.getStudentProfile().subscribe(profile => {
      this.user = profile.user;
      console.log(profile);
    },
    err => {
      console.log(err);
      return false;
    });
  }

}

auth.service.ts 中的身份验证用户正常工作并将用户登录,但由于错误:GET request 401 Unauthorized,无法显示配置文件信息。

【问题讨论】:

    标签: node.js angular rest


    【解决方案1】:

    我认为您需要添加“承载”+令牌以进行授权

    像这样:

    const httpOptions = {
      headers: new HttpHeaders ({
        'Authorization': `Bearer ${this.authTokenStudent}`,
        'Content-Type': 'application/json'
      }),
    };
    
    return this.http.get<data>('http://localhost:3000/users/profile/student', httpOptions).map(res => res); 
    

    【讨论】:

    • 抱歉,我把它改成了正确的大小写。请确保标头实际上是在您的浏览器中发送的
    • 也试过了,还是不行。以及如何确保标题实际上是在我的浏览器中发送的?
    • CTRL-SHIFT-I -> 浏览器中的开发者控制台。那里有某种形式的网络选项卡。如果您单击请求(如果有),您应该找到详细信息
    • 登录的 POST 请求,Response 说 success: true 同一个 token,但是 GET 请求说是未授权的。
    • 它现在可以工作了,非常感谢您的帮助。这就是我所做的:getStudentProfile(){ const httpOptions = { headers: new HttpHeaders ({ 'Authorization': this.loadToken(), 'Content-Type': 'application/json' }), };返回 this.http.get ('localhost:3000/users/profile/student', httpOptions).map(res => res); }
    猜你喜欢
    • 1970-01-01
    • 2017-12-20
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 2018-05-19
    相关资源
    最近更新 更多