So as per documentation in Azure:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
public class AADServicePrincipal {
public static void main(String[] args) throws Exception{
String principalId = "1846943b-ad04-4808-aa13-4702d908b5c1"; // Replace with your AAD service principal ID.
String principalSecret = "..."; // Replace with your AAD principal secret.
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("aad-managed-demo.database.windows.net"); // Replace with your server name
ds.setDatabaseName("demo"); // Replace with your database
ds.setAuthentication("ActiveDirectoryServicePrincipal");
ds.setAADSecurePrincipalId(principalId);
ds.setAADSecurePrincipalSecret(principalSecret);
try (Connection connection = ds.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT SUSER_SNAME()")) {
if (rs.next()) {
System.out.println("You have successfully logged on as: " + rs.getString(1));
}
}
}
}
We can use this to create a Data Source that can connect via AD Service Principal to SQL Server, and insert it as a bean I believe as:
@Bean
@Primary
DataSource dataSource() throws SQLException {
String principalId = "1846943b-ad04-4808-aa13-4702d908b5c1"; // Replace with your AAD service principal ID.
String principalSecret = "..."; // Replace with your AAD principal secret.
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("aad-managed-demo.database.windows.net"); // Replace with your server name
ds.setDatabaseName("demo"); // Replace with your database
ds.setAuthentication("ActiveDirectoryServicePrincipal");
ds.setAADSecurePrincipalId(principalId);
ds.setAADSecurePrincipalSecret(principalSecret);
return ds;
}
But what I am looking for is - Can we configure this whole thing via YAML/application.properties so that it gets auto configured with Data JPA ? If not, can the above process work ?