本文阐述使用JDBC接入AZURE SQL 数据库
笔者认为AZURE云上的AZURE SQL和SQL SERVER是很相似的,在普通的账号密码情况下JDBC字符串都是一致的。
下来全部说明官方的多种连接方式,参考官方:
https://learn.microsoft.com/zh-cn/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver16#set-kerberos-ticket-on-windows-linux-and-macos
a.SqlPassword
SQL SERVER验证
使用数据库用户名+密码
String sql="jdbc:sqlserver://xx.database.windows.net:1433;database=dbname;user=user;password=pass;authentication=SqlPassword";
b.ActiveDirectoryPassword
采用Azure AD用户名+密码验证
首先设置AD账号为数据库管理员
然后
String sql="jdbc:sqlserver://xx.database.windows.net:1433;database=db;user=xx@xx.onmicrosoft.com;password=xx;authentication=ActiveDirectoryPassword";
e.ActiveDirectoryMSI
采用AZURE内部得服务器,等验证
先设置服务器为数据库的管理员
再进行请求,MSIClientId是服务器的id,是不必要的
String sql="jdbc:sqlserver://xx.database.windows.net:1433;database=whalead;MSIClientId=xxx;authentication=ActiveDirectoryMSI";
f.ActiveDirectoryServicePrincipal
采用应用和密钥进行验证
先设置应用为数据库的管理员
给应用设置密钥
String sql="jdbc:sqlserver://xx.database.windows.net:1433;database=xxdb;authentication=ActiveDirectoryServicePrincipal;user=cliendid;password=secret;";
g.accessToken
采用使用和密钥去获取token进行验证,步骤和principal一致,代码不一样,不能使用jdbc连接
多了一步要获取
String spn = "https://database.windows.net/";
String stsurl = "https://login.microsoftonline.com/xxxx/oauth2/v2.0/token"; // Replace with your STS URL.
String clientId = "xxx"; // Replace with your client ID.
String clientSecret = "xxx"; // Replace with your client secret.
String scope = spn + "/.default";
Set<String> scopes = new HashSet<>();
scopes.add(scope);
ExecutorService executorService = Executors.newSingleThreadExecutor();
IClientCredential credential = ClientCredentialFactory.createFromSecret(clientSecret);
ConfidentialClientApplication clientApplication = ConfidentialClientApplication
.builder(clientId, credential).executorService(executorService).authority(stsurl).build();
CompletableFuture<IAuthenticationResult> future = clientApplication
.acquireToken(ClientCredentialParameters.builder(scopes).build());
IAuthenticationResult authenticationResult = future.get();
String accessToken = authenticationResult.accessToken();
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!Access Token: " + accessToken);
String sql="jdbc:sqlserver://xxx.database.windows.net:1433;database=xxx;user=xxx;accessToken="+accessToken;
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!Access Token: " + sql);
// Connect with the access token.
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("xxx.database.windows.net"); // Replace with your server name.
ds.setDatabaseName("xxx"); // Replace with your database name.
ds.setAccessToken(accessToken);
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));
}
}
未实践的
c.ActiveDirectoryIntegrated
WINDOWS下是使用mssql-jdbc_auth--.dll 进行验证
Linux下采用
在 Windows、Linux 和 macOS 上设置 Kerberos 票证
d.ActiveDirectoryInteractive
使用时,弹出网页让用户自行三方验证