发布于 2021-04-12 03:18:42
导入HttpModule
import { HttpModule, Module } from '@nestjs/common';
@Module({
imports: [HttpModule],
...
})然后创建一个服务来验证captcha值
注意::您必须从获取秘密/站点密钥(站点密钥将在客户端使用)
import { HttpService, Inject, Injectable } from "@nestjs/common";
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
import { map } from 'rxjs/operators'
@Injectable()
export class CaptchaService {
constructor(
@Inject(REQUEST) private readonly request: Request,
private httpService: HttpService) { }
public validate(value:string): Promise<any> {
const remoteAddress = this.request.socket.remoteAddress
const secretKey = "XXXXXXXXX"
const url = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + value + "&remoteip=" + remoteAddress;
return this.httpService.post(url).pipe(map(response => {
return response['data']
})).toPromise()
}
}然后在你的控制器中:
const value="XXXXX" // client send this for you
const result = await this.captchService.validate(value)
if (!result.success) throw new BadRequestException()客户端
如果你使用的是angular,你可以使用
https://stackoverflow.com/questions/65687512
复制相似问题