简体   繁体   English

如何使用外部身份验证服务对用户进行身份验证?

[英]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.将您的JwtStrategyimports移动到providers数组。 Providers never go in the imports array.提供者从不imports数组中 go 。 Only modules belong there.只有模块属于那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用Google Chrome扩展程序中的Firebase身份验证服务对用户进行身份验证 - Unable to authenticate a user using firebase auth service from google chrome extension 如果用户存在,则仅使用 Google 身份验证提供程序进行身份验证 - Only authenticate with Google Auth Provider if user exists 从外部网站验证Moodle用户 - Authenticate Moodle User from external website 我如何使用 firebase auth 在 nodejs 的服务器端验证用户电话号码 - How can i authenticate user phone number on server side in nodejs with firebase auth 如何在 gatsby 中对用户进行身份验证 - How to authenticate user in gatsby 使用 Firebase Auth 通过小部件对用户进行身份验证 - Authenticate a user from via a widget using Firebase Auth 如何仅授予其Cognito子用户和具有完整AWS特权的安全服务器来认证Cognito用户(安全的后端服务器到服务器身份验证) - How to authenticate a cognito user given only their cognito sub and a secure server with full AWS privileges (secure backend server-to-server auth) 如何使用AWS Cognito SDK使用NodeJS从REST服务验证用户? - How to use AWS Cognito SDK to authenticate user from REST Service using NodeJS? 如何使用上下文 API 对用户进行身份验证? - How to authenticate user with Context API? 流星js中不带服务的用户身份验证 - User auth without service in meteor js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM