Setting Up MSAL4J with Maven on RedHat Linux for Azure SQL Active Directory Authentication
When transitioning from on-prem MS SQL to Azure SQL, one of the critical steps is setting up authentication using Microsoft Authentication Library for Java (MSAL4J). This guide provides clear steps for installing and configuring MSAL4J and its dependencies to enable Active Directory (AD) authentication.
Prerequisites
- Java Development Kit (JDK): Ensure you have JDK 8 or above installed.
- To verify, run:
java -version
.
- Apache Maven: Confirm Maven is installed on your system.
- To verify, run:
mvn -v
.
Step 1: Install Maven on RedHat
If Maven is not installed, follow these steps:
sudo yum install maven
mvn -v
Step 2: Understand Maven and MSAL4J Integration
Maven is a dependency management tool, and it automatically fetches libraries like MSAL4J and their dependencies from central repositories.
Do not attempt to directly install msal4j-1.18.0.jar
as a lifecycle phase. This error occurs because Maven expects specific commands like install
, clean
, or package
, rather than a direct file reference.
Step 3: Create a Maven Project
- Create a directory for your project:
mkdir azure-sql-auth
cd azure-sql-auth
- Initialize a Maven project:
mvn archetype:generate -DgroupId=com.example -DartifactId=azure-sql-auth -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd azure-sql-auth
Step 4: Add MSAL4J Dependency
Edit the pom.xml
file in your project directory and add the MSAL4J dependency:
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.18.0</version>
</dependency>
<!-- JDBC Driver for SQL Server -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.2.0.jre8</version>
</dependency>
</dependencies>
Step 5: Build the Project
Run the following command to download and install dependencies:
mvn clean install
Maven will fetch msal4j
, the JDBC driver, and their dependencies, storing them in the local repository (~/.m2/repository
).
Step 6: Add Dependencies to CLASSPATH
Once dependencies are downloaded, you can include them in your application’s CLASSPATH. Locate the JAR files in the .m2
directory:
~/.m2/repository/com/microsoft/azure/msal4j/1.18.0/msal4j-1.18.0.jar
~/.m2/repository/com/microsoft/sqlserver/mssql-jdbc/12.2.0.jre8/mssql-jdbc-12.2.0.jre8.jar
Update your CLASSPATH:
export CLASSPATH=/nfs/data/drivers/java/msal4j/msal4j-1.18.0.jar:/nfs/data/drivers/java/mssql-jdbc/mssql-jdbc-12.2.0.jre8.jar:$CLASSPATH
Step 7: Configure MSAL4J for Authentication
Use MSAL4J in your Java application to perform Azure AD authentication. Below is an example code snippet for integrating MSAL4J with JDBC:
import com.microsoft.aad.msal4j.*;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
public class AzureSQLAuth {
public static void main(String[] args) throws Exception {
// Replace with your Azure AD tenant and client details
String clientId = "<your-client-id>";
String clientSecret = "<your-client-secret>";
String authority = "https://login.microsoftonline.com/<your-tenant-id>/";
ConfidentialClientApplication app = ConfidentialClientApplication.builder(
clientId, ClientCredentialFactory.createFromSecret(clientSecret))
.authority(authority)
.build();
IAuthenticationResult result = app.acquireToken(ClientCredentialParameters.builder(
Collections.singleton("https://database.windows.net/.default"))
.build()).get();
System.out.println("Access Token: " + result.accessToken());
}
}
Step 8: Verify the Connection
Update your JDBC connection string to include the access token:
String connectionUrl = "jdbc:sqlserver://<your-server>.database.windows.net:1433;"
+ "database=<your-database>;"
+ "accessToken=" + result.accessToken();
Troubleshooting Tips
- Dependency Issues: Ensure the Maven Central Repository is accessible. Use
mvn dependency:tree
to verify dependencies. - Network Configuration: Ensure RedHat Linux can access Azure endpoints.
- Logs: Use
-X
with Maven commands to enable debug logs.
By following these steps, you should be able to seamlessly set up MSAL4J and JDBC for Azure SQL authentication on your RedHat Linux server.