Unable to connect azure SQL with r2dbc spring boot using AD group authentication

Vaibhav Kumar Singh 0 Reputation points
2025-01-24T04:45:36.5133333+00:00

I am trying to connect with azure sql instance using AD group authentication with my spring boot reactive application using r2dbc but it seems there is no way to custom initialize the ConnectionFactory bean in configuration so that I can paas the AD group credential.

When I am trying to connect using username and password , I am getting error Loing failed for user and not able to open server. When trying to create a token using clientId, client secret and tenantId the token is too long for password.
Please let me know if anyone has successfully created custom configuration for this.

Azure SQL Database
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 16,446 Reputation points
    2025-01-24T14:56:09.2966667+00:00

    Hello Vaibhav Kumar Singh,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you're unable to connect azure SQL with r2dbc spring boot using AD group authentication.

    To address the problem more precisely, let's focus on the specific requirements for AD group authentication and handling long tokens. The steps below will guide you to be able to configure your Spring Boot application to connect to Azure SQL using AD group authentication with R2DBC:

    You need to use Azure AD authentication with R2DBC:

    1. Ensure you have the necessary dependencies in your pom.xml similar to the below:
            <dependency>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-identity</artifactId>
                <version>1.3.7</version>
            </dependency>
            <dependency>
                <groupId>io.r2dbc</groupId>
                <artifactId>r2dbc-mssql</artifactId>
                <version>0.8.6.RELEASE</version>
            </dependency>
      
    2. Then, create a custom ConnectionFactory bean that uses Azure AD authentication:
            @Configuration
            public class R2dbcConfig {
                @Bean
                public ConnectionFactory connectionFactory() {
                    AzureCredentialBuilderFactory credentialBuilderFactory = new AzureCredentialBuilderFactory();
                    TokenCredential tokenCredential = credentialBuilderFactory.createDefaultAzureCredentialBuilder().build();
                    return ConnectionFactories.get(ConnectionFactoryOptions.builder()
                        .option(ConnectionFactoryOptions.DRIVER, "mssql")
                        .option(ConnectionFactoryOptions.HOST, "your-server.database.windows.net")
                        .option(ConnectionFactoryOptions.DATABASE, "your-database")
                        .option(ConnectionFactoryOptions.USER, "your-username")
                        .option(ConnectionFactoryOptions.PASSWORD, tokenCredential.getToken(new TokenRequestContext().addScopes("https://database.windows.net/.default")).block().getToken())
                        .option(ConnectionFactoryOptions.PROTOCOL, "r2dbc")
                        .build());
                }
            }
      
    3. Then, if the token is too long, ensure that the token is correctly formatted and truncated if necessary. You might need to adjust the configuration to handle longer tokens or use a different authentication method that supports longer tokens.
    4. There are similar questions to yours on this platform though scenario might be different, you can check the links on using Spring Data R2DBC with Azure SQL Database - https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-data-r2dbc-with-azure-sql-server and Baeldung's guide on integrating Azure AD with Spring Boot - https://www.baeldung.com/spring-boot-azuread-authenticate-users for more detailed examples and discussions.

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.