laowu vor 3 Tagen
Ursprung
Commit
905b47773f

+ 23 - 0
app/Connectors/TrustConnectionFactory.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Connectors;
+
+use Illuminate\Database\Connectors\ConnectionFactory;
+
+/**
+ * 扩展连接工厂,将 sqlsrv 驱动的连接器替换为信任自签名证书版本。
+ */
+class TrustConnectionFactory extends ConnectionFactory
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function createConnector(array $config)
+    {
+        if (($config['driver'] ?? '') === 'sqlsrv') {
+            return new TrustSqlServerConnector;
+        }
+
+        return parent::createConnector($config);
+    }
+}

+ 29 - 0
app/Connectors/TrustSqlServerConnector.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Connectors;
+
+use Illuminate\Database\Connectors\SqlServerConnector;
+
+/**
+ * 信任自签名证书的 SQL Server 连接器
+ * 解决 ODBC Driver 18 强制 TLS + 自签名证书导致的 "certificate verify failed" 错误。
+ *
+ * 在 DSN 末尾追加 ;TrustServerCertificate=1 ,兼容旧版 pdo_sqlsrv (无 SQLSRV_ATTR_TRUST_SERVER_CERTIFICATE 常量)。
+ */
+class TrustSqlServerConnector extends SqlServerConnector
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getDsn(array $config)
+    {
+        $dsn = parent::getDsn($config);
+
+        // 仅在未包含 TrustServerCertificate 时追加
+        if (strpos($dsn, 'TrustServerCertificate') === false) {
+            $dsn .= ';TrustServerCertificate=1';
+        }
+
+        return $dsn;
+    }
+}

+ 7 - 0
app/Providers/AppServiceProvider.php

@@ -33,5 +33,12 @@ class AppServiceProvider extends ServiceProvider
         $this->app->singleton(DingDingRobot::class, function () {
             return new DingDingRobot(env('DINGDING_ACCESS_TOKEN'), env('DINGDING_SECRET'));
         });
+
+        // 仅在测试环境使用信任自签名证书的 SQL Server 连接器 (兼容 ODBC Driver 18)
+        if (env('APP_ENV') === 'testing') {
+            $this->app->singleton('db.factory', function ($app) {
+                return new \App\Connectors\TrustConnectionFactory($app);
+            });
+        }
     }
 }