我想主持我的静态网站在AWS S3。但它包含“联系我们”页面,这是用于发送电子邮件。当用户单击submit按钮将请求发送到API网关时,API网关将触发lambda函数;lambda函数将邮件发送给Admin。
但它有一些问题。当我们在服务器上托管网站时,我们可以使用captcha来防止提交请求的自动化和欺诈活动。在这种情况下,任何人都可以误用(通过在URL中传递查询字符串发送更多请求)我的API网关URL。
因此,我的问题是如何知道提交请求是从我的网站请求的--只有和--有什么方法可以代替Captcha吗?
发布于 2017-12-06 14:34:38
谷歌reCAPTCHA看起来很容易整合-
https://developers.google.com/recaptcha/intro破解了-
这可以很容易地通过API网关端点和lambda实现(您的秘密将在lambda中,可能作为env变量,站点密钥将在s3静态站点中)
无论如何,为什么不设计自己的验证机制(如果你真的想自己动手的话:)?询问用户的电话号码,然后再让他们提交(或在提交)。然后调用一个API网关端点,该端点获取这个号码,并通过SNS发送otp。一旦用户验证了这个otp (另一个api网关调用),然后只发送管理电子邮件(做进一步的处理)。您可以存储此otp是一些带有已验证布尔标志的rds。您不必担心这些API网关端点,因为它们实际上只用于身份验证,并且使用云端自动缩放。
发布于 2017-12-07 03:42:33
使用API网关和Lambda验证reCAPTCHA
对于S3静态站点上无服务器SPA站点上的联系人表单,我使用AngularJS和“角- recaptcha”服务(https://github.com/VividCortex/angular-recaptcha),尽管您可以使用任何方法获取和POSTing recaptcha值。
在后端,您可以使用API,它调用Lambda函数来验证recaptcha值并执行一些操作。使用NodeJS和Recaptcha2 (https://www.npmjs.com/package/recaptcha2)验证令牌。
var reCAPTCHA = require('recaptcha2')
module.exports.sendemail = (event, context, callback) => {
// parse the data that was sent from API Gateway
var eventData = JSON.parse(event.body);
// Prepare the recaptcha connection to Google
var recaptcha = new reCAPTCHA({
siteKey: process.env.RECAPTCHA_KEY,
secretKey: process.env.RECAPTCHA_SECRET
})
// Validate the recaptcha value
recaptcha.validate(eventData.recaptcha)
.then(function(){
// validated ok
console.log("ReCaptcha Valid")
... DO STUFF HERE ...
})
.catch(function(errorCodes){
// invalid recaptcha
console.log("ReCaptcha Not Valid")
// translate error codes to human readable text
console.log(recaptcha.translateErrors(errorCodes));
// send a fail message with cors headers back to the UI
var response = {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
},
body: JSON.stringify({"message":"Error: Invalid Recaptcha"})
}
callback(null, response);
});
};使用车把和的模板电子邮件示例
作为奖励,这里有一些代码我可以重用,用于使用html/text模板发送电子邮件:
对我来说很好:)
'use strict';
var AWS = require('aws-sdk');
var ses = new AWS.SES();
var reCAPTCHA = require('recaptcha2')
var fs = require('fs');
var Handlebars = require('handlebars');
module.exports.sendemail = (event, context, callback) => {
// parse the data that was sent from API Gateway
var eventData = JSON.parse(event.body);
// Prepare the recaptcha connection to Google
var recaptcha = new reCAPTCHA({
siteKey: process.env.RECAPTCHA_KEY,
secretKey: process.env.RECAPTCHA_SECRET
})
// Validate the recaptcha value
recaptcha.validate(eventData.recaptcha)
.then(function(){
// validated ok
console.log("reCAPTCHA Valid")
// Read the HTML template from the package root
fs.readFile('./contact/email_template.html', function (err, emailHtmlTemplate) {
if (err) {
console.log("Unable to load HTML Template");
throw err;
}
// Read the TEXT template from the package root
fs.readFile('./contact/email_template.txt', function (err, emailTextTemplate) {
if (err) {
console.log("Unable to load TEXT Template");
throw err;
}
// Gather data to be injected to the templates
var emailData = {
"websiteaddress": process.env.WEBSITEADDRESS,
"websitename": process.env.WEBSITENAME,
"content": null,
"email": process.env.EMAIL_TO,
"event": eventData
};
// Use Handlebars to compile the template and inject values into the title (used in subject and body of email)
var templateTitle = Handlebars.compile(process.env.EMAIL_TITLE);
var titleText = templateTitle(emailData);
console.log(titleText);
// Add title to the values object
emailData.title = titleText;
// Use Handlebars to compile email plaintext body
var templateText = Handlebars.compile(emailTextTemplate.toString());
var bodyText = templateText(emailData);
console.log(bodyText);
// Use Handlebars to compile email html body
var templateHtml = Handlebars.compile(emailHtmlTemplate.toString());
var bodyHtml = templateHtml(emailData);
console.log(bodyHtml);
// Prepare the SES payload
var params = {
Destination: {
ToAddresses: [
process.env.EMAIL_TO
]
},
Message: {
Body: {
Text: {
Data: bodyText,
Charset: 'UTF-8'
},
Html: {
Data: bodyHtml
},
},
Subject: {
Data: titleText,
Charset: 'UTF-8'
}
},
Source: process.env.EMAIL_FROM
}
console.log(JSON.stringify(params,null,4));
// Send SES Email
ses.sendEmail(params, function(err,data){
if(err) {
console.log(err,err.stack); // error
// Handle SES send errors
var response = {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
},
body: JSON.stringify({"message":"Error: Unable to Send Message"})
}
callback(null, response);
}
else {
console.log(data); // success
// SES send was successful
var response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
},
body: JSON.stringify({"message":"Message Sent"})
}
callback(null, response);
}
});
}); //end of load text template
}); //end of load html template
})
.catch(function(errorCodes){
// invalid recaptcha
console.log("reCAPTCHA Not Valid")
// translate error codes to human readable text
console.log(recaptcha.translateErrors(errorCodes));
// send a fail message with cors headers back to the UI
var response = {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
},
body: JSON.stringify({"message":"Error: Invalid Recaptcha"})
}
callback(null, response);
});
};编辑:固定语法错误
发布于 2017-12-06 11:26:10
您可以在http上使用Referer标头。它是由浏览器设置的。
https://stackoverflow.com/questions/47671711
复制相似问题