services | platforms | author | level | client | service | endpoint |
---|---|---|---|---|---|---|
active-directory |
Java |
davmhelm |
300 |
Java console daemon |
azure-storage |
AAD V1 |
This sample demonstrates a Java console application calling Azure storage using Azure Active Directory RBAC.
- The Java web application uses the Active Directory Authentication Library for Java (ADAL4J) to obtain a JWT access token from Azure Active Directory (Azure AD)
- The access token is used as a bearer token to authenticate the user when calling Azure Storage.
This sample shows how to build a Java daemon app (confidential client) that uses OpenID Connect to authenticate against a single Azure Active Directory (Azure AD) tenant using ADAL4J. For more information about how the protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
To run this sample, you'll need:
- Working installation of Java and Maven
- An Internet connection
- An Azure Subscription. If you don't have an Azure subscription, create a free account before you begin.
- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see How to get an Azure AD tenant
- A service principal in your Azure AD tenant.
To successfully use this sample, you need a working installation of Java and Maven.
From your shell or command line:
git clone https://github.com/davmhelm/storage-blobs-java-v10-quickstart.git
You will be creating a Service Principal
account for your app. To register the project, you can:
As a first step you'll need to:
- Sign in to the Azure portal.
- On the top bar, click on your account, and then on Switch Directory.
- Once the Directory + subscription pane opens, choose the Active Directory tenant where you wish to register your application, from the Favorites or All Directories list.
- Click on All services in the left-hand nav, and choose Azure Active Directory.
In the next steps, you might need the tenant name (or directory name) or the tenant ID (or directory ID). These are presented in the Properties of the Azure Active Directory window respectively as Name and Directory ID
- In the Azure Active Directory pane, click on App registrations and choose New registration.
- Enter a friendly name for the application, for example
Storage-Quickstart-Java-Application
. - For Supported Account Types leave at the default
Accounts in this organizational directory only
. - For the Redirect URI, select
Web
as the type, and enterhttps://localhost
. - Click Register to create the application.
- In the succeeding page, Find the values for Application (client) ID and Directory (tenant) ID and copy them down. You'll need them to configure the properties file for this project.
- Configure an Application Key (Secret) for your application.
- Click on Certificates & Secrets.
- Under Client secrets click + New Client Secret. Enter a description and leave the default expiration of 1 year, then click
Add
. - Copy the client secret value you just created to the same location as the Application (client) ID and Directory (tenant) ID.
- Configure Permissions for your application.
- Click on API permissions, and click + Add a Permission.
- Under Select an API click APIs my organization uses.
- Type
Azure Storage
in the search box. Then, click on Delegated Permissions and select user_impersonation. - Click on Add Permissions.
Create a new general-purpose storage account to use for this tutorial.
- Go to the Azure portal and log in using your Azure account.
- On the Hub menu, select New > Storage > Storage account - blob, file, table, queue.
- Enter a name for your storage account. The name must be between 3 and 24 characters in length and may contain numbers and lowercase letters only. It must also be unique. Make note of this name for later.
- Set
Deployment model
to Resource manager. - Set
Account kind
to General purpose (v2). - Set
Performance
to Standard. - Set
Replication
to Locally Redundant storage (LRS). - Leave
Secure transfer required
at the default value Enabled. - Select your subscription.
- For
resource group
, create a new one and give it a unique name. - Select the
Location
to use for your storage account. - Click Create to create your storage account.
In the storage account you just created:
- From the left-nav bar, click Access control (IAM).
- Click + Add > Add role assignment.
- In the Role field, type
Storage Blob Data Contributor
. - Leave Assign access to set to
Azure AD user, group, or service principal
. - In the Select field, type the name of the App Registration you created in Step 3 above, ex
Storage-Quickstart-Java-Application
and select the member from the filtered list. - Click Save. These settings changes can take up to 5 minutes to take effect.
This Quickstart uses a Java properties key/value pair file in order to connect to Azure AD and the Azure Storage account at runtime. An example properties file is included at secrets/azureauth.properties.template
.
It is recommended to keep the populated file in a safe place, and ensure it does not get uploaded into source control (using .gitignore
for example).
With the exception of authorityURL, fill in the rest of the fields in the file with values taken from the instructions above.
If anything you're filling in uses a special character like :
or =
, make sure to escape it first using the escape character \
(ex. :
becomes \:
).
# your Azure AD Tenant ID 'aaaa0000-bbbb-1111-cccc-2222dddd3333'
tenantId=
# your Service principal client ID 'aaaa0000-bbbb-1111-cccc-2222dddd3333'
clientId=
# your Service principal secret (AKA client secret, auth key, etc.)
clientSecret=
# Leave this alone unless you're on a different cloud from Commercial
authorityUrl=https\://login.microsoftonline.com
# Storage account name (just the name, app takes care of the rest of the URI)
storageAccountName=
From your shell or command line:
export STORAGE_QUICKSTART_AUTH_PROPERTIES="/path/to/azureauth.properties"
set STORAGE_QUICKSTART_AUTH_PROPERTIES="<driveletter>:\path\to\azureauth.properties"
From your shell or command line:
at this point you can build this application using maven: mvn package
. Then, run the package using java -jar <packageFile>
. It creates its own file to upload and download, and then cleans up after itself by deleting everything at the end.
mvn package
java -jar target/Quickstart-jar-with-dependencies.jar
mvn package
java -jar target\Quickstart-jar-with-dependencies.jar
- Azure Storage SDK v10 for Java
- Azure Storage Java SDK Reference
- Authenticate with Azure Active Directory to Azure Blob Storage
- Manage access to Azure Resources using RBAC
The information contained in this quickstart and any accompanying materials (including, but not limited to, scripts, sample code, etc.) are provided "AS-IS" and "WITH ALL FAULTS." Any estimated pricing information is provided solely for demonstration purposes and does not represent final pricing and Microsoft assumes no liability arising from your use of the information. Microsoft makes NO GUARANTEES OR WARRANTIES OF ANY KIND, WHETHER EXPRESSED OR IMPLIED, in providing this information, including any pricing information.