Access token authentication
- Last Updated: May 14, 2024
- 1 minute read
- DataDirect Connectors
- JDBC
- Microsoft SQL Server 6.0
- Documentation
The driver supports access token authentication with a token obtained from
the Microsoft Entra ID (Azure Active Directory). The driver sets the AuthenticationMethod
property to
auto, if an access token is specified.
Configuring access token authentication is recommended using a Properties or DataSource object only. The
driver uses the SSL value for EncryptionMethod property
when authenticating via access token.Note: If an access token is specified, it will take precedence over other
authentication methods.
An access token can be generated using MSAL4J or external libraries.
Alternatively, a token can be generated using the driver class
SQLServerMSAL4JUtil as shown in the following example.
import com.ddtek.jdbc.sqlserver.SQLServerMSAL4JUtils;
String spn="https://database.windows.net/";
String stsurl="https://login.microsoftonline.com/stsurl";// Replace with your STS URL.
String clientId="ab123c45-def6-7gm2345no67891";// Replace with your client ID.
String clientSecret="12a3=bCD/Gh4Ijk+Lm7qR8s=//TuV+Wd";// Replace with your client secret.
String accessToken="";
SQLServerMSAL4JUtils adal4jUtils=new SQLServerMSAL4JUtils();
try {
accessToken=adal4jUtils.generateFedAuthAccessToken(
spn,
stsurl,
clientId,
clientSecret.toCharArray());}
catch (Exception exception)
{
System.err.println("exception in access token generation:"+exception.getMessage());
}The following example shows the configuration of access token authentication using a datasource object.
SQLServerDataSource ds=new SQLServerDataSource();
ds.setServerName("myserver.windows.net");
ds.setDatabaseName("mydatabase");
ds.setAccessToken(accessToken);
try (Connection connection=ds.getConnection();
Statement stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery("SELECT USER_NAME()"))
{
if (rs.next())
{
System.out.println("You have successfully logged on as: " + rs.getString(1));
}
}
Access Token: access_token
You have successfully logged on as client_id.