Dcat-Admin如何实现按需使用https呢?

admin 2022-01-11 10:39:44 2058

Dcat 内置一个 强制 https 的 配置 ADMIN_HTTPS 也行纯线上业务还是蛮好用的。但是有很多问题,比如配置繁琐、稍有不慎,后台就无法登录了。当然这些还好,都是比较容易解决的。

比较大的问题就是,如果你是个人开发者,又不想买证书,现在免费证书申请限制也越来越多了,又或者有些特殊业务可能https并不友好,比如咱们的 可爱猫-iHttp 插件,在一些电脑里有时候会出现下载https资源失败的情况,众多Windows版本兼容各不相同。这时候,你一定迫切希望 http 和 https 共存出现。

更大一点的问题,如果你项目比较大,需要使用负载均衡,比如阿里额slb,大家也都知道,往往使用负载均衡,那么只会在负载均衡机器配置ssl,转发后端往往使用80端口,那么这个配置就完全用不了啦!

最后一个我比较膈应的问题,就是不同环境的切换,也很麻烦。。。比如你不可能给你的测试环境、分支环境等等的环境配置证书吧。。。。

废话说完了,直接上代码吧~

1.创建app/helpers.php,内容如下

<?php
/**
 * 帮助函数
 * 因为要覆盖其他三方扩展的帮助函数,所以必须尽早加载
 * 加载方式:/bootstrap/app.php 顶部加载,如下
 * include_once __DIR__.'/../app/helpers.php';
 */

if (! function_exists('admin_url')) {
    /**
     * Get admin url.
     * 重写Dcat方法,目的是为了实现用户按需根据http或者https访问
     * 实现按需https还需要配合在app/Providers/AppServiceProvider.php
     * 里面的boot()里增加一行如下
     * URL::forceScheme(request()->server->get('REQUEST_SCHEME'));
     * 同时废弃 .env里的 ADMIN_HTTPS 配置,若想强制ssl,直接nginx的server里实现即可,如下
     * #HTTP_TO_HTTPS_START
     * if ($server_port !~ 443){
     *   rewrite ^(/.*)$ https://$host$1 permanent;
     * }
     *
     * @param  string  $path
     * @param  mixed  $parameters
     * @param  bool  $secure
     * @return string
     */
    function admin_url($path = '', $parameters =   [], $secure = null)
    {
        if (url()->isValidUrl($path)) {
            return $path;
        }

        $secure = $secure ?: request()->secure();

        return url(admin_base_path($path), $parameters, $secure);
    }
}

2.修改app/Providers/AppServiceProvider.php,内容如下

<?php

namespace App\Providers;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        URL::forceScheme(request()->server->get('REQUEST_SCHEME'));//根据当前请求确定是否使用ssl
    }
}

3.修改public/index.php,内容如下

<?php

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Check If Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is maintenance / demo mode via the "down" command we
| will require this file so that any prerendered template can be shown
| instead of starting the framework, which could cause an exception.
|
*/

if (file_exists(__DIR__.'/../storage/framework/maintenance.php')) {
    require __DIR__.'/../storage/framework/maintenance.php';
}

include_once __DIR__.'/../app/helpers.php';//优先加载自己的帮助函数文件

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/

require __DIR__.'/../vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';

$kernel = $app->make(Kernel::class);

$response = tap($kernel->handle(
    $request = Request::capture()
))->send();

$kernel->terminate($request, $response);

 

这样就齐活了,注释在第一个文件里都写了。。。

最后于 2022-2-23 被admin编辑 ,原因:
可爱猫?Telegram电报群 https://t.me/ikeaimao

社区声明 1、本站提供的一切软件、教程和内容信息仅限用于学习和研究目的
2、本站资源为用户分享,如有侵权请邮件与我们联系处理敬请谅解!
3、本站信息来自网络,版权争议与本站无关。您必须在下载后的24小时之内,从您的电脑或手机中彻底删除上述内容
最新回复 (0)

您可以在 登录 or 注册 后,对此帖发表评论!

返回