您好,欢迎访问一九零五行业门户网

ThinkPHP怎么配置路由,让后台隐藏起来!

thinkphp如何隐藏后台?下面本篇文章就来给大家介绍一下thinkphp巧用路由规则隐藏后台的方法,让您的网站更安全!
众所周知,如果thinkphp框架的后台模块名为admin的话,可以直接使用http://域名/admin的方式访问管理员后台,这个访问方式很方便,但也存在很大的安全隐患,黑客很容易就猜到了你的后台,然后进行暴力破解后台。那有什么方法可以解决这个隐患呢?下面我们一起来探讨如何利用路由规则来修改后台路径,防止黑客知道我们的后台入口。网上有很多隐藏后台admin的教程,但真正好用的,还是这个路由规则法。
第一步、后台添加可以修改后台模块名的设置参数1、
2、保存设置的关键代码,如下:
if(request()->ispost()) { $data=input('post.'); //获取系统全部模块名 $system_module = []; foreach (scandir(app_path) as $dir) { if($dir == '.' || $dir == '..') { continue; } if(is_dir(app_path.$dir)) { array_push($system_module, $dir); } } foreach ($data as $key => $vo) { if($key == 'admin_module' && $vo != 'admin' && in_array($vo, $system_module)) { $this->error('后台地址不能与现有系统模块名同名'); } }}
注意事项:
admin_module 是我数据库里保存后台模块名的键app_path 是thinkphp5.0版本的常量,如果是其他版本,请自行修改。第二步、在application/common.php里读取网站的配置信息1、config数据表主要结构如下:
drop table if exists `config`;create table `config` ( `id` int(11) not null auto_increment, `key` varchar(255) default null, `val` text, primary key (`id`)) engine=innodb auto_increment=1 default charset=utf8;
2、sysconfig($name)方法:根据键名获取对应的值
<?phpuse think\cache;use app\common\model\config;/** * 获取或配置系统参数 * @param string $name 参数名称 * @return string */function sysconfig($name) { $config = cache::get('config'); if (empty($config)) { $config = config::column('key,val'); cache::set('config',$config,1800);//缓存30分钟 } return isset($config[$name]) ? $config[$name] : '';}
补充:1、如果只是个人使用的系统,不想那么麻烦的话,也可以直接在config.php里直接添加如下配置:
return [ // +---------------------------------------------------------------------- // | 应用设置 // +---------------------------------------------------------------------- // 后台模块名 'admin_module' => 'myadmin',]
2、然后在项目里直接调用:
$admin_module = config('admin_module');
第三步、路由设置 application/route.php<?phpuse think\route;$route_config = [ 'index'=>'index/index',];//1.获取后台模块$admin_module = sysconfig('admin_module');if ($admin_module == '') { $admin_module = 'admin';}//2.设置后台路由if ($admin_module != 'admin') { $admin_route_config = [ //路由禁止:原理是把它指到非登陆地址,在没有登陆情况下,跳转到404页面; 'admin/$' => 'admin/login/jump', 'admin/login$' => 'admin/login/jump', 'admin/login/index' => 'admin/login/jump', $admin_module . '/$' => 'admin/login/index', ]; $route_config = array_merge($route_config, $admin_route_config);}return $route_config;
第四步、在登录控制器login.php里添加跳转验证的jump()方法1、这个jump()方法实际上就是我们第三步禁止路由的指定方法
public function jump() { if(!session::has('uid')) { $request = request::instance(); if(sysconfig('admin_module') == 'admin' || sysconfig('admin_module') == '') { $this->redirect('@admin/login/index'); } else { header("http/1.1 404 not found"); return $this->fetch(app_path.'/404.html'); } } else { $this->redirect('@admin/index/index'); }}
2、上面jump()里的代码,实现功能只有一个,那就是在未登录的情况下,访问被禁止的路由,会跳转到404页面,如下:
3、404.html页面放到application目录,代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>很抱歉,此页面暂时找不到!</title><style type="text/css">body {margin: 0px; padding:0px; font-family:"微软雅黑", arial, "trebuchet ms", verdana, georgia,baskerville,palatino,times; font-size:16px;}div{margin-left:auto; margin-right:auto;}a {text-decoration: none; color: #1064a0;}a:hover {color: #0078d2;}img { border:none; }h1,h2,h3,h4 {/* display:block;*/ margin:0; font-weight:normal; font-family: "微软雅黑", arial, "trebuchet ms", helvetica, verdana ; }h1{font-size:44px; color:#0188de; padding:20px 0px 10px 0px;}h2{color:#0188de; font-size:16px; padding:10px 0px 40px 0px;}#page{width:910px; padding:20px 20px 40px 20px; margin-top:80px;}.button{width:180px; height:28px; margin-left:0px; margin-top:10px; background:#009cff; border-bottom:4px solid #0188de; text-align:center;}.button a{width:180px; height:28px; display:block; font-size:14px; color:#fff; }.button a:hover{ background:#5bbfff;}</style></head><body><div id="page" style="border-style:dashed;border-color:#e4e4e4;line-height:30px;"> <h1>抱歉,找不到此页面~</h1> <h2>sorry, the page you're trying to find has moved. </h2> <font color="#666666">你请求访问的页面,暂时找不到!</font><br /><br /> <div class="button"> <a href="javascript:;" onclick="javascript :history.back(-1);" title="返回上一页">返回上一页</a> </div></div></body></html>
4、退出登录的方法
public function logout() { if(session::has('adminid')) { session::delete('adminid'); } $this->redirect(url('@'.sysconfig('admin_module')));}
原文地址:https://juejin.cn/post/6981428649765371940
更多编程相关知识,请访问:编程入门!!
以上就是thinkphp怎么配置路由,让后台隐藏起来!的详细内容。
其它类似信息

推荐信息