Skip to content

Conversation

Copy link

Copilot AI commented Sep 10, 2025

This PR introduces a new Spring Boot project mi-azuresql-spring that demonstrates how to connect to Azure SQL Database using Azure Managed Identity authentication, eliminating the need for password-based authentication.

Key Changes

1. Spring Boot Project Structure

Created a complete Spring Boot application with:

  • Main Application: AzureSqlManagedIdentityApplication.java - Entry point for the Spring Boot application
  • Database Service: DatabaseService.java - Service layer for Azure SQL connectivity with managed identity
  • REST Controller: DatabaseController.java - Exposes endpoints for testing database connectivity
  • Comprehensive Tests: Unit tests with H2 in-memory database for CI/CD validation

2. Maven Configuration (pom.xml)

Added Spring Cloud Azure BOM and dependencies:

<!-- Spring Cloud Azure BOM -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-dependencies</artifactId>
            <version>5.22.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- Azure Spring Cloud starter for managed identity -->
<dependency>
    <groupId>com.azure.spring</groupId>
    <artifactId>spring-cloud-azure-starter</artifactId>
</dependency>

3. Managed Identity Configuration (application.properties)

Configured Azure SQL connection with managed identity authentication:

# Azure SQL with Managed Identity authentication
spring.datasource.url=jdbc:sqlserver://${AZ_DATABASE_SERVER_NAME}.database.windows.net:1433;database=demo;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;authentication=ActiveDirectoryMSI

# Enable managed identity for Spring Cloud Azure
spring.cloud.azure.credential.managed-identity-enabled=true
spring.cloud.azure.credential.client-id=${AZURE_CLIENT_ID}

4. REST API Endpoints

The application provides the following endpoints for testing:

  • GET /api/database/health - Application health check
  • GET /api/database/test-connection - Test Azure SQL connectivity
  • GET /api/database/version - Get database version
  • GET /api/database/test-query - Execute a simple test query

5. Documentation

Comprehensive README.md with:

  • Setup instructions for Azure resources
  • Environment variable configuration
  • API endpoint documentation
  • Deployment guidance for Azure services

Benefits

Security: Eliminates password-based authentication
Azure Native: Uses Azure Managed Identity for seamless authentication
Production Ready: Includes proper error handling, logging, and testing
Easy Testing: REST endpoints for validating connectivity
Documentation: Complete setup and usage instructions

This migration demonstrates best practices for connecting Spring Boot applications to Azure SQL Database using managed identity, providing a secure and maintainable solution for Azure-native applications.

Created from VS Code via the GitHub Pull Request extension.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: richposada <454664+richposada@users.noreply.github.com>
Copilot AI changed the title [WIP] Migrate to Azure Managed Identity for Azure SQL (mi-azuresql-spring) Migrate to Azure Managed Identity for Azure SQL (mi-azuresql-spring) Sep 10, 2025
Copilot AI requested a review from richposada September 10, 2025 20:23
Copilot finished work on behalf of richposada September 10, 2025 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants