我正在MEAN堆栈上运行的WebApplication上工作(遵循TraversyMedia教程)。但是,我遇到了一个问题,这里提供的所有解决方案似乎都不适合我。每当我尝试访问我的/ profile页面时(除非得到您的授权,该页面都受到保护),我都会得到返回的错误。但是,我已登录并具有正确的访问令牌。
我看了护照jwt git。但是我似乎匹配。
我尝试使用:
ExtractJwt.fromAuthHeaderWithScheme("bearer");
ExtractJwt.fromAuthHeaderWithScheme("bearer");
ExtractJwt.fromAuthHeaderAsBearerToken();
当前使用: ExtractJwt.fromAuthHeaderWithScheme(“ bearer”),因为没有显示出差异。
我已经查看了HttpClientModule,但是我的调用似乎还可以。
passport.js
module.exports = function(passport) {
let opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme("bearer");
opts.secretOrKey = config.secret;
passport.use(
new JwtStrategy(opts, (jwt_payload, done) => {
User.getUserById(jwt_payload.data._id, (err, user) => {
if (err) {
return done(err, false);
}
if (user) {
return done(null, user);
} else {
return done(null, false);
}
});
})
);
};
users.js
const express = require("express");
const router = express.Router();
const passport = require("passport");
const jwt = require("jsonwebtoken");
const config = require("../config/database");
const User = require("../models/user");
...
router.get(
"/profile",
passport.authenticate("jwt", { session: false }),
(req, res, next) => {
res.json({ user: req.user });
}
);
module.exports = router;
auth.service.ts
import { Injectable } from "@angular/core";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { map } from "rxjs/operators";
// Left some unrelated stuff out here
export class AuthService {
authToken: any;
user: any;
constructor(private http: HttpClient) {}
...
getProfile() {
let headers = new HttpHeaders();
this.loadToken();
headers.append("authorization", this.authToken);
headers.append("Content-Type", "application/json");
return this.http
.get("http://localhost:3000/users/profile", {
headers: headers
})
.pipe(map(res => res));
}
...
loadToken() {
const token = localStorage.getItem("id_token");
this.authToken = token;
}
profile.component.ts
export class ProfileComponent implements OnInit {
user: Object;
constructor(private authService: AuthService, private router: Router) {}
ngOnInit() {
this.authService.getProfile().subscribe(
profile => {
this.user = profile.user;
},
err => {
console.log(err);
return false;
}
);
}
}
预计将转到个人资料页面,但我却得到:
GET http://localhost:3000/users/profile 401 (Unauthorized)
HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "Unauthorized", url: "http://localhost:3000/users/profile", ok: false, …}
error: "Unauthorized"
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message: "Http failure response for http://localhost:3000/users/profile: 401 Unauthorized"
name: "HttpErrorResponse"
ok: false
status: 401
statusText: "Unauthorized"
url: "http://localhost:3000/users/profile"
答案 0 :(得分:0)
问题已解决。对于其他遇到相同问题的人,为我解决的是:替换:
let headers = new HttpHeaders();
this.loadToken();
headers.append("authorization", this.authToken);
headers.append("Content-Type", "application/json");
使用:
const headers = new HttpHeaders({
"Content-Type": "application/json",
Authorization: this.authToken
});
this.loadToken();