Skip to content

Commit 45c6328

Browse files
authored
Merge pull request #210927 from MicrosoftDocs/release-cred-free-java
Merge release-cred-free-java to main--scheduled release at 10AM of 9/26
2 parents 4120a35 + 51190af commit 45c6328

17 files changed

+1453
-347
lines changed
-119 KB
Loading
-117 KB
Loading

articles/app-service/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
items:
8383
- name: .NET with SQL DB
8484
href: tutorial-connect-msi-sql-database.md
85+
- name: Java Tomcat with Postgres
86+
href: tutorial-java-tomcat-connect-managed-identity-postgresql-database.md
8587
- name: Azure databases
8688
href: tutorial-connect-msi-azure-database.md
8789
- name: Connect to services

articles/app-service/tutorial-java-quarkus-postgresql-app.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ ms.custom: mvc
1111

1212
# Tutorial: Build a Quarkus web app with Azure App Service on Linux and PostgreSQL
1313

14-
This tutorial walks you through the process of building, configuring, deploying, and scaling Java web apps on Azure.
14+
This tutorial walks you through the process of building, configuring, deploying, and scaling Java web apps on Azure.
1515
When you are finished, you will have a [Quarkus](https://quarkus.io) application storing data in [PostgreSQL](../postgresql/index.yml) database running on [Azure App Service on Linux](overview.md).
1616

17-
![Screenshot of Quarkus application storing data in PostgreSQL.](./media/tutorial-java-quarkus-postgresql/quarkus-crud-running-locally.png)
17+
:::image type="content" source="./media/tutorial-java-quarkus-postgresql/quarkus-crud-running-locally.png" alt-text="Screenshot of Quarkus application storing data in PostgreSQL.":::
1818

1919
In this tutorial, you learn how to:
2020

@@ -30,7 +30,7 @@ In this tutorial, you learn how to:
3030

3131
## Prerequisites
3232

33-
* [Azure CLI](/cli/azure/overview), installed on your own computer.
33+
* [Azure CLI](/cli/azure/overview), installed on your own computer.
3434
* [Git](https://git-scm.com/)
3535
* [Java JDK](/azure/developer/java/fundamentals/java-support-on-azure)
3636
* [Maven](https://maven.apache.org)
@@ -39,7 +39,6 @@ In this tutorial, you learn how to:
3939

4040
This tutorial uses a sample Fruits list app with a web UI that calls a Quarkus REST API backed by [Azure Database for PostgreSQL](../postgresql/index.yml). The code for the app is available [on GitHub](https://github.com/quarkusio/quarkus-quickstarts/tree/main/hibernate-orm-panache-quickstart). To learn more about writing Java apps using Quarkus and PostgreSQL, see the [Quarkus Hibernate ORM with Panache Guide](https://quarkus.io/guides/hibernate-orm-panache) and the [Quarkus Datasource Guide](https://quarkus.io/guides/datasource).
4141

42-
4342
Run the following commands in your terminal to clone the sample repo and set up the sample app environment.
4443

4544
```bash
@@ -54,7 +53,7 @@ cd quarkus-quickstarts/hibernate-orm-panache-quickstart
5453
```azurecli
5554
az login
5655
az account set -s <your-subscription-id>
57-
```
56+
```
5857
5958
2. Create an Azure Resource Group, noting the resource group name (referred to with `$RESOURCE_GROUP` later on)
6059
@@ -63,6 +62,7 @@ cd quarkus-quickstarts/hibernate-orm-panache-quickstart
6362
--name <a-resource-group-name> \
6463
--location <a-resource-group-region>
6564
```
65+
6666
3. Create an App Service Plan. The App Service Plan is the compute container, it determines your cores, memory, price, and scale.
6767
6868
```azurecli
@@ -72,6 +72,7 @@ cd quarkus-quickstarts/hibernate-orm-panache-quickstart
7272
--sku B2 \
7373
--is-linux
7474
```
75+
7576
4. Create an app service within the App Service Plan.
7677
7778
```azurecli
@@ -82,6 +83,7 @@ cd quarkus-quickstarts/hibernate-orm-panache-quickstart
8283
--runtime "JAVA|11-java11" \
8384
--plan "quarkus-tutorial-app-service-plan"
8485
```
86+
8587
> [!IMPORTANT]
8688
> The `WEBAPP_NAME` must be **unique across all Azure**. A good pattern is to use a combination of your company name or initials of your name along with a good webapp name, for example `johndoe-quarkus-app`.
8789
@@ -140,6 +142,7 @@ Follow these steps to create an Azure PostgreSQL database in your subscription.
140142
--start-ip-address 0.0.0.0 \
141143
--end-ip-address 0.0.0.0
142144
```
145+
143146
3. Create a database named `fruits` within the Postgres service with this command:
144147
145148
```azurecli
@@ -181,6 +184,9 @@ Use Maven to run the sample.
181184
mvn quarkus:dev
182185
```
183186

187+
> [!IMPORTANT]
188+
> Be sure you have the H2 JDBC driver installed. You can add it using the following Maven command: `./mvnw quarkus:add-extension -Dextensions="jdbc-h2"`.
189+
184190
This will build the app, run its unit tests, and then start the application in developer live coding. You should see:
185191

186192
```output
@@ -196,7 +202,7 @@ INFO [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cdi, hiber
196202

197203
You can access Quarkus app locally by typing the `w` character into the console, or using this link once the app is started: `http://localhost:8080/`.
198204

199-
![Screenshot of Quarkus application storing data in PostgreSQL.](./media/tutorial-java-quarkus-postgresql/quarkus-crud-running-locally.png)
205+
:::image type="content" source="./media/tutorial-java-quarkus-postgresql/quarkus-crud-running-locally.png" alt-text="Screenshot of Quarkus application storing data in PostgreSQL.":::
200206

201207
If you see exceptions in the output, double-check that the configuration values for `%dev` are correct.
202208

@@ -221,6 +227,7 @@ az webapp config appsettings set \
221227
'PORT=8080' \
222228
'WEBSITES_PORT=8080'
223229
```
230+
224231
> [!NOTE]
225232
> The use of single quotes (`'`) to surround the settings is required if your password has special characters.
226233
@@ -258,7 +265,7 @@ az webapp browse \
258265
259266
You should see the app running with the remote URL in the address bar:
260267

261-
![Screenshot of Quarkus application storing data in PostgreSQL running remotely.](./media/tutorial-java-quarkus-postgresql/quarkus-crud-running-remotely.png)
268+
:::image type="content" source="./media/tutorial-java-quarkus-postgresql/quarkus-crud-running-remotely.png" alt-text="Screenshot of Quarkus application storing data in PostgreSQL running remotely.":::
262269

263270
If you see errors, use the following section to access the log file from the running app:
264271

@@ -279,14 +286,15 @@ az appservice plan update --number-of-workers 2 \
279286
## Clean up resources
280287

281288
If you don't need these resources for another tutorial (see [Next steps](#next-steps)), you can delete them by running the following command in the Cloud Shell or on your local terminal:
289+
282290
```azurecli
283291
az group delete --name $RESOURCE_GROUP --yes
284292
```
285293

286294
## Next steps
287295

288296
[Azure for Java Developers](/java/azure/)
289-
[Quarkus](https://quarkus.io),
297+
[Quarkus](https://quarkus.io),
290298
[Getting Started with Quarkus](https://quarkus.io/get-started/),
291299
and
292300
[App Service Linux](overview.md).
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
title: 'Tutorial: Access data with managed identity in Java'
3+
description: Secure Azure Database for PostgreSQL connectivity with managed identity from a sample Java Tomcat app, and apply it to other Azure services.
4+
ms.devlang: java
5+
ms.topic: tutorial
6+
ms.date: 09/26/2022
7+
author: shizn
8+
ms.author: xshi
9+
---
10+
11+
# Tutorial: Connect to a PostgreSQL Database from Java Tomcat App Service without secrets using a managed identity
12+
13+
[Azure App Service](overview.md) provides a highly scalable, self-patching web hosting service in Azure. It also provides a [managed identity](overview-managed-identity.md) for your app, which is a turn-key solution for securing access to [Azure Database for PostgreSQL](/azure/postgresql/) and other Azure services. Managed identities in App Service make your app more secure by eliminating secrets from your app, such as credentials in the environment variables. In this tutorial, you will learn how to:
14+
15+
> [!div class="checklist"]
16+
> * Create a PostgreSQL database.
17+
> * Deploy the sample app to Azure App Service on Tomcat using WAR packaging.
18+
> * Configure a Spring Boot web application to use Azure AD authentication with PostgreSQL Database.
19+
> * Connect to PostgreSQL Database with Managed Identity using Service Connector.
20+
21+
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
22+
23+
## Prerequisites
24+
25+
* [Git](https://git-scm.com/)
26+
* [Java JDK](/azure/developer/java/fundamentals/java-support-on-azure)
27+
* [Maven](https://maven.apache.org)
28+
* [Azure CLI](/cli/azure/overview). This quickstart requires that you are running the latest [edge build of Azure CLI](https://github.com/Azure/azure-cli/blob/dev/doc/try_new_features_before_release.md). [Download and install the edge builds](https://github.com/Azure/azure-cli#edge-builds) for your platform.
29+
30+
## Clone the sample app and prepare the repo
31+
32+
Run the following commands in your terminal to clone the sample repo and set up the sample app environment.
33+
34+
```bash
35+
git clone https://github.com/Azure-Samples/Passwordless-Connections-for-Java-Apps
36+
cd Passwordless-Connections-for-Java-Apps/Tomcat/checklist/
37+
```
38+
39+
## Create an Azure Postgres DB
40+
41+
Follow these steps to create an Azure Database for Postgres Single Server in your subscription. The Spring Boot app will connect to this database and store its data when running, persisting the application state no matter where you run the application.
42+
43+
1. Sign into the Azure CLI, and optionally set your subscription if you have more than one connected to your login credentials.
44+
45+
```azurecli-interactive
46+
az login
47+
az account set --subscription <subscription-ID>
48+
```
49+
50+
1. Create an Azure Resource Group, noting the resource group name.
51+
52+
```azurecli-interactive
53+
RESOURCE_GROUP=<resource-group-name>
54+
LOCATION=eastus
55+
56+
az group create --name $RESOURCE_GROUP --location $LOCATION
57+
```
58+
59+
1. Create an Azure Postgres Database server. The server is created with an administrator account, but it won't be used as we'll use the Azure Active Directory (Azure AD) admin account to perform administrative tasks.
60+
61+
```azurecli-interactive
62+
POSTGRESQL_ADMIN_USER=azureuser
63+
# PostgreSQL admin access rights won't be used as Azure AD authentication is leveraged to administer the database.
64+
POSTGRESQL_ADMIN_PASSWORD=<admin-password>
65+
POSTGRESQL_HOST=<postgresql-host-name>
66+
67+
# Create a PostgreSQL server.
68+
az postgres server create \
69+
--resource-group $RESOURCE_GROUP \
70+
--name $POSTGRESQL_HOST \
71+
--location $LOCATION \
72+
--admin-user $POSTGRESQL_ADMIN_USER \
73+
--admin-password $POSTGRESQL_ADMIN_PASSWORD \
74+
--public-network-access 0.0.0.0 \
75+
--sku-name B_Gen5_1
76+
```
77+
78+
1. Create a database for the application.
79+
80+
```azurecli-interactive
81+
DATABASE_NAME=checklist
82+
83+
az postgres db create \
84+
--resource-group $RESOURCE_GROUP \
85+
--server-name $POSTGRESQL_HOST \
86+
--name $DATABASE_NAME
87+
```
88+
89+
## Deploy the application to App Service
90+
91+
Follow these steps to build a WAR file and deploy to Azure App Service on Tomcat using a WAR packaging.
92+
93+
The changes you made in *application.properties* also apply to the managed identity, so the only thing to do is to remove the existing application settings in App Service.
94+
95+
1. The sample app contains a *pom-war.xml* file that can generate the WAR file. Run the following command to build the app.
96+
97+
```bash
98+
mvn clean package -f pom-war.xml
99+
```
100+
101+
1. Create an Azure App Service resource on Linux using Tomcat 9.0.
102+
103+
```azurecli-interactive
104+
# Create an App Service plan
105+
az appservice plan create \
106+
--resource-group $RESOURCE_GROUP \
107+
--name $APPSERVICE_PLAN \
108+
--location $LOCATION \
109+
--sku B1 \
110+
--is-linux
111+
112+
# Create an App Service resource.
113+
az webapp create \
114+
--resource-group $RESOURCE_GROUP \
115+
--name $APPSERVICE_NAME \
116+
--plan $APPSERVICE_PLAN \
117+
--runtime "TOMCAT:9.0-jre8"
118+
```
119+
120+
1. Deploy the WAR package to App Service.
121+
122+
```azurecli-interactive
123+
az webapp deploy \
124+
--resource-group $RESOURCE_GROUP \
125+
--name $APPSERVICE_NAME \
126+
--src-path target/app.war \
127+
--type war
128+
```
129+
130+
## Connect Postgres Database with identity connectivity
131+
132+
Next, connect your app to an Postgres Database Single Server with a system-assigned managed identity using Service Connector. To do this, run the [az webapp connection create](/cli/azure/webapp/connection/create#az-webapp-connection-create-postgres) command.
133+
134+
```azurecli-interactive
135+
az webapp connection create postgres \
136+
--resource-group $RESOURCE_GROUP \
137+
--name $APPSERVICE_NAME \
138+
--target-resource-group $RESOURCE_GROUP \
139+
--server $POSTGRESQL_HOST \
140+
--database $DATABASE_NAME \
141+
--system-assigned-identity
142+
```
143+
144+
This command creates a connection between your web app and your PostgreSQL server, and manages authentication through a system-assigned managed identity.
145+
146+
## View sample web app
147+
148+
Run the following command to open the deployed web app in your browser.
149+
150+
```azurecli-interactive
151+
az webapp browse \
152+
--resource-group $RESOURCE_GROUP \
153+
--name MyWebapp \
154+
--name $APPSERVICE_NAME
155+
```
156+
157+
[!INCLUDE [cli-samples-clean-up](../../includes/cli-samples-clean-up.md)]
158+
159+
## Next steps
160+
161+
Learn more about running Java apps on App Service on Linux in the developer guide.
162+
163+
> [!div class="nextstepaction"]
164+
> [Java in App Service Linux dev guide](configure-language-java.md?pivots=platform-linux)

articles/container-apps/TOC.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@
8989
- name: Set up custom domains and certificates
9090
href: custom-domains-certificates.md
9191
- name: Connect to a cloud service using Service Connector
92-
href: service-connector.md
92+
items:
93+
- name: .NET app with Blob Storage
94+
href: service-connector.md
95+
- name: Java Quarkus app with Postgres DB
96+
href: tutorial-java-quarkus-connect-managed-identity-postgresql-database.md
9397
- name: Connect multiple apps
9498
href: connect-apps.md
9599
- name: Publish revisions with GitHub Actions

0 commit comments

Comments
 (0)