[英]How to authenticate user with external auth service?
I have a microservice for logging in and user registration.我有一个用于登录和用户注册的微服务。 After launch request on localhost:8080
with body { "username": "test", "password":"test"}
i get an auth token like: { "token":"asdfsadfasdf..." }
,在localhost:8080
上使用 body { "username": "test", "password":"test"}
启动请求后,我得到一个身份验证令牌,例如: { "token":"asdfsadfasdf..." }
,
And i must use this token in another microservice to authenticate user before launching request on my endpoint, And i have code like this:在我的端点上启动请求之前,我必须在另一个微服务中使用这个令牌来验证用户,我有这样的代码:
import { Injectable } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {}
@Get()
@UseGuards(JwtAuthGuard)
private async getHello(): Promise<void> {
console.log("Hello world");
}
@Module({
imports: [
JwtModule.register({
secret: 'secretKey',
}),
JwtStrategy,
PassportModule.register({ defaultStrategy: 'jwt' })
],
providers: [AppService],
controllers: [AppController]
})
export class AppModule{}
after launch my Get
request i get an error:启动我的Get
请求后,我收到一个错误:
[Nest] 10472 - 04.05.2021, 17:14:00 [ExceptionsHandler] Unknown authentication strategy "jwt" +3677ms
Error: Unknown authentication strategy "jwt"
strategy战略
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'secret',
});
}
}
can someone tell me what I'm doing wrong and why my auth strategy is bad?有人可以告诉我我做错了什么以及为什么我的身份验证策略不好?
thanks for any help!谢谢你的帮助!
Move your JwtStrategy
from the imports
to the providers
array.将您的JwtStrategy
从imports
移动到providers
数组。 Providers never go in the imports
array.提供者从不在imports
数组中 go 。 Only modules belong there.只有模块属于那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.