首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Nestjs和Google Recaptcha

Nestjs和Google Recaptcha
EN

Stack Overflow用户
提问于 2021-01-13 00:01:30
回答 3查看 729关注 0票数 0

EN

Stack Overflow用户

发布于 2021-04-12 03:18:42

导入HttpModule

代码语言:javascript
复制
import { HttpModule, Module } from '@nestjs/common';

@Module({
    imports: [HttpModule],
    ...
})

然后创建一个服务来验证captcha值

注意::您必须从获取秘密/站点密钥(站点密钥将在客户端使用)

代码语言:javascript
复制
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()
    }
    
}

然后在你的控制器中:

代码语言:javascript
复制
 const value="XXXXX" // client send this for you
 const result = await this.captchService.validate(value)
 if (!result.success) throw new BadRequestException()

客户端

如果你使用的是angular,你可以使用

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65687512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档