简体   繁体   English

PHP Laravel 与 MS SQL Azure 数据库连接具有 Azure Active Directory - 通用 MFA 启用

[英]PHP Laravel with MS SQL Azure database connection having Azure Active Directory - Universal with MFA enables

We have Laravel App having MS SQL Database connection.我们有具有 MS SQL 数据库连接的 Laravel 应用程序。 SQL database is hosted on microsoft azure app service. SQL 数据库托管在 microsoft azure app 服务上。

To connect database with MS SQL using Laravel, we have installed the required PHP drivers for Microsoft SQL Server (Ubuntu) as per instructions given in link.要使用 Laravel 将数据库与 MS SQL 连接,我们已经按照链接中给出的说明安装了 Microsoft SQL Server (Ubuntu) 所需的 PHP 驱动程序。 https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15 https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15

By following steps from above link, our Laravel web APP connection with MS SQL Database was working fine and we were able to perform queries on database.按照上面链接中的步骤,我们的 Laravel Web APP 与 MS SQL 数据库的连接工作正常,我们能够对数据库执行查询。

Now client has enabled Azure Active Directory – Universal with MFA Authentication to access the database.现在客户端已启用 Azure Active Directory – Universal with MFA Authentication 来访问数据库。 So our database connection is not working now.所以我们的数据库连接现在不起作用。 We have below code in .env file to connect SQL database.我们在 .env 文件中有以下代码来连接 SQL 数据库。

DB_CONNECTION_sqlsrv=sqlsrv
DB_HOST_sqlsrv=sql-server-host-name
DB_PORT_sqlsrv=1433
DB_DATABASE_sqlsrv=sql-database-name
DB_USERNAME_sqlsrv= sql-database-username
DB_PASSWORD_sqlsrv= sql-database-password

To verify the database connection, we are trying to connect database from SSMS(Application from window) as below to verify the credentials and connection is working fine there.为了验证数据库连接,我们正在尝试从 SSMS(来自窗口的应用程序)连接数据库,以验证凭据和连接在那里正常工作。

在此处输入图像描述

But our Laravel connection is not working here.但是我们的 Laravel 连接在这里不起作用。 So is there any alternate way or any parameters we have to pass to connect MS SQL database having Azure Active Directory with MFA enabled on Azure App Server?那么是否有任何替代方式或任何参数我们必须传递来连接具有 Azure Active Directory 的 MS SQL 数据库,并在 Azure App Server 上启用了 MFA? Without that authentication enables, database connection is working very fine for us.如果没有启用身份验证,数据库连接对我们来说工作得很好。 Any help would be appreciated.任何帮助,将不胜感激。

After doing some research, we found the solution.经过一番研究,我们找到了解决方案。 We have resolved it by extending the default database connection classes which can accomplish ActiveDirectoryPassword authentication.我们通过扩展可以完成 ActiveDirectoryPassword 身份验证的默认数据库连接类来解决它。 On behalf of solution we have follow below steps.代表解决方案,我们遵循以下步骤。

Step 1:步骤1:

We have created a new folder under app , and copied the following core files over.我们在app下创建了一个新文件夹,并将以下核心文件复制过来。

       ◦ Custom
           ▪ Database
               • Connectors
                   ◦ ConnectionFactory.php
                   ◦ SqlServerConnector.php

These core files can be found in vendor/laravel/framework/src/Illuminate/.这些核心文件可以在 vendor/laravel/framework/src/Illuminate/ 中找到。 The objective here is to extend the base classes, then load the extended class in the Database Service provider.这里的目标是扩展基类,然后在数据库服务提供程序中加载扩展类。 After we have these files copied over, We have edited the files.在我们复制了这些文件之后,我们已经编辑了这些文件。

Step 2:第2步:

Go to your app > Custom > Database > Connectors > ConnectionFactory.php转到您的应用程序 > 自定义 > 数据库 > 连接器 > ConnectionFactory.php

Change the namespace.更改命名空间。

namespace App\Custom\Database\Connectors;

Import the original class so we can extend it.导入原始类,以便我们可以扩展它。

use Illuminate\Database\Connectors\ConnectionFactory as BaseFactory;

class ConnectionFactory extends BaseFactory {

Comment out the original class and replace it with your new extended class.注释掉原始类并将其替换为新的扩展类。

/*use Illuminate\Database\Connectors\SqlServerConnector;*/ use App\Custom\Database\Connectors\SqlServerConnector;

Step 3:第 3 步:

Then Go To app > Custom > Database > Connectors > SqlServerConnector.php然后转到应用程序 > 自定义 > 数据库 > 连接器 > SqlServerConnector.php

In this file, we need to add some extra attributes to the getSqlSrvDsn() method.在这个文件中,我们需要为 getSqlSrvDsn() 方法添加一些额外的属性。

Updated the namespace.更新了命名空间。

namespace App\Custom\Database\Connectors;

Add required classes.添加所需的类。

use Illuminate\Database\Connectors\ConnectorInterface; use Illuminate\Database\Connectors\Connector;

Update getSqlSrvDsn() by adding the following attributes.通过添加以下属性更新 getSqlSrvDsn()。

//Added to support ActiveDirectoryPassword

if (isset($config['authentication'])) { $arguments['Authentication'] = $config['authentication']; }

Step 4:第4步:

After adding the 'Authentication' to the attributes, update you sqlsrv connection settings array in database.php.将“身份验证”添加到属性后,更新 database.php 中的 sqlsrv 连接设置数组。

'authentication' => env('DB_AUTHENTICATION', 'ActiveDirectoryPassword'),

We set the default to be SqlPassword.我们将默认设置为 SqlPassword。 In order to use Active Directory Managed Identities, we can set it to 'ActiveDirectoryPassword' in our .env file为了使用 Active Directory Managed Identities,我们可以在 .env 文件中将其设置为“ActiveDirectoryPassword”

In config/database.php add 'authentication' => 'ActiveDirectoryPassword', and check default connection as it is below :在 config/database.php 添加 'authentication' => 'ActiveDirectoryPassword',并检查默认连接,如下所示:

//'default' => env('DB_CONNECTION', 'mysql'),
'default' => env('DB_CONNECTION', 'sqlsrv'), 

'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL_sqlsrv'),
            'host' => env('DB_HOST_sqlsrv', 'hostname.dbmssql.com'),
            'port' => env('DB_PORT_sqlsrv', '1433'),
            'database' => env('DB_DATABASE_sqlsrv', 'sqldb'),
            'username' => env('DB_USERNAME_sqlsrv', 'SqlUsername'),
            'password' => env('DB_PASSWORD_sqlsrv', 'SqlPassword'),
            'authentication' => env('DB_AUTHENTICATION', 'ActiveDirectoryPassword'),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

Step 5:第 5 步:

Next, create a new DatabaseServiceProvider.php to use our custom ConnectionFactory.接下来,创建一个新的 DatabaseServiceProvider.php 以使用我们的自定义 ConnectionFactory。

php artisan make:provider DatabaseServiceProvider

Copy over the existing DatabaseServiceProvider code located in:复制位于以下位置的现有 DatabaseServiceProvider 代码:

Illuminate\Database\DatabaseServiceProvider

Update the ConnectionFactory class to grab our custom ConnectionnFactory.更新 ConnectionFactory 类以获取我们的自定义 ConnectionnFactory。

/*use Illuminate\Database\Connectors\ConnectionFactory;*/
use App\Custom\Database\Connectors\ConnectionFactory;

Add below package in DatabaseServiceProvider在 DatabaseServiceProvider 中添加以下包

use Illuminate\Database\DatabaseManager;
use Illuminate\Database\DatabaseTransactionsManager;
use Illuminate\Support\ServiceProvider;

Step 6:第 6 步:

Last, in our app/config/app.php file, comment out the existing DatabaseServiceProvider.最后,在我们的 app/config/app.php 文件中,注释掉现有的 DatabaseServiceProvider。

/*Illuminate\Database\DatabaseServiceProvider::class,*/

And add our new DatabaseServiceProvider.并添加我们新的 DatabaseServiceProvider。

App\Providers\DatabaseServiceProvider::class,

By following above steps, connection is working fine and able to access the database from Laravel 8.按照上述步骤,连接工作正常并且能够从 Laravel 8 访问数据库。

you can go immediately to this location to edit vendor\laravel\framework\src\Illuminate\Database\Connectors\SqlServerConnector.php in getSqlSrvDsn() function just add this code您可以立即到此位置编辑 vendor\laravel\framework\src\Illuminate\Database\Connectors\SqlServerConnector.php 在 getSqlSrvDsn() 函数中只需添加此代码

    if (isset($config['authentication'])) {
        $arguments['Authentication'] = $config['authentication'];
      }

and after that, you need to pass the之后,您需要通过

'authentication'=>'ActiveDirectoryMsi'

in your database connection array in config/database.php and should be working fine在 config/database.php 的数据库连接数组中,应该可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM