大网数据广告活动
域与目录服务

Enable MSAL4J Logging in a Spring Boot Web Application in Microsoft Entra ID

· 52CE 我爱测

Discusses how to enable MSAL4J logging in a Spring Boot web application in Microsoft Entra.

Summary

This article provides step-by-step instructions to enable Microsoft Authentication Library for Java (MSAL4J) logging by using the Logback framework in a Spring Boot web application.

Code sample

The complete code sample and configuration guide for this implementation are available on GitHub.

Enable MSAL4J logging

  1. Add the following dependency to your Pom.xml file to include the Logback framework:

    <dependency>
        <groupid>ch.qos.logback</groupid>
        <artifactid>logback-classic</artifactid>
        <version>1.2.3</version>
    </dependency>
    
  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:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
        <root level="debug">
            <appender-ref ref="STDOUT" />
        </root>
    </configuration>
    

    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.

  3. Set the logging.config property to the location of the Logback.xml file before the main method:

    @SpringBootApplication
    public class MsalB2CWebSampleApplication {
    
    	static { System.setProperty("logging.config", "C:\\Users\\<your path>\\src\\main\\resources\\logback.xml");}
    	public static void main(String[] args) {
    		// Console.log("main");
    		// System.console().printf("hello");
    		// System.out.printf("Hello %s!%n", "World");
    		System.out.printf("%s%n", "Hello World");
    		SpringApplication.run(MsalB2CWebSampleApplication.class, args);
    	}
    }
    

Configuration for running the code sample

Enable HTTPs support

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 to generate a self-signed certificate. Put the keystore.p12 file in the resources folder.

App registration configuration

To configure app registration in Azure AD B2C, follow these steps:

  1. Create two app registrations in your Azure AD B2C tenant: One for the web application and the other for the web API.
  2. Expose the required scope in the web API. For more information, see Configure web API app scopes.
  3. Configure the web API scope in the API Permissions blade for the web application.
  4. Grant admin consent to all configured permissions in the web application.

For more information, see Configure authentication in a sample web app that calls a web API by using Azure AD B2C.

Example configuration:

:::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":::

Logging output example

If the app is configured correctly, the logging output should resemble the following output.

:::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":::

Third-party information disclaimer

The third-party products that this article discusses are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, about the performance or reliability of these products.

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors (upstream author metadata: bachoang). Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 03/10/2025
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。