【Azure Spring Cloud】Azure Spring Cloud connect to SQL using MSI
问题描述
在Azure Spring Cloud中,通过ActiveDirectoryMSI方式来连接到SQL Service,需要如何配置呢?
问题分析
在SQL Service中启用Active Directory MSI认证方式,需要执行两个步骤:
1)在Auzre Spring Cloud App中分配一个Managed Identity。
2)在SQL Service中,使用CREATE USER 命令创建一个Contained User,并且与第一步中的Managed Identity关联。
- 创建映射到 Azure AD 标识的包含的用户:https://docs.microsoft.com/zh-cn/azure/azure-sql/database/authentication-aad-configure?tabs=azure-powershell&view=azuresql#create-contained-users-mapped-to-azure-ad-identities
- CREATE USER [<Azure_AD_principal_name>] FROM EXTERNAL PROVIDER;
完成以上配置后,使用JDBC + ActiveDirectoryMSI的示例代码为:
import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import com.microsoft.sqlserver.jdbc.SQLServerDataSource; public class AAD_MSI { public static void main(String[] args) throws Exception { SQLServerDataSource ds = new SQLServerDataSource(); ds.setServerName("aad-managed-demo.database.chinacloudapi.cn"); // Replace with your server name ds.setDatabaseName("demo"); // Replace with your database name ds.setAuthentication("ActiveDirectoryMSI"); // Optional ds.setMSIClientId("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); // Replace with Client ID of User-Assigned Managed Identity to be used 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)); } } } }
或者使用SQLServerDataSource.setURL()来设置SQL的连接字符串
SQLServerDataSource.setURL(): Sets the URL that is used to connect to the data source.
================================================================================================================= jdbc:sqlserver://aad-managed-demo.database.chinacloudapi.cn:1433;
database=hsp-sql-database1-dev;
authentication=ActiveDirectoryMSI;
msiClientId=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;
encrypt=true;
trustServerCertificate=false;
hostNameInCertificate=*.database.chinacloudapi.cn;
loginTimeout=3000;
=================================================================================================================
参考资料
创建映射到 Azure AD 标识的包含的用户: https://docs.microsoft.com/zh-cn/azure/azure-sql/database/authentication-aad-configure?tabs=azure-powershell&view=azuresql#create-contained-users-mapped-to-azure-ad-identities
Connect using ActiveDirectoryMSI authentication mode:https://docs.microsoft.com/en-us/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver15#connect-using-activedirectorymsi-authentication-mode
setURL Method (SQLServerDataSource):https://docs.microsoft.com/en-us/sql/connect/jdbc/reference/seturl-method-sqlserverdatasource?view=sql-server-ver15
- How to deploy spring cloud microservice on Azure cloud
- 7 Simple Steps to Connect SQL Server Using WCF from SilverLight
- spring-cloud-hystrix之Unable to connect to Command Metric Stream.异常
- SpringCloud之Spring2.0版本Hystrix-Dashboard Unable to Connect to Command Metric Stream解决办法
- SpringCloud testWhileIdle is true, validationQuery not set与java.sql.SQLException: connect error解决方案
- oracle sqlplus connect to cloud database
- 【Azure 应用服务】记一次Azure Spring Cloud 的部署错误 (az spring-cloud app deploy -g dev -s testdemo -n demo -p ./hellospring-0.0.1-SNAPSHOT.jar --->>> Failed to wait for deployment instances to be ready)
- How To Connect Using A Sqlplus Preliminary Connection (Doc ID 986640.1)
- Using C# to connect to and query from a SQL database
- spring-cloud-hystrix之Unable to connect to Command Metric Stream.异常
- php 5.3:connection failed:SQLSTATE[HY000] [2000] mysqlnd cannot connect to MySQL 4.1+ using old auth
- How to connect to another database using sqlplus?
- SQL: How to get a series of dates in sql using connect by
- 7 Simple Steps to Connect SQL Server using WCF from SilverLight
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- org.springframework.remoting.RemoteConnectFailureException: Could not connect to HTTP invoker remote
- Codeigniter出现Unable to connect to your database server using the provided settings错误解决办法
- java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect
- Beginners Guide to SQL Server Integration Services Using Visual Studio 2005
- mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication