后台【两步验证】插件
为了防止网站后台工作人员账号密码意外泄露带来的安全问题(如CSDN事件),可以考虑给后台登录加一个两步验证
一、安装
本插件基于 DolphinPHP 1.0.7 开发
目前测试下来支持 DolphinPHP 1.1.0 !但如果你从低版本升级到 1.1.0 后发现无法登录后台,请先删除 runtime 目录;如果还使用了Redis缓存,需要清空下Redis缓存数据的内容!
一、安装
- 解压附件中的插件到 /plugins 目录
- 修改 application/admin/controller/Admin.php 在 protected function _initialize() 方法的第一行添加代码
/** * 初始化 * @author 蔡伟明 <314013107@qq.com> */ protected function _initialize() { // 两步验证钩子 $hook_result = \think\Hook::listen('two_step'); if (!empty($hook_result) && true !== $hook_result[0]) { $current_url = request()->url(); // 当前 url if ( // 下面三个 URL 不跳转到两步验证页 strpos ( $current_url , plugin_url('TwoStep/Admin/verification') ) === false && // 两步验证页 strpos ( $current_url , plugin_url('TwoStep/Admin/signin') ) === false && // 验证两步验证码 strpos ( $current_url , url('user/publics/signout') ) === false // 登出账号 ){ $this->redirect(plugin_url('TwoStep/Admin/verification')); } } . . . . . }
- 修改 application/admin/controller/Index.php 用下面代码覆盖 public function profile() 方法
/** * 个人设置 * @author 蔡伟明 <314013107@qq.com> */ public function profile() { // 保存数据 if ($this->request->isPost()) { $data = $this->request->post(); $data['nickname'] == '' && $this->error('昵称不能为空'); $data['id'] = UID; // 如果没有填写密码,则不更新密码 if ($data['password'] == '') { unset($data['password']); } $UserModel = new UserModel(); if ($user = $UserModel->allowField(['nickname', 'email', 'password', 'mobile', 'avatar'])->update($data)) { // 记录行为 action_log('user_edit', 'admin_user', UID, UID, get_nickname(UID)); $this->success('编辑成功'); } else { $this->error('编辑失败'); } } // 获取数据 $info = UserModel::where('id', UID)->field('password', true)->find(); // 开启两步验证功能 $generate_secret_url = plugin_url('TwoStep/Admin/generateSecret'); $js = <<<EOF <script type="text/javascript"> $(function(){ $("#is_two_step").change(function() { if($('#is_two_step').is(':checked')) { $.get("{$generate_secret_url}", function(res){ if(res.code == 0){ Dolphin.notify(res.msg, 'danger'); $("#is_two_step").removeAttr("checked"); } else { $("#two_step_secret").val(res.secret); $("#form_group_is_two_step > .col-sm-12").after("<img src='"+res.QRCode+"' id='QRCode' alt='QRCode'>"); } }); } else { $("#two_step_secret").val(''); $("#QRCode").remove(); } }); }); </script>EOF; $items = [ // 批量添加表单项 ['static', 'username', '用户名', '不可更改'], ['text', 'nickname', '昵称', '可以是中文'], ['text', 'email', '邮箱', ''], ['password', 'password', '密码', '必填,6-20位'], ['text', 'mobile', '手机号'], ['image', 'avatar', '头像'] ]; if (isset($info['two_step_secret'])){ // 两步验证插件已安装 if ($info['two_step_secret']){ array_push ( $items , ['switch', 'is_two_step', '两步验证', '关闭后再开启可重新生成二维码', 1] ); }else{ array_push ( $items , ['switch', 'is_two_step', '两步验证', '开启后用 <code>Google Authenticator</code> 扫描二维码', 0] ); } array_push ( $items , ['hidden', 'two_step_secret'] ); }else{ $js = ''; } // 使用ZBuilder快速创建表单 return ZBuilder::make('form') ->addFormItems($items) ->setFormData($info) // 设置表单数据 ->setExtraJs($js) ->fetch(); }
- 到后台“扩展中心”安装“两步验证”插件
- 安装后再“设置插件”,把主机名改成自己的
- 开启非“超级管理员”的插件运行权限
- 打开“个人设置”页
- 拖到页面底部,开启两步验证
- 开启后立即用 Google Authenticator 扫描二维码(不过我还是推荐使用 Authy 这款APP,即使刷机也不怕验证码丢失)
- 确定扫描成功后再提交保存!
- 退出后台重新登录,在系统验证完账号密码后,如果要登录的用户开启了两步验证,则会再要求输入验证码,验证成功后方可进入后台
- 若要关闭两步验证只需要在“个人设置”页关闭并保存即可
本插件基于 DolphinPHP 1.0.7 开发
目前测试下来支持 DolphinPHP 1.1.0 !但如果你从低版本升级到 1.1.0 后发现无法登录后台,请先删除 runtime 目录;如果还使用了Redis缓存,需要清空下Redis缓存数据的内容!
10 个评论
强
刚刚测试一把,挺好用的,赞一个
非常棒 感谢提供
请问能不能直接做到密码下面,一次性输入~
谢谢大神回复,我这个情况比较特殊,后台数据信息比较重要,避免弱口令,还有安装好了普通会员启用两步验证显示没有权限,超级管理员可以,如何设置权限,找了半天没找到。方便私信个QQ或者微信沟通下吗?
不好意思,没仔细看帖子截图,找到插件没有权限问题了