nginx 和 nestjs 身份验证部分之间的错误
Error between nginx and nestjs authentication part
我尝试将 NestJS 后端与 Nginx 反向代理一起使用。
我在我的 NestJS 后端编写了一个身份验证部分。
我的问题是,当我在本地模式下使用我的前端/后端时,一切正常。
当我通过 Nginx 使用它时,我总是检索到 401 错误。
我认为这是由于 NestJS 中的 LocalStrategy
这是 local.strategy.ts 文件中的部分
import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from '../auth.service';
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({
usernameField: 'userLogin',
passwordField: 'userPassword',
});
}
async validate(userLogin: string, userPassword: string): Promise<any> {
const user = await this.authService.validateUser(userLogin, userPassword);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
这是 app.controller.ts 文件中的部分
@Public()
@UseGuards(LocalAuthGuard)
@Post('auth/login')
async login(@Request() req) {
return this.authService.login(req.user);
}
但是不知道怎么改。
如果有人有一个例子,那就太好了。
提前致谢。
我已经在服务器上本地使用 curl 进行了测试(没有 nginx)。
我看到了我的错误(mysql 访问测试用户),这不是由于 nginx 配置。
我尝试将 NestJS 后端与 Nginx 反向代理一起使用。 我在我的 NestJS 后端编写了一个身份验证部分。
我的问题是,当我在本地模式下使用我的前端/后端时,一切正常。 当我通过 Nginx 使用它时,我总是检索到 401 错误。 我认为这是由于 NestJS 中的 LocalStrategy
这是 local.strategy.ts 文件中的部分
import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from '../auth.service';
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({
usernameField: 'userLogin',
passwordField: 'userPassword',
});
}
async validate(userLogin: string, userPassword: string): Promise<any> {
const user = await this.authService.validateUser(userLogin, userPassword);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
这是 app.controller.ts 文件中的部分
@Public()
@UseGuards(LocalAuthGuard)
@Post('auth/login')
async login(@Request() req) {
return this.authService.login(req.user);
}
但是不知道怎么改。 如果有人有一个例子,那就太好了。 提前致谢。
我已经在服务器上本地使用 curl 进行了测试(没有 nginx)。 我看到了我的错误(mysql 访问测试用户),这不是由于 nginx 配置。