|
| 1 | +--- |
| 2 | +title: Configure Java apps |
| 3 | +description: Learn how to configure Java apps to run on Azure App Service. This article shows the most common configuration tasks. |
| 4 | +keywords: azure app service, web app, windows, oss, java, tomcat, jboss |
| 5 | +ms.devlang: java |
| 6 | +ms.topic: article |
| 7 | +ms.date: 05/13/2024 |
| 8 | +ms.custom: devx-track-java, devx-track-azurecli, devx-track-extended-java, linux-related-content |
| 9 | +zone_pivot_groups: app-service-platform-windows-linux |
| 10 | +adobe-target: true |
| 11 | +author: cephalin |
| 12 | +ms.author: cephalin |
| 13 | +--- |
| 14 | + |
| 15 | +## Configure security for a Java app in Azure App Service |
| 16 | + |
| 17 | +This article shows how to confgure Java-specific security settings in App Service. Java applications running in App Service have the same set of [security best practices](../security/fundamentals/paas-applications-using-app-services.md) as other applications. |
| 18 | + |
| 19 | +[!INCLUDE [java-variants](includes/configure-language-java/java-variants.md)] |
| 20 | + |
| 21 | +### Authenticate users (Easy Auth) |
| 22 | + |
| 23 | +Set up app authentication in the Azure portal with the **Authentication and Authorization** option. From there, you can enable authentication using Microsoft Entra ID or social sign-ins like Facebook, Google, or GitHub. Azure portal configuration only works when configuring a single authentication provider. For more information, see [Configure your App Service app to use Microsoft Entra sign-in](configure-authentication-provider-aad.md) and the related articles for other identity providers. If you need to enable multiple sign-in providers, follow the instructions in [Customize sign-ins and sign-outs](configure-authentication-customize-sign-in-out.md). |
| 24 | + |
| 25 | +::: zone pivot="java-javase" |
| 26 | + |
| 27 | +Spring Boot developers can use the [Microsoft Entra Spring Boot starter](/java/azure/spring-framework/configure-spring-boot-starter-java-app-with-azure-active-directory) to secure applications using familiar Spring Security annotations and APIs. Be sure to increase the maximum header size in your *application.properties* file. We suggest a value of `16384`. |
| 28 | + |
| 29 | +::: zone-end |
| 30 | + |
| 31 | +::: zone pivot="java-tomcat" |
| 32 | + |
| 33 | +Your Tomcat application can access the user's claims directly from the servlet by casting the Principal object to a Map object. The `Map` object maps each claim type to a collection of the claims for that type. In the following code example, `request` is an instance of `HttpServletRequest`. |
| 34 | + |
| 35 | +```java |
| 36 | +Map<String, Collection<String>> map = (Map<String, Collection<String>>) request.getUserPrincipal(); |
| 37 | +``` |
| 38 | + |
| 39 | +Now you can inspect the `Map` object for any specific claim. For example, the following code snippet iterates through all the claim types and prints the contents of each collection. |
| 40 | + |
| 41 | +```java |
| 42 | +for (Object key : map.keySet()) { |
| 43 | + Object value = map.get(key); |
| 44 | + if (value != null && value instanceof Collection { |
| 45 | + Collection claims = (Collection) value; |
| 46 | + for (Object claim : claims) { |
| 47 | + System.out.println(claims); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +``` |
| 52 | + |
| 53 | +To sign out users, use the `/.auth/ext/logout` path. To perform other actions, see the documentation on [Customize sign-ins and sign-outs](configure-authentication-customize-sign-in-out.md). There's also official documentation on the Tomcat [HttpServletRequest interface](https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletRequest.html) and its methods. The following servlet methods are also hydrated based on your App Service configuration: |
| 54 | +
|
| 55 | +```java |
| 56 | +public boolean isSecure() |
| 57 | +public String getRemoteAddr() |
| 58 | +public String getRemoteHost() |
| 59 | +public String getScheme() |
| 60 | +public int getServerPort() |
| 61 | +``` |
| 62 | +
|
| 63 | +To disable this feature, create an Application Setting named `WEBSITE_AUTH_SKIP_PRINCIPAL` with a value of `1`. To disable all servlet filters added by App Service, create a setting named `WEBSITE_SKIP_FILTERS` with a value of `1`. |
| 64 | +
|
| 65 | +::: zone-end |
| 66 | +
|
| 67 | +::: zone pivot="java-jboss" |
| 68 | +
|
| 69 | +For JBoss EAP, `[TODO]`. |
| 70 | +
|
| 71 | +::: zone-end |
| 72 | +
|
| 73 | +### Configure TLS/SSL |
| 74 | +
|
| 75 | +To upload an existing TLS/SSL certificate and bind it to your application's domain name, follow the instructions in [Secure a custom DNS name with an TLS/SSL binding in Azure App Service](configure-ssl-bindings.md). You can also configure the app to enforce TLS/SSL. |
| 76 | + |
| 77 | +### Use KeyVault References |
| 78 | + |
| 79 | +[Azure KeyVault](../key-vault/general/overview.md) provides centralized secret management with access policies and audit history. You can store secrets (such as passwords or connection strings) in KeyVault and access these secrets in your application through environment variables. |
| 80 | + |
| 81 | +First, follow the instructions for [granting your app access to a key vault](app-service-key-vault-references.md#grant-your-app-access-to-a-key-vault) and [making a KeyVault reference to your secret in an Application Setting](app-service-key-vault-references.md#source-app-settings-from-key-vault). You can validate that the reference resolves to the secret by printing the environment variable while remotely accessing the App Service terminal. |
| 82 | + |
| 83 | +::: zone pivot="java-javase" |
| 84 | + |
| 85 | +For Spring configuration files, see this documentation on [externalized configurations](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html). |
| 86 | + |
| 87 | +To inject these secrets in your Spring configuration file, use environment variable injection syntax (`${MY_ENV_VAR}`). |
| 88 | + |
| 89 | +::: zone-end |
| 90 | + |
| 91 | +::: zone pivot="java-tomcat" |
| 92 | + |
| 93 | +To inject these secrets in your Tomcat configuration file, use environment variable injection syntax (`${MY_ENV_VAR}`). |
| 94 | + |
| 95 | +::: zone-end |
| 96 | + |
| 97 | +### Use the Java key store in Linux |
| 98 | + |
| 99 | +By default, any public or private certificates [uploaded to App Service Linux](configure-ssl-certificate.md) are loaded into the respective Java key stores as the container starts. After uploading your certificate, you'll need to restart your App Service for it to be loaded into the Java key store. Public certificates are loaded into the key store at `$JRE_HOME/lib/security/cacerts`, and private certificates are stored in `$JRE_HOME/lib/security/client.jks`. |
| 100 | +
|
| 101 | +More configuration might be necessary for encrypting your JDBC connection with certificates in the Java key store. Refer to the documentation for your chosen JDBC driver. |
| 102 | +
|
| 103 | +- [PostgreSQL](https://jdbc.postgresql.org/documentation/ssl/) |
| 104 | +- [SQL Server](/sql/connect/jdbc/connecting-with-ssl-encryption) |
| 105 | +- [MongoDB](https://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/ssl/) |
| 106 | +- [Cassandra](https://docs.datastax.com/en/developer/java-driver/4.3/) |
| 107 | +
|
| 108 | +#### Initialize the Java key store in Linux |
| 109 | +
|
| 110 | +To initialize the `import java.security.KeyStore` object, load the keystore file with the password. The default password for both key stores is `changeit`. |
| 111 | +
|
| 112 | +```java |
| 113 | +KeyStore keyStore = KeyStore.getInstance("jks"); |
| 114 | +keyStore.load( |
| 115 | + new FileInputStream(System.getenv("JRE_HOME")+"/lib/security/cacerts"), |
| 116 | + "changeit".toCharArray()); |
| 117 | +
|
| 118 | +KeyStore keyStore = KeyStore.getInstance("pkcs12"); |
| 119 | +keyStore.load( |
| 120 | + new FileInputStream(System.getenv("JRE_HOME")+"/lib/security/client.jks"), |
| 121 | + "changeit".toCharArray()); |
| 122 | +``` |
| 123 | +
|
| 124 | +#### Manually load the key store in Linux |
| 125 | +
|
| 126 | +You can load certificates manually to the key store. Create an app setting, `SKIP_JAVA_KEYSTORE_LOAD`, with a value of `1` to disable App Service from loading the certificates into the key store automatically. All public certificates uploaded to App Service via the Azure portal are stored under `/var/ssl/certs/`. Private certificates are stored under `/var/ssl/private/`. |
| 127 | +
|
| 128 | +You can interact or debug the Java Key Tool by [opening an SSH connection](configure-linux-open-ssh-session.md) to your App Service and running the command `keytool`. See the [Key Tool documentation](https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html) for a list of commands. For more information on the KeyStore API, see [the official documentation](https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html). |
| 129 | +
|
| 130 | +# [Linux](#tab/linux) |
| 131 | +
|
| 132 | +1. Create an AppDynamics account at [AppDynamics.com](https://www.appdynamics.com/community/register/) |
| 133 | +2. Download the Java agent from the AppDynamics website. The file name is similar to *AppServerAgent-x.x.x.xxxxx.zip* |
| 134 | +3. [SSH into your App Service instance](configure-linux-open-ssh-session.md) and create a new directory */home/site/wwwroot/apm*. |
| 135 | +4. Upload the Java agent files into a directory under */home/site/wwwroot/apm*. The files for your agent should be in */home/site/wwwroot/apm/appdynamics*. |
| 136 | +5. In the Azure portal, browse to your application in App Service and create a new Application Setting. |
| 137 | +
|
| 138 | + ::: zone pivot="java-javase" |
| 139 | +
|
| 140 | + Create an environment variable named `JAVA_OPTS` with the value `-javaagent:/home/site/wwwroot/apm/appdynamics/javaagent.jar -Dappdynamics.agent.applicationName=<app-name>` where `<app-name>` is your App Service name. If you already have an environment variable for `JAVA_OPTS`, append the `-javaagent:/...` option to the end of the current value. |
| 141 | +
|
| 142 | + ::: zone-end |
| 143 | +
|
| 144 | + ::: zone pivot="java-tomcat" |
| 145 | +
|
| 146 | + Create an environment variable named `CATALINA_OPTS` with the value `-javaagent:/home/site/wwwroot/apm/appdynamics/javaagent.jar -Dappdynamics.agent.applicationName=<app-name>` where `<app-name>` is your App Service name. If you already have an environment variable for `CATALINA_OPTS`, append the `-javaagent:/...` option to the end of the current value. |
| 147 | +
|
| 148 | + ::: zone-end |
| 149 | +
|
| 150 | + ::: zone pivot="java-jboss" |
| 151 | +
|
| 152 | + For **JBoss EAP**, `[TODO]`. |
| 153 | +
|
| 154 | + ::: zone-end |
| 155 | +
|
| 156 | +
|
| 157 | +--- |
| 158 | +
|
| 159 | +## Next steps |
| 160 | +
|
| 161 | +Visit the [Azure for Java Developers](/java/azure/) center to find Azure quickstarts, tutorials, and Java reference documentation. |
| 162 | +
|
| 163 | +- [App Service Linux FAQ](faq-app-service-linux.yml) |
| 164 | +- [Environment variables and app settings reference](reference-app-settings.md) |
0 commit comments