Is it possible to connect to Microsoft Azure SQL Server database with Active Directory authentication using Database Toolbox R2020b?

27 Ansichten (letzte 30 Tage)
Is it possible to connect to Microsoft Azure SQL Server database with Active Directory authentication using Database Toolbox R2020b?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 19 Jul. 2021
Bearbeitet: MathWorks Support Team am 19 Jul. 2021
As of R2020b, there is no direct specific built-in support for Active Directory Authentication in Database Toolbox; nevertheless it should still be able to work with these in MATLAB.
In order to connect to Microsoft Azure SQL Server using Azure Active Directory authentication, you will need MS SQL JDBC Driver 6.0 or newer; the examples below are based on version 7.0 (as of MS SQL JDBC driver version 9.1, dependencies of the driver changed and you would need MSAL libraries rather than ADAL for authentication).
ActiveDirectoryIntegrated | Instructions for Windows
Note:
Instructions for Linux can be found after the "ActiveDirectoryPassword" approach section below.
For ActiveDirectoryIntegrated authentication, simply load the JDBC-driver and make sqljdbc_auth.dll available on the Java Library Path, in the same way as in which you would setup for "local" Microsoft SQL Server with integrated security, see:
http://www.mathworks.com/help/database/ug/microsoft-sql-server-jdbc-windows.html#bt5dht6
Then to connect to the database using:
conn = database('myDatabasse','','',...
'com.microsoft.sqlserver.jdbc.SQLServerDriver',...
['jdbc:sqlserver://myServer.database.windows.net:1433;' ...
'encrypt=true;trustServerCertificate=false;' ...
'hostNameInCertificate=*.database.windows.net;' ...
'loginTimeout=30;authentication=ActiveDirectoryIntegrated;database='])
Where you need to update "myDatabase" with the actual database name and "myServer" with the actual server address.
ActiveDirectoryPassword (All platforms)
For ActiveDirectoryPassword the setup is more complicated.
1. Add the Microsoft SQL JDBC Driver as well as the adal4j package which it requires for ActiveDirectoryPassword plus all their dependencies to the front of your static Java Class Path by creating a javaclasspath.txt in your preferences directory:
>> edit(fullfile(prefdir,'javaclasspath.txt'))
And listing all required files in there prepended by the "<before>" tag, for example:
<before>
c:\full\path\to\accessors-smart-1.2.jar
c:\full\path\to\activation-1.1.jar
c:\full\path\to\adal4j-1.6.3.jar
c:\full\path\to\asm-5.0.4.jar
c:\full\path\to\commons-codec-1.11.jar
c:\full\path\to\commons-lang3-3.5.jar
c:\full\path\to\gson-2.8.0.jar
c:\full\path\to\javax.mail-1.6.1.jar
c:\full\path\to\jcip-annotations-1.0-1.jar
c:\full\path\to\json-smart-2.3.jar
c:\full\path\to\lang-tag-1.4.4.jar
c:\full\path\to\mssql-jdbc-7.0.0.jre8.jar
c:\full\path\to\nimbus-jose-jwt-6.5.jar
c:\full\path\to\oauth2-oidc-sdk-5.64.4.jar
c:\full\path\to\slf4j-api-1.7.21.jar
See the following related article on the website to learn more about downloading a Java Package plus all its dependencies if you have a Maven dependency reference:
https://www.mathworks.com/matlabcentral/answers/713843-can-i-load-java-classes-into-matlab-r2020b-using-maven
2. Before connecting in MATLAB, make sure to run the following command:
java.lang.System.clearProperty('javax.xml.transform.TransformerFactory')
This reverts the java.xml.transform.TransformerFactory to its default Java setting. MATLAB will normally have overridden this setting which would make third-party Java classes work with an older Saxon Transformer, this is incompatible with adal4j though. So we reset this to the default such that adal4j can use the default Java XML Transformer again.
3. Connect to the database using:
conn = database('myDatabase','user@domain.com','myPassword',...
'com.microsoft.sqlserver.jdbc.SQLServerDriver',...
['jdbc:sqlserver://myServer.database.windows.net:1433;' ...
'encrypt=true;trustServerCertificate=false;' ...
'hostNameInCertificate=*.database.windows.net;' ...
'loginTimeout=30;authentication=ActiveDirectoryPassword;database='])
Where you will need to set the actual database name, username, password and server address.
ActiveDirectoryIntegrated (Linux)
Azure Microsoft SQL Server also supports ActiveDirectoryIntegrated authentication through Kerberos on non-Windows platforms. This works in combination with MATLAB as well:
1. Make sure Kerberos client support is installed on your system (e.g. on Ubuntu install the following packages: "krb5-user libpam-krb5 libpam-ccreds auth-client-config").2. For Kerberos you will likely need unlimited strength cryptography in Java. The Java Runtime environment in MATLAB is typically not configured for this. If unlimited strength cryptography is allowed in your region, you can configure this in MATLAB as follows:a. For MATLAB releases prior to R2018b, download the "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files" from the Oracle website, applicable for the Java version used in MATLAB and apply them to the Jave installation inside MATLAB (found inside the sys/java/jre/glnxa64/jre directory inside your MATLAB installation).b. For MATLAB releases R2018b and newer, edit the file sys/java/jre/glnxa64/jre/lib/security/java.security inside your MATLAB installation, uncomment (remove the # at the front) the line:
#crypto.policy=unlimited
3. Make sure Kerberos is configured correctly and a session is initialized using "kinit", see the following and/or Kerberos documentation for your Linux distribution:https://docs.microsoft.com/en-gb/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-2017#set-kerberos-ticket-on-windows-linux-and-mac
4. Continue with steps 1 and 2 from ActiveDirectoryPassword (All platforms) to add the required libraries to the MATLAB classpath and reset the XML Transformer settings.
5. Connect using:
conn = database('myDatabasse','','',...
'com.microsoft.sqlserver.jdbc.SQLServerDriver',...
['jdbc:sqlserver://myServer.database.windows.net:1433;' ...
'encrypt=true;trustServerCertificate=false;' ...
'hostNameInCertificate=*.database.windows.net;' ...
'loginTimeout=30;authentication=ActiveDirectoryIntegrated;database='])

Weitere Antworten (0)

Produkte


Version

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by