Toggle navigation
首页
技术
骑行
羽毛球
资讯
联络我
登录
Craftcms用户登录安全机制及引入google-recaptcha
2017-10-31
CraftCMS
> Craft CMS用户登录默认的安全机制是:1小时以内,连续5次用户名密码登录失败,账号锁定5分钟,安全性不高 # Craft用户登录相关设定 参数 | 说明 | 默认值 | 推荐 ---|----|-----|--- cooldownDuration | 账户锁定持续时间 | 5分钟 | 1天 invalidLoginWindowDuration | 登录失败次数追踪时间区间 | 1小时 | 1天 maxInvalidLogins | 最大登录失败次数 | 5 | 5 # Google reCaptcha 除了Craft CMS系统的安全设定以外,可以通过引入Google rechaptcha等来提升安全性。 ## 安装插件 craft-recaptcha 插件地址如下: https://github.com/aberkie/craft-recaptcha 下载之后,将recaptcha目录复制到craft/plugins下即可 ## 修改代码将recaptcha引入Admin管理界面 ### login.html 修改文件craft/app/templates/login.html,添加 {{craft.recaptcha.render()}}: ```html '{{ forms.passwordField({ id: "password", name: "password", placeholder: "Password"|t })|e("js") }}' + '\{{craft.recaptcha.render()}}' + '<a id="forgot-password">{{ "Forget your password?"|t }}</a>' + ``` ### login.js 修改文件craft/app/resources/js/login.js,在提交的数据字段添加 g-recaptcha-response ```js 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: ```js 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验证 ```php 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; } …… ``` # 效果: | | | | ------------ | ------------ | |  |  | # 参考: * [cooldownDuration](https://craftcms.com/docs/config-settings#cooldownDuration) * [craft-recaptcha](https://github.com/aberkie/craft-recaptcha)
×
本文为博主原创,如需转载,请注明出处:
http://www.supperxin.com
返回博客列表