|
| 1 | +--- |
| 2 | +title: Enable MSAL4J Logging in a Spring Boot Web Application in Microsoft Entra ID |
| 3 | +description: Discusses how to enable MSAL4J logging in a Spring Boot web application in Microsoft Entra. |
| 4 | +ms.date: 03/10/2025 |
| 5 | +ms.author: bachoang |
| 6 | +ms.service: entra-id |
| 7 | +ms.custom: sap:Microsoft Entra App Integration and Development |
| 8 | +--- |
| 9 | + |
| 10 | +# Enable MSAL4J logging in a Spring Boot web application |
| 11 | + |
| 12 | +This article provides step-by-step instructions to enable [Microsoft Authentication Library for Java](https://github.com/AzureAD/microsoft-authentication-library-for-java) (MSAL4J) logging by using the [Logback framework](https://logback.qos.ch/) in a Spring Boot web application. |
| 13 | + |
| 14 | +## Code sample |
| 15 | + |
| 16 | +The complete code sample and configuration guide for this implementation are available on [GitHub](https://github.com/bachoang/MSAL4J_SpringBoot_Logging/tree/main/msal-b2c-web-sample). |
| 17 | + |
| 18 | +## Enable MSAL4J logging |
| 19 | + |
| 20 | +1. Add the following dependency to your Pom.xml file to include the Logback framework: |
| 21 | + |
| 22 | + ```xml |
| 23 | + <dependency> |
| 24 | + <groupid>ch.qos.logback</groupid> |
| 25 | + <artifactid>logback-classic</artifactid> |
| 26 | + <version>1.2.3</version> |
| 27 | + </dependency> |
| 28 | + ``` |
| 29 | + |
| 30 | +2. In your app project, create a file in the **src/main/resources** folder, and name the file **Logback.xml**. Then, add the following content: |
| 31 | + |
| 32 | + ```xml |
| 33 | + <?xml version="1.0" encoding="UTF-8"?> |
| 34 | + <configuration> |
| 35 | + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
| 36 | + <encoder> |
| 37 | + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> |
| 38 | + </encoder> |
| 39 | + </appender> |
| 40 | + <root level="debug"> |
| 41 | + <appender-ref ref="STDOUT" /> |
| 42 | + </root> |
| 43 | + </configuration> |
| 44 | + ``` |
| 45 | + |
| 46 | + This Appender configuration logs messages to the console. You can adjust the logging level to `error`, `warn`, `info`, or `verbose` based on your preference. For more information, see [LogBack: Appenders](https://logback.qos.ch/manual/appenders.html). |
| 47 | +3. Set the **logging.config** property to the location of the **Logback.xml** file before the main method: |
| 48 | + |
| 49 | + ```java |
| 50 | + @SpringBootApplication |
| 51 | + public class MsalB2CWebSampleApplication { |
| 52 | + |
| 53 | + static { System.setProperty("logging.config", "C:\\Users\\<your path>\\src\\main\\resources\\logback.xml");} |
| 54 | + public static void main(String[] args) { |
| 55 | + // Console.log("main"); |
| 56 | + // System.console().printf("hello"); |
| 57 | + // System.out.printf("Hello %s!%n", "World"); |
| 58 | + System.out.printf("%s%n", "Hello World"); |
| 59 | + SpringApplication.run(MsalB2CWebSampleApplication.class, args); |
| 60 | + } |
| 61 | + } |
| 62 | + ``` |
| 63 | + |
| 64 | +## Configuration for running the code sample |
| 65 | + |
| 66 | +### Enable HTTPs support |
| 67 | + |
| 68 | +This code sample is set up to run on the local server (localhost) by using the HTTPS protocol. Follow the steps in [Configure the sample to use your Azure AD B2C tenant](https://github.com/bachoang/MSAL4J_SpringBoot_Logging/tree/main/msal-b2c-web-sample#step-2--configure-the-sample-to-use-your-azure-ad-b2c-tenant) to generate a self-signed certificate. Put the **keystore.p12** file in the resources folder. |
| 69 | + |
| 70 | +### App registration configuration |
| 71 | + |
| 72 | +To configure app registration in Azure AD B2C, follow these steps: |
| 73 | + |
| 74 | +1. Create two app registrations in your Azure AD B2C tenant: One for the web application and the other for the web API. |
| 75 | +2. Expose the required scope in the web API. For more information, see [Configure web API app scopes](/azure/active-directory-b2c/configure-authentication-sample-web-app-with-api?tabs=visual-studio#step-22-configure-web-api-app-scopes). |
| 76 | +3. Configure the web API scope in the **API Permissions** blade for the web application. |
| 77 | +4. Grant admin consent to all configured permissions in the web application. |
| 78 | + |
| 79 | +For more information, see [Configure authentication in a sample web app that calls a web API by using Azure AD B2C](/azure/active-directory-b2c/configure-authentication-sample-web-app-with-api). |
| 80 | + |
| 81 | +Example configuration: |
| 82 | + |
| 83 | + :::image type="content" source="media/enable-msal4j-logging-spring-boot-webapp/app-reg.png" alt-text="Diagram that shows configured app registration." border="true" lightbox="media/enable-msal4j-logging-spring-boot-webapp/app-reg.png"::: |
| 84 | + |
| 85 | +## Logging output example |
| 86 | + |
| 87 | +If the app is configured correctly, the logging output should resemble the following output. |
| 88 | + |
| 89 | + :::image type="content" source="media/enable-msal4j-logging-spring-boot-webapp/log-sample.png" alt-text="Diagram that shows logging output." border="true" lightbox="media/enable-msal4j-logging-spring-boot-webapp/log-sample.png"::: |
0 commit comments