Java onprem机器访问Azure Active Directory,以便我们可以访问ActiveDirectoryMSI身份验证以及IMDS服务器
我们希望将AzureSqlServer与ActiveDirectoryMSI身份验证以及基于令牌的身份验证和 我们能够从Azure网络中创建并作为Azure广告组成员添加的VM成功执行。 为此,我们通过以下链接创建了Contained user 并将VM添加为AzureActiveDirectory的一部分 通过以下链接 我们可以使用IMDS服务器访问SQL数据,而无需提供用户名和密码,并且可以使用 及 ActiveDirectorMSI URI字符串jdbc:sqlserver://azuresqlserverNAME:1433;databaseName=数据库名称;身份验证=ActiveDirectoryMsi; 但当涉及到从Onprem Windows/Linux/Mac计算机访问时,我们无法访问Azure SQL serverJava onprem机器访问Azure Active Directory,以便我们可以访问ActiveDirectoryMSI身份验证以及IMDS服务器,java,sql,azure,mssql-jdbc,adal4j,Java,Sql,Azure,Mssql Jdbc,Adal4j,我们希望将AzureSqlServer与ActiveDirectoryMSI身份验证以及基于令牌的身份验证和 我们能够从Azure网络中创建并作为Azure广告组成员添加的VM成功执行。 为此,我们通过以下链接创建了Contained user 并将VM添加为AzureActiveDirectory的一部分 通过以下链接 我们可以使用IMDS服务器访问SQL数据,而无需提供用户名和密码,并且可以使用 及 ActiveDirectorMSI URI字符串jdbc:sqlserver://az
有没有人可以建议我在prem上需要做些什么,以便我们可以使用ActiveDirectoryMSI身份验证以及基于令牌的身份验证访问AzureSqlServer?根据我的测试,如果您想连接Azure SQL本地计算机,请参考以下步骤
com.azure
azure身份
1.0.7
com.microsoft.sqlserver
mssql jdbc
8.2.2.jre8
publicstaticvoidmain(字符串[]args)
{
AccessToken令牌=GetAccessToken();
SQLServerDataSource ds=新SQLServerDataSource();
ds.setServerName(“.database.windows.net”);//替换为您的服务器名。
ds.setDatabaseName(“demo”);//替换为您的数据库名称。
setAccessToken(token.getToken());
try(Connection=ds.getConnection();
语句stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery(“选择SUSER\u SNAME()”){
如果(rs.next()){
System.out.println(“您已成功登录为:”+rs.getString(1));
}
}
}
公共静态AccessToken GetAccessToken(){
DefaultAzureCredential creds=新的DefaultAzureCredentialBuilder()
.build();
TokenRequestContext请求=新的TokenRequestContext();
系统输出打印号(“444”);
request.addScopes(“https://database.windows.net//.default");
字符串标记;
AccessToken AccessToken=creds.getToken(请求).block();
返回accesstoken;
}
根据我的测试,如果要连接Azure SQL本地计算机,请参考以下步骤
com.azure
azure身份
1.0.7
com.microsoft.sqlserver
mssql jdbc
8.2.2.jre8
publicstaticvoidmain(字符串[]args)
{
AccessToken令牌=GetAccessToken();
SQLServerDataSource ds=新SQLServerDataSource();
ds.setServerName(“.database.windows.net”);//替换为您的服务器名。
ds.setDatabaseName(“demo”);//替换为您的数据库名称。
setAccessToken(token.getToken());
try(Connection=ds.getConnection();
语句stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery(“选择SUSER\u SNAME()”){
如果(rs.next()){
System.out.println(“您已成功登录为:”+rs.getString(1));
}
}
}
公共静态AccessToken GetAccessToken(){
DefaultAzureCredential creds=新的DefaultAzureCredentialBuilder()
.build();
TokenRequestContext请求=新的TokenRequestContext();
系统输出打印号(“444”);
request.addScopes(“https://database.windows.net//.default");
字符串标记;
AccessToken AccessToken=creds.getToken(请求).block();
返回accesstoken;
}
据我所知,我们不能在本地使用Azure MSI。根据这种情况,我建议您使用EnvironmentCredential
获取令牌并连接Azure SQL:Hi,@JimXu感谢您的回复。您能告诉我从On-prem机器获取令牌的先决条件是什么吗。我的意思是,在prem上,机器应该是Azure Active Directory的一部分,以访问Azure资源,并且两者应该位于同一网络下。如果你能帮上忙,那对我来说将是一个很大的帮助。你说prem机器上的应该是Azure Active Directory的一部分是什么意思?@JimXu on prem意味着Azure网络的外部,就像你的个人操作系统将其添加为Azure Active Directory的一部分一样。据我所知,我们不能在本地使用Azure MSI。根据这种情况,我建议您使用EnvironmentCredential
获取令牌并连接Azure SQL:Hi,@JimXu感谢您的回复。您能告诉我从On-prem机器获取令牌的先决条件是什么吗。我的意思是,在prem上,机器应该是Azure Active Directory的一部分,以访问Azure资源,并且两者应该位于同一网络下。如果你能帮上忙,那对我来说将是一个很大的帮助。你说prem机器上的应该是Azure Active Directory的一部分是什么意思?@JimXu on prem意味着Azure网络的外部,就像你的个人操作系统将它添加为Azure Active Directory的一部分一样。谢谢@JimXu它为我工作了。我们是否可以使用多台on prem机器,同时在不同的机器中提供相同的环境变量?是的,您可以这样做
az ad sp create-for-rbac -n 'name' --skip-assignment
AZURE_TENANT_ID: ID of the service principal's tenant. Also called its 'directory' ID.
AZURE_CLIENT_ID: the service principal's client ID
AZURE_CLIENT_SECRET: one of the service principal's client secrets
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.2.2.jre8</version>
</dependency>
public static void main( String[] args )
{
AccessToken token= GetAccessToken();
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("<>.database.windows.net"); // Replace with your server name.
ds.setDatabaseName("demo"); // Replace with your database name.
ds.setAccessToken(token.getToken());
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));
}
}
}
public static AccessToken GetAccessToken() {
DefaultAzureCredential creds = new DefaultAzureCredentialBuilder()
.build();
TokenRequestContext request = new TokenRequestContext();
System.out.println("444");
request.addScopes("https://database.windows.net//.default");
String token;
AccessToken accesstoken=creds.getToken(request).block();
return accesstoken;
}