64 craftcms用户登录安全机制及引入google recaptcha - xiaoxin01/Blog GitHub Wiki
Craft CMS用户登录默认的安全机制是:1小时以内,连续5次用户名密码登录失败,账号锁定5分钟,安全性不高
| 参数 | 说明 | 默认值 | 推荐 |
|---|---|---|---|
| cooldownDuration | 账户锁定持续时间 | 5分钟 | 1天 |
| invalidLoginWindowDuration | 登录失败次数追踪时间区间 | 1小时 | 1天 |
| maxInvalidLogins | 最大登录失败次数 | 5 | 5 |
除了Craft CMS系统的安全设定以外,可以通过引入Google rechaptcha等来提升安全性。
插件地址如下:
https://github.com/aberkie/craft-recaptcha
下载之后,将recaptcha目录复制到craft/plugins下即可
修改文件craft/app/templates/login.html,添加 {{craft.recaptcha.render()}}:
'{{ forms.passwordField({ id: "password", name: "password", placeholder: "Password"|t })|e("js") }}' +
'\{{craft.recaptcha.render()}}' +
'<a id="forgot-password">{{ "Forget your password?"|t }}</a>' +修改文件craft/app/resources/js/login.js,在提交的数据字段添加 g-recaptcha-response
submitLogin: function () {
var data = {
loginName: this.$loginNameInput.val(),
password: this.$passwordInput.val(),
rememberMe: (this.$rememberMeCheckbox.prop('checked') ? 'y' : ''),
'g-recaptcha-response': $('#g-recaptcha-response').val()
};在登录失败之后,调用方法 grecaptcha.reset() 重置rechaptcha:
if (textStatus == 'success') {
if (response.success) {
window.location.href = Craft.getUrl(response.returnUrl);
}
else {
Garnish.shake(this.$form);
this.onSubmitResponse();
// Add the error message
this.showError(response.error);
grecaptcha.reset();
}
}修改完之后将压缩版本复制到:
craft/app/resources/js/compressed/login.js
修改 craft/app/controllers/UsersController.php,添加recaptcha验证
if (craft()->request->isPostRequest())
{
$captcha = craft()->request->getPost('g-recaptcha-response');
$verified = craft()->recaptcha_verify->verify($captcha);
if($verified)
{
//User is a person, not a robot. Go on and process the form!
} else {
//Uh oh...its a robot. Don't process this form!
$this->returnJson(array(
'errorCode' => '1024',
'error' => 'Uh oh...its a robot'
));
return;
}
……