Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 23ecf4d

Browse files
authored
Merge pull request #19396 from David-Engel/jdbc
Fix JDBC service principal code sample
2 parents 9a4d810 + bd773a7 commit 23ecf4d

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

docs/connect/jdbc/connecting-using-azure-active-directory-authentication.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Connecting using Azure Active Directory authentication
33
description: Learn how to develop Java applications that use the Azure Active Directory authentication feature with the Microsoft JDBC Driver for SQL Server.
44
ms.custom: ""
5-
ms.date: 04/14/2021
5+
ms.date: 05/24/2021
66
ms.reviewer: ""
77
ms.prod: sql
88
ms.prod_service: connectivity
@@ -33,7 +33,7 @@ Connection properties to support Azure Active Directory authentication in the Mi
3333
- **ActiveDirectoryInteractive**
3434
- Supported since driver version **v9.2**, `authentication=ActiveDirectoryInteractive` can be used to connect to an Azure SQL Database/Synapse Analytics using an interactive authentication flow (multi-factor authentication).
3535
- **ActiveDirectoryServicePrincipal**
36-
- Supported since driver version **v9.2**, `authentication=ActiveDirectoryServicePrincipal` can be used to connect to an Azure SQL Database/Synapse Analytics using the client ID and secret of a service principal identity.
36+
- Supported since driver version **v9.2**, `authentication=ActiveDirectoryServicePrincipal` can be used to connect to an Azure SQL Database/Synapse Analytics using the application/client ID and secret of a service principal identity.
3737
- **SqlPassword**
3838
- Use `authentication=SqlPassword` to connect to a SQL Server using userName/user and password properties.
3939
- **NotSpecified**
@@ -326,9 +326,9 @@ public class AADInteractive {
326326
SQLServerDataSource ds = new SQLServerDataSource();
327327
ds.setServerName("aad-managed-demo.database.windows.net"); // Replace with your server name
328328
ds.setDatabaseName("demo"); // Replace with your database
329-
ds.setAuthentication("ActiveDirectoryInteractive");
329+
ds.setAuthentication("ActiveDirectoryInteractive");
330330

331-
// Optional
331+
// Optional login hint
332332
ds.setUser("bob@cqclinic.onmicrosoft.com"); // Replace with your user name
333333

334334
try (Connection connection = ds.getConnection();
@@ -370,17 +370,19 @@ The following example shows how to use `authentication=ActiveDirectoryServicePri
370370
Before building and running the example:
371371

372372
1. On the client machine (on which, you want to run the example), download the [Microsoft Authentication Library (MSAL) for Java](https://github.com/AzureAD/microsoft-authentication-library-for-java) and its dependencies for JDBC Driver 9.1 and above, or [Microsoft Azure Active Directory Authentication Library (ADAL) for Java](https://github.com/AzureAD/azure-activedirectory-library-for-java) and its dependencies for driver versions prior to JDBC Driver 9.1, and include them in the Java build path
373+
373374
2. Locate the following lines of code and replace the server/database name with your server/database name.
374375

375376
```java
376377
ds.setServerName("aad-managed-demo.database.windows.net"); // replace 'aad-managed-demo' with your server name
377378
ds.setDatabaseName("demo"); // replace with your database name
378379
```
379380

380-
3. Locate the following lines of code and replace user name, with the name of the Azure AD user you want to connect as.
381+
3. Locate the following lines of code and replace principalId with the Application ID / Client ID of the Azure AD service principal you want to connect as.
381382

382383
```java
383-
ds.setUser("bob@cqclinic.onmicrosoft.com"); // replace with your user name
384+
String principalId = "1846943b-ad04-4808-aa13-4702d908b5c1"; // Replace with your AAD service principal ID.
385+
String principalSecret = "..."; // Replace with your AAD principal secret.
384386
```
385387

386388
The example to use ActiveDirectoryInteractive authentication mode:
@@ -394,18 +396,15 @@ import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
394396

395397
public class AADServicePrincipal {
396398
public static void main(String[] args) throws Exception{
397-
String principalId = "1846943b-ad04-4808-aa13-4702d908b5c1"; // Replace with your AAD secure principal ID.
399+
String principalId = "1846943b-ad04-4808-aa13-4702d908b5c1"; // Replace with your AAD service principal ID.
398400
String principalSecret = "..."; // Replace with your AAD principal secret.
399401

400402
SQLServerDataSource ds = new SQLServerDataSource();
401403
ds.setServerName("aad-managed-demo.database.windows.net"); // Replace with your server name
402404
ds.setDatabaseName("demo"); // Replace with your database
403-
ds.setAuthentication("ActiveDirectoryServicePrincipal");
404-
ds.setAADSecurePrincipalId(principalId);
405-
ds.setAADSecurePrincipalSecret(principalSecret);
406-
407-
// Optional
408-
ds.setUser("bob@cqclinic.onmicrosoft.com"); // Replace with your user name
405+
ds.setAuthentication("ActiveDirectoryServicePrincipal");
406+
ds.setAADSecurePrincipalId(principalId);
407+
ds.setAADSecurePrincipalSecret(principalSecret);
409408

410409
try (Connection connection = ds.getConnection();
411410
Statement stmt = connection.createStatement();
@@ -421,11 +420,11 @@ public class AADServicePrincipal {
421420
If a connection is established, you should see the following message as output:
422421

423422
```output
424-
You have successfully logged on as: <your user name>
423+
You have successfully logged on as: <your app/client ID>
425424
```
426425

427426
> [!NOTE]
428-
> A contained user database must exist and a contained database user representing the specified Azure AD user or one of the groups the specified Azure AD user belongs to, must exist in the database and must have the CONNECT permission (except for an Azure Active Directory server admin or group)
427+
> A contained user database must exist and a contained database user representing the specified Azure AD principal or one of the groups the specified Azure AD principal belongs to, must exist in the database and must have the CONNECT permission (except for an Azure Active Directory server admin or group)
429428
430429
## Connecting using access token
431430

@@ -486,19 +485,19 @@ public class AADTokenBased {
486485
String clientId = "1846943b-ad04-4808-aa13-4702d908b5c1"; // Replace with your client ID.
487486
String clientSecret = "..."; // Replace with your client secret.
488487

489-
String scope = spn + "/.default";
490-
Set<String> scopes = new HashSet<>();
488+
String scope = spn + "/.default";
489+
Set<String> scopes = new HashSet<>();
491490
scopes.add(scope);
492491

493-
ExecutorService executorService = Executors.newSingleThreadExecutor();
494-
IClientCredential credential = ClientCredentialFactory.createFromSecret(clientSecret);
495-
ConfidentialClientApplication clientApplication = ConfidentialClientApplication
492+
ExecutorService executorService = Executors.newSingleThreadExecutor();
493+
IClientCredential credential = ClientCredentialFactory.createFromSecret(clientSecret);
494+
ConfidentialClientApplication clientApplication = ConfidentialClientApplication
496495
.builder(clientId, credential).executorService(executorService).authority(stsurl).build();
497-
CompletableFuture<IAuthenticationResult> future = clientApplication
496+
CompletableFuture<IAuthenticationResult> future = clientApplication
498497
.acquireToken(ClientCredentialParameters.builder(scopes).build());
499498

500-
IAuthenticationResult authenticationResult = future.get();
501-
String accessToken = authenticationResult.accessToken();
499+
IAuthenticationResult authenticationResult = future.get();
500+
String accessToken = authenticationResult.accessToken();
502501

503502
System.out.println("Access Token: " + accessToken);
504503

0 commit comments

Comments
 (0)